package eu.smoser.epv

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.google.zxing.integration.android.IntentIntegrator
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(findViewById(R.id.toolbar))

        findViewById<ImageButton>(R.id.buttonScan).setOnClickListener {
            val intentIntegrator = IntentIntegrator(this@MainActivity)
            intentIntegrator.setBeepEnabled(false)
            intentIntegrator.setCameraId(0)
            intentIntegrator.setPrompt("Privileg-Code scannen")
            intentIntegrator.setBarcodeImageEnabled(false)
            intentIntegrator.initiateScan(IntentIntegrator.PRODUCT_CODE_TYPES)
        }

        val editTextSearch = findViewById<EditText>(R.id.editTextSearch)
        findViewById<Button>(R.id.buttonSearch).setOnClickListener {
            val input = editTextSearch.text.toString().trim()
            // TODO: set {13} after development
            if(input.matches("\\d{1,13}".toRegex())) {
                val intent = Intent(this@MainActivity, ResultActivity::class.java)
                intent.putExtra("search", input)
                startActivity(intent)
            } else {
                Toast.makeText(this, getString(R.string.emptySearch), Toast.LENGTH_SHORT).show()
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
        if (result != null) {
            if (result.contents == null) {
                Toast.makeText(this, getString(R.string.cancelled), Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, getString(R.string.scanned, result.contents), Toast.LENGTH_SHORT)
                    .show()
                editTextSearch.setText(result.contents)
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId) {
            R.id.action_settings -> {
                startActivity(Intent(this@MainActivity, SettingsActivity::class.java))
                return true
            }
            R.id.action_about -> {
                startActivity(Intent(this@MainActivity, AboutActivity::class.java))
                return true
            }
            R.id.action_help -> {
                startActivity(Intent(this@MainActivity, HelpActivity::class.java))
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private var backPressedOnce = false
    override fun onBackPressed() {
        if (backPressedOnce) {
            super.onBackPressed()
            return
        }
        this.backPressedOnce = true
        Toast.makeText(this, "Drücke nochmal Zurück,\num die App zu verlassen", Toast.LENGTH_SHORT).show()
        GlobalScope.launch {
            delay(2000L)
            backPressedOnce = false
        }
    }
}