package org.bandie.yipanic;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.security.crypto.EncryptedSharedPreferences;

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

public class PasscodeActivity<RequestQueue, StringRequest> extends AppCompatActivity {

  private String passcode;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_passcode);

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

    if (this.passcode == null) {
      Intent intent = new Intent(this, CommandActivity.class);
      startActivity(intent);
    }
  }

  @Override
  protected void onResume() {
    super.onResume();
    try {
      EncryptedSharedPreferences sharedPreferences = (EncryptedSharedPreferences) EncryptedSharedPreferences
          .create(
              "Yipanic",
              "Yipanic",
              getApplicationContext(),
              EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
              EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
          );
      this.passcode = sharedPreferences.getString("passcode", null);
    } catch (IOException | GeneralSecurityException e) {
      if (e.getMessage() != null)
        Log.e("Yipanic", e.getMessage());
      else
        Log.e("Yipanic", "[ No exception message ]");
    }

    if (this.passcode == null) {
      Intent intent = new Intent(this, CommandActivity.class);
      startActivity(intent);
    }
  }

  private void checkPasscode() {
    EditText v = findViewById(R.id.pwPasscodeInput);
    if (v.getText().toString().equals(this.passcode) || this.passcode == null) {
      v.setText("");
      Intent intent = new Intent(this, CommandActivity.class);
      startActivity(intent);
    } else {
      v.setText("");
      v.setError("Passcode wrong.");
    }
  }

  public void yip(@SuppressWarnings("unused") View view) {
    int audio;

    switch (new Random().nextInt(4)) {
      default:
      case 0:
        audio = R.raw.yip1;
        break;
      case 1:
        audio = R.raw.yip2;
        break;
      case 2:
        audio = R.raw.yip3;
        break;
      case 3:
        audio = R.raw.yip4;
        break;
    }

    MediaPlayer mp = MediaPlayer.create(this, audio);
    mp.start();
  }

  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_ENTER) {
      this.checkPasscode();
      return true;
    }
    return true;
  }

}
