package com.example.surveillanceapp;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ActivitySettings extends AppCompatActivity {
    String email, password;
    RadioButton rb1, rb2, rb3, rb4;
    CheckBox remSettings;
    EditText mail;
    EditText mailPass;


   private SharedPreferences remPrefs;
   private static final String PREFS_NAME= "PrefsFile";



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setTitle("Settings");
        setContentView(R.layout.activity_settings);


       remPrefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
       String checkbox = remPrefs.getString("remember", "");
       if(checkbox.equals("true")){
           Toast.makeText(this, "Choose preferred image size", Toast.LENGTH_LONG).show();


       }else if (checkbox.equals("false")){

           Toast.makeText(this, "Please fill your email settings and choose preferred image size", Toast.LENGTH_LONG).show();

       }


        Button btMail = findViewById(R.id.btPubMail);

        Button btMailpass = findViewById(R.id.btPubMailpass);
        remSettings = findViewById(R.id.remSettings);

        mail = findViewById(R.id.textMail);
        mailPass = findViewById(R.id.textMailpass);

        initRadioButtons ();


        btMail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String topic = "Rpi/Address";
                email= mail.getText().toString();

                ((CustomApplication)getApplicationContext()).getConnectionHandler().sendMessage(topic,email);


                if (remSettings.isChecked()) {
                    Boolean boolIsChecked = remSettings.isChecked();
                    SharedPreferences.Editor editor = remPrefs.edit();
                    editor.putString("pref_mail", mail.getText().toString() );
                    editor.apply();
                    Toast.makeText(getApplicationContext(), "Settings are saved", Toast.LENGTH_SHORT).show();
                } else {
                    remPrefs.edit().clear().apply();
                }

                mail.getText().clear();
            }
        });

        btMailpass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String topic = "Rpi/Password";
                password= mailPass.getText().toString();

                ((CustomApplication)getApplicationContext()).getConnectionHandler().sendMessage(topic,password);

                //Rememember pass
                if (remSettings.isChecked()) {
                    Boolean boolIsChecked = remSettings.isChecked();
                    SharedPreferences.Editor editor = remPrefs.edit();
                    editor.putString("pref_mail_password", mailPass.getText().toString() );
                    editor.apply();
                    Toast.makeText(getApplicationContext(), "Settings are saved", Toast.LENGTH_SHORT).show();
                } else {
                    remPrefs.edit().clear().apply();
                }

                mailPass.getText().clear();
            }
        });


      remSettings.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

              if(compoundButton.isChecked()){
                  SharedPreferences preferences = getSharedPreferences("checkbox", MODE_PRIVATE);
                  SharedPreferences.Editor editor = preferences.edit();
                  editor.putString("remember", "true");
                  editor.apply();
                  Toast.makeText(ActivitySettings.this, "Checked", Toast.LENGTH_SHORT).show();

              } else if (!compoundButton.isChecked()){
                  SharedPreferences preferences = getSharedPreferences("checkbox", MODE_PRIVATE);
                  SharedPreferences.Editor editor = preferences.edit();
                  editor.putString("remember", "false");
                  editor.apply();
                  Toast.makeText(ActivitySettings.this, "Unchecked", Toast.LENGTH_SHORT).show();
              }
          }
      });

        getPrefData();


    }
    public void initRadioButtons (){

        rb1 = findViewById(R.id.rB1);
        rb2 = findViewById(R.id.rB2);
        rb3 = findViewById(R.id.rB3);
        rb4 = findViewById(R.id.rB4);
    }

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.rB1:
                if (checked){
                    String topic = "Rpi/Quality";
                    String message = "550";
                    ((CustomApplication)getApplicationContext()).getConnectionHandler().sendMessage(topic,message);

                    rb2.setChecked(false);
                    rb3.setChecked(false);
                    rb4.setChecked(false);


                }
                break;
            case R.id.rB2:
                if (checked)
                {
                    String topic = "Rpi/Quality";
                    String message = "800";
                    ((CustomApplication)getApplicationContext()).getConnectionHandler().sendMessage(topic,message);

                    rb1.setChecked(false);
                    rb3.setChecked(false);
                    rb4.setChecked(false);
                }
                break;
            case R.id.rB3:
                if (checked)
                {
                    String topic = "Rpi/Quality";
                    String message = "1000";
                    ((CustomApplication)getApplicationContext()).getConnectionHandler().sendMessage(topic,message);

                    rb2.setChecked(false);
                    rb1.setChecked(false);
                    rb4.setChecked(false);
                }
                break;
            case R.id.rB4:
                if (checked)
                {
                    String topic = "Rpi/Quality";
                    String message = "1500";
                    ((CustomApplication)getApplicationContext()).getConnectionHandler().sendMessage(topic,message);

                    rb2.setChecked(false);
                    rb3.setChecked(false);
                    rb1.setChecked(false);
                }
                break;
        }
    }




private void getPrefData() {
        SharedPreferences sp = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        if (sp.contains("pref_mail")
        ) {

        String u = sp.getString("pref_mail", "not found");
        mail.setText(u.toString());
        }
        if (sp.contains("pref_mail_password")
        ) {
        String p = sp.getString("pref_mail_password", "not found");
        mailPass.setText(p.toString());
        }
        if (sp.contains("pref_check")
        ) {
        Boolean b= sp.getBoolean("pref_check", false);
        remSettings.setChecked(b);
        }
        }


    }

