package com.github.mondei1.offpass

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.github.mondei1.offpass.entities.Compression
import com.github.mondei1.offpass.entities.CompressionHelper
import com.google.zxing.integration.android.IntentIntegrator
import com.google.zxing.integration.android.IntentResult
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.supportActionBar?.hide()

        setContentView(R.layout.activity_main)

        var dbHandler = CompressionHelper(this, null)
        dbHandler.writableDatabase.execSQL("DELETE FROM `${CompressionHelper.TABLE_NAME}`;")

        dbHandler.writableDatabase.close()

        main_screen.setOnClickListener {
            IntentIntegrator(this)
                .setPrompt("Scan OffPass created QR-Code")
                .setBeepEnabled(false)
                .setBarcodeImageEnabled(false)
                .initiateScan()
        }

        add_button.setOnClickListener {
            val intent: Intent = Intent(this, CreateActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        var result: IntentResult =
            IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
        if (result.contents == null) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show()
        } else {
            // TODO: Check if scanned QR-Code is an OffPass one.
            val intent: Intent = Intent(this, PassphraseActivity::class.java)
            intent.putExtra("raw", result.contents)
            startActivity(intent)
            //Toast.makeText(this, "Scanned: " + result.contents, Toast.LENGTH_LONG).show()
        }
    }

}