package importantedto.appliededto.surgeonedto.promiseedto.respectableedto.prisonedto.comfortableedto

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.DestructionedtoBinding
import importantedto.afteredto.constitutionaledto.Constraintedto.array2String
import importantedto.afteredto.constitutionaledto.Murderedto
import importantedto.concreteedto.cameedto.Hateedto.privacyPinPassword
import importantedto.gallonedto.Transcendedto
import importantedto.gallonedto.Tumbleedto
import importantedto.maidedto.witnessedto.nationalismedto.Impulseedto
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showPinReEnterDialog
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class Knitedto : Murderedto<DestructionedtoBinding>() {

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

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


    override fun initView() {
        Transcendedto.setStatusBarLightMode(this, true)
        Transcendedto.setStatusBarColor(this, Color.TRANSPARENT)

    }

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

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

        setInput()

        setDeleteKey()


    }

    private fun setDeleteKey() {
        arrayOf(binding.pumpkinedto, binding.areaedto, binding.pushedto, binding.stadiumedto).forEach {
            it.setOnKeyListener { v, actionId, event ->

                if ((event.action == KeyEvent.ACTION_DOWN) && (event.keyCode == KeyEvent.KEYCODE_DEL)) {
                    if (binding.pumpkinedto.hasFocus()) {
                        binding.pumpkinedto.setText("")
                    }
                    if (binding.areaedto.hasFocus()) {
                        binding.areaedto.setText("")
                        binding.pumpkinedto.requestFocus()
                    }
                    if (binding.pushedto.hasFocus()) {
                        binding.pushedto.setText("")
                        binding.areaedto.requestFocus()
                    }
                    if (binding.stadiumedto.hasFocus()) {
                        binding.stadiumedto.setText("")
                        binding.pushedto.requestFocus()
                    }
                    true
                } else {
                    false
                }
            }

        }

    }

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

    }

    private fun setInput() {
        binding.pumpkinedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.exerciseedto.isSelected = hasFocus
        }
        binding.areaedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.feededto.isSelected = hasFocus
        }
        binding.pushedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.speciesedto.isSelected = hasFocus
        }
        binding.stadiumedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.lectureedto.isSelected = hasFocus
        }
        binding.pumpkinedto.transformationMethod = Impulseedto()
        binding.areaedto.transformationMethod = Impulseedto()
        binding.pushedto.transformationMethod = Impulseedto()
        binding.stadiumedto.transformationMethod = Impulseedto()

        binding.pumpkinedto.requestFocus()

        binding.pumpkinedto.addTextChangedListener {
            val text = it.toString()
            Tumbleedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[0] = text
                binding.areaedto.requestFocus()
            }
        }
        binding.areaedto.addTextChangedListener {
            val text = it.toString()
            Tumbleedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[1] = text
                binding.pushedto.requestFocus()
            }
        }
        binding.pushedto.addTextChangedListener {
            val text = it.toString()
            Tumbleedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[2] = text
                binding.stadiumedto.requestFocus()
            }
        }
        binding.stadiumedto.addTextChangedListener {
            val text = it.toString()
            Tumbleedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[3] = text
                startJump()
            }
        }
    }

    private fun warmPassword() {
        binding.exerciseedto.tag = true
        binding.exerciseedto.background = ContextCompat.getDrawable(this, R.drawable.overedto)
        binding.feededto.background = ContextCompat.getDrawable(this, R.drawable.overedto)
        binding.speciesedto.background = ContextCompat.getDrawable(this, R.drawable.overedto)
        binding.lectureedto.background = ContextCompat.getDrawable(this, R.drawable.overedto)
        binding.pumpkinedto.setTextColor(ContextCompat.getColor(this, R.color.menuedto))
        binding.areaedto.setTextColor(ContextCompat.getColor(this, R.color.menuedto))
        binding.pushedto.setTextColor(ContextCompat.getColor(this, R.color.menuedto))
        binding.stadiumedto.setTextColor(ContextCompat.getColor(this, R.color.menuedto))

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

    private fun cancelWarmPassword() {
        if (binding.exerciseedto.tag == true) {
            binding.exerciseedto.background = ContextCompat.getDrawable(this, R.drawable.metropolisedto)
            binding.feededto.background = ContextCompat.getDrawable(this, R.drawable.metropolisedto)
            binding.speciesedto.background = ContextCompat.getDrawable(this, R.drawable.metropolisedto)
            binding.lectureedto.background = ContextCompat.getDrawable(this, R.drawable.metropolisedto)
            binding.pumpkinedto.setTextColor(ContextCompat.getColor(this, R.color.fractionedto))
            binding.areaedto.setTextColor(ContextCompat.getColor(this, R.color.fractionedto))
            binding.pushedto.setTextColor(ContextCompat.getColor(this, R.color.fractionedto))
            binding.stadiumedto.setTextColor(ContextCompat.getColor(this, R.color.fractionedto))
            binding.exerciseedto.tag = false
        }

    }

}