package org.bandie.yipanic;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;
import androidx.security.crypto.EncryptedSharedPreferences;

import com.google.android.material.snackbar.Snackbar;

import java.io.IOException;
import java.security.GeneralSecurityException;

public class SettingsActivity extends AppCompatActivity {

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case 16908332:
      case R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_activity);
    ActionBar actionBar = getSupportActionBar();
    EditText srv = findViewById(R.id.textServer);

    if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(true);
    }

    try {
      EncryptedSharedPreferences sharedPreferences = (EncryptedSharedPreferences) EncryptedSharedPreferences
          .create(
              "Yipanic",
              "Yipanic",
              getApplicationContext(),
              EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
              EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
          );
      String server = sharedPreferences.getString("server", null);
      srv.setText(server);
    } catch (IOException | GeneralSecurityException e) {
      if (e.getMessage() != null)
        Log.e("Yipanic", e.getMessage());
      else
        Log.e("Yipanic", "[ No exception message ]");
    }

  }

  @Override
  protected void onPause() {
    super.onPause();
    this.finish();
  }

  public void saveServerSettings(View view) {
    Snackbar sbSaved = Snackbar.make(view, "Server settings saved! <3", 3000);
    Snackbar sbFailedToSave = Snackbar.make(view, "Couldn't save settings! :'(", 3000);
    try {
      EditText srv = findViewById(R.id.textServer);
      EditText k = findViewById(R.id.pwKey);
      EditText s = findViewById(R.id.pwSecret);

      EncryptedSharedPreferences sharedPreferences = (EncryptedSharedPreferences) EncryptedSharedPreferences
          .create(
              "Yipanic",
              "Yipanic",
              this,
              EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
              EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
          );

      SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
      sharedPrefsEditor.putString("server", srv.getText().toString());
      if (!k.getText().toString().equals(""))
        sharedPrefsEditor.putString("key", k.getText().toString());
      if (!s.getText().toString().equals(""))
        sharedPrefsEditor.putString("secret", s.getText().toString());

      sharedPrefsEditor.apply();
      sbSaved.show();

    } catch (IOException | GeneralSecurityException e) {
      if (e.getMessage() != null)
        Log.e("Yipanic", e.getMessage());
      else
        Log.e("Yipanic", "[ No exception message ]");
      sbFailedToSave.show();
    }

  }

  public void savePasscode(View view) {
    Snackbar sbSaved = Snackbar.make(view, "Passcode saved! <3", 3000);
    Snackbar sbFailedToSave = Snackbar.make(view, "Couldn't save! :'(", 3000);
    try {
      EditText pc = findViewById(R.id.pwSetPasscode);
      EditText pcc = findViewById(R.id.pwSetPasscodeConfirm);
      String pcS = pc.getText().toString();
      String pccS = pcc.getText().toString();

      if (pcS.equals("") || pccS.equals("")) {
        pc.setError("Passcode is empty. >;(");
        pcc.setError("Passcode is empty. >;(");
        sbFailedToSave.show();
        return;
      }

      if (pcS.length() < 4) {
        pc.setError("Passcode needs to have at least 4 digits.");
        pcc.setError("Passcode needs to have at least 4 digits.");
        sbFailedToSave.show();
        return;
      }

      if (!pcS.equals(pccS)) {
        pc.setError("Passcode didn't match. ;(");
        pcc.setError("Passcode didn't match. ;(");
        sbFailedToSave.show();
        return;
      }

      EncryptedSharedPreferences sharedPreferences = (EncryptedSharedPreferences) EncryptedSharedPreferences
          .create(
              "Yipanic",
              "Yipanic",
              this,
              EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
              EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
          );

      SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
      sharedPrefsEditor.putString("passcode", pcS);
      sharedPrefsEditor.apply();
      sbSaved.show();
      pc.setText("");
      pcc.setText("");
    } catch (IOException | GeneralSecurityException e) {
      if (e.getMessage() != null)
        Log.e("Yipanic", e.getMessage());
      else
        Log.e("Yipanic", "[ No exception message ]");
      sbFailedToSave.show();
    }
  }

  public void deletePasscode(View view) {
    Snackbar sbSaved = Snackbar.make(view, "Passcode deleted! D:", 3000);
    Snackbar sbFailedToSave = Snackbar.make(view, "Couldn't delete passcode! :'(", 3000);
    try {
      EditText pc = findViewById(R.id.pwSetPasscode);
      String pcS = pc.getText().toString();

      EncryptedSharedPreferences sharedPreferences = (EncryptedSharedPreferences) EncryptedSharedPreferences
          .create(
              "Yipanic",
              "Yipanic",
              this,
              EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
              EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
          );

      SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();

      if (pcS.equals(sharedPreferences.getString("passcode", null))) {
        sharedPrefsEditor.remove("passcode");
        sharedPrefsEditor.apply();
        sbSaved.show();
      } else {
        pc.setError("Wrong passcode.");
        sbFailedToSave.show();
      }

    } catch (IOException | GeneralSecurityException e) {
      if (e.getMessage() != null)
        Log.e("Yipanic", e.getMessage());
      else
        Log.e("Yipanic", "[ No exception message ]");
      sbFailedToSave.show();
    }
  }
}