package rowedto.jockeyedto.sheetedto.atticedto.harboredto

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.view.KeyEvent
import androidx.activity.addCallback
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.R
import com.base.datarecovery.databinding.CashedtoBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showPinReEnterDialog
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Substanceedto
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.privacyPinPassword
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Suitcaseedto.array2String
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dishedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto

class Poemedto : Crossingedto<CashedtoBinding>() {

    private val TAG = "PrivacyPinActivity"
    private val firstInput = arrayOf("", "", "", "")

    override val binding: CashedtoBinding by lazy {
        CashedtoBinding.inflate(layoutInflater)
    }


    override fun initView() {
        Spareedto.setStatusBarLightMode(this, true)
        Spareedto.setStatusBarColor(this, Color.TRANSPARENT)
        Scandaledto.admobMaxShowNativeAd(this,binding.tooledto,1)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun initListener() {

        binding.conditionedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }

        setInput()

        setDeleteKey()


    }

    private fun setDeleteKey() {
        arrayOf(binding.worseedto, binding.herdedto, binding.lifestyleedto, binding.cryedto).forEach {
            it.setOnKeyListener { v, actionId, event ->

                if ((event.action == KeyEvent.ACTION_DOWN) && (event.keyCode == KeyEvent.KEYCODE_DEL)) {
                    if (binding.worseedto.hasFocus()) {
                        binding.worseedto.setText("")
                    }
                    if (binding.herdedto.hasFocus()) {
                        binding.herdedto.setText("")
                        binding.worseedto.requestFocus()
                    }
                    if (binding.lifestyleedto.hasFocus()) {
                        binding.lifestyleedto.setText("")
                        binding.herdedto.requestFocus()
                    }
                    if (binding.cryedto.hasFocus()) {
                        binding.cryedto.setText("")
                        binding.lifestyleedto.requestFocus()
                    }
                    true
                } else {
                    false
                }
            }

        }

    }

    private fun startJump() {
        if (privacyPinPassword.isNotEmpty()) {
            if (privacyPinPassword == firstInput.array2String()) {
                startActivity(Intent(this, Reignedto::class.java))
                finish()
            } else {
                warmPassword()
            }
        } else {
            launcher.launch(
                Intent(this, Digressedto::class.java).apply {
                    addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
                    putExtra("FirstInput", firstInput.array2String())
                }
            ) {
                startActivity(Intent(this, Reignedto::class.java))
                finish()
            }
        }

    }

    private fun setInput() {
        binding.worseedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.tireedto.isSelected = hasFocus
        }
        binding.herdedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.advertiseedto.isSelected = hasFocus
        }
        binding.lifestyleedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.insureedto.isSelected = hasFocus
        }
        binding.cryedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.greenedto.isSelected = hasFocus
        }
        binding.worseedto.transformationMethod = Substanceedto()
        binding.herdedto.transformationMethod = Substanceedto()
        binding.lifestyleedto.transformationMethod = Substanceedto()
        binding.cryedto.transformationMethod = Substanceedto()

        binding.worseedto.requestFocus()

        binding.worseedto.addTextChangedListener {
            val text = it.toString()
            Dishedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[0] = text
                binding.herdedto.requestFocus()
            }
        }
        binding.herdedto.addTextChangedListener {
            val text = it.toString()
            Dishedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[1] = text
                binding.lifestyleedto.requestFocus()
            }
        }
        binding.lifestyleedto.addTextChangedListener {
            val text = it.toString()
            Dishedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[2] = text
                binding.cryedto.requestFocus()
            }
        }
        binding.cryedto.addTextChangedListener {
            val text = it.toString()
            Dishedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[3] = text
                startJump()
            }
        }
    }

    private fun warmPassword() {
        binding.tireedto.tag = true
        binding.tireedto.background = ContextCompat.getDrawable(this, R.drawable.cognacedto)
        binding.advertiseedto.background = ContextCompat.getDrawable(this, R.drawable.cognacedto)
        binding.insureedto.background = ContextCompat.getDrawable(this, R.drawable.cognacedto)
        binding.greenedto.background = ContextCompat.getDrawable(this, R.drawable.cognacedto)
        binding.worseedto.setTextColor(ContextCompat.getColor(this, R.color.dragedto))
        binding.herdedto.setTextColor(ContextCompat.getColor(this, R.color.dragedto))
        binding.lifestyleedto.setTextColor(ContextCompat.getColor(this, R.color.dragedto))
        binding.cryedto.setTextColor(ContextCompat.getColor(this, R.color.dragedto))

        val dialog = showPinReEnterDialog()
        lifecycleScope.launch {
            delay(2000)
            dialog.dismiss()
        }
    }

    private fun cancelWarmPassword() {
        if (binding.tireedto.tag == true) {
            binding.tireedto.background = ContextCompat.getDrawable(this, R.drawable.qualifyedto)
            binding.advertiseedto.background = ContextCompat.getDrawable(this, R.drawable.qualifyedto)
            binding.insureedto.background = ContextCompat.getDrawable(this, R.drawable.qualifyedto)
            binding.greenedto.background = ContextCompat.getDrawable(this, R.drawable.qualifyedto)
            binding.worseedto.setTextColor(ContextCompat.getColor(this, R.color.exemptionedto))
            binding.herdedto.setTextColor(ContextCompat.getColor(this, R.color.exemptionedto))
            binding.lifestyleedto.setTextColor(ContextCompat.getColor(this, R.color.exemptionedto))
            binding.cryedto.setTextColor(ContextCompat.getColor(this, R.color.exemptionedto))
            binding.tireedto.tag = false
        }

    }

}