package com.github.mondei1.offpass

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.view.View
import kotlinx.android.synthetic.main.activity_passphrase.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.runBlocking
import javax.crypto.BadPaddingException
import kotlin.reflect.typeOf

class PassphraseActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_passphrase)

        val raw: String = intent.getStringExtra("raw")!!
        val qrSchema: QRSchema = QRSchema(this)

        back.setOnClickListener {
            finish()
        }

        decrypt_button.setOnClickListener {
            decrypt_button.text = "Decrypting ..."
            var _continue = true;

            runBlocking {
                try {
                    qrSchema.decrypt(raw, passphrase_input.text.toString())
                } catch (err: BadPaddingException) {
                    failed.visibility = View.VISIBLE
                    decrypt_button.text = "Decrypt"
                    _continue = false
                    return@runBlocking
                }
            }

            if(!_continue) return@setOnClickListener

            val intent: Intent = Intent(this, ViewActivity::class.java)
            intent.putExtra("decrypted_raw",qrSchema.decrypted_raw)
            startActivity(intent)
        }
    }
}