package com.example.onetap_ssh.unlock;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.onetap_ssh.BaseActivity;
import com.example.onetap_ssh.MainActivity;
import com.example.onetap_ssh.R;
import com.example.onetap_ssh.crypto.BiometricKey;
import com.example.onetap_ssh.crypto.Crypto;
import com.example.onetap_ssh.crypto.CryptoException;
import com.example.onetap_ssh.databinding.ActivityUnlockBinding;
import com.example.onetap_ssh.util.BiometricUtil;
import com.example.onetap_ssh.util.DialogUtil;
import com.example.onetap_ssh.util.SettingsUtil;
import com.example.onetap_ssh.util.ThemeUtil;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class UnlockActivity extends BaseActivity {

    private ActivityUnlockBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(!SettingsUtil.isDatabaseEncrypted(this)) {
            success();
            return;
        }

        binding = ActivityUnlockBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ThemeUtil.loadBackground(this);

        if(SettingsUtil.isBiometricEncryption(this) && BiometricUtil.isSupported(this)) {
            Runnable onSuccess = () -> {

            };

            binding.unlockBiometrics.setOnClickListener(view -> BiometricUtil.promptBiometricAuth(this, onSuccess, () -> {}));
            BiometricUtil.promptBiometricAuth(this, onSuccess, () -> {});
        }else {
            binding.unlockBiometrics.setVisibility(View.GONE);
        }

        binding.unlockButton.setOnClickListener(view -> {
            if(binding.unlockPassword.getText().length() == 0) {
                DialogUtil.showErrorDialog(this, getString(R.string.error_unlock_no_password));
                return;
            }

            String password = binding.unlockPassword.getText().toString();

        });
    }

    private void success() {
        if(getIntent() != null && getIntent().hasExtra("contract")) {
            setResult(RESULT_OK);
            finish();
            return;
        }

        Intent m = new Intent(getApplicationContext(), MainActivity.class);
        m.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(m);
        finish();
    }

    private void failure() {
        setResult(RESULT_CANCELED);
        finish();
    }

}
