package rowedto.jockeyedto.sheetedto.atticedto.harboredto

import android.graphics.Color
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.activity.addCallback
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.R
import com.base.datarecovery.databinding.QuotaedtoBinding
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 Digressedto : Crossingedto<QuotaedtoBinding>() {

    private var firstInput = "null"
    private val secondInput = arrayOf("", "", "", "")
    private val TAG = "PrivacyPinTwoActivity"

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


    override fun initView() {
        Spareedto.setStatusBarLightMode(this, true)
        Spareedto.setStatusBarColor(this, Color.TRANSPARENT)
        Scandaledto.admobMaxShowNativeAd(this,binding.tooledto,1)
        firstInput = intent.extras?.getString("FirstInput") ?: "null"
        Dishedto.logDebug(TAG, "firstInput=$firstInput")
    }

    override fun initListener() {
        binding.conditionedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            finish()
        }
        setInput()
        setDeleteKey()
    }

    private fun hideInput() {
        val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(binding.cryedto.windowToken, 0)
        binding.cryedto.clearFocus()
    }

    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
        }

    }

    private fun startJump() {
        binding.recklessedto.visibility = View.GONE
        binding.fishedto.visibility = View.VISIBLE
        hideInput()
        onBackPressedDispatcher.addCallback {
            setResult(0, intent)
            finish()
        }
        binding.conditionedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

    }

    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()) {
                secondInput[0] = text
                binding.herdedto.requestFocus()
            }
        }
        binding.herdedto.addTextChangedListener {
            val text = it.toString()
            Dishedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[1] = text
                binding.lifestyleedto.requestFocus()
            }
        }
        binding.lifestyleedto.addTextChangedListener {
            val text = it.toString()
            Dishedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[2] = text
                binding.cryedto.requestFocus()
            }
        }
        binding.cryedto.addTextChangedListener {
            val text = it.toString()
            Dishedto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[3] = text
                if (firstInput == secondInput.array2String()) {
                    privacyPinPassword = firstInput
                    startJump()
                } else {
                    warmPassword()
                }
            }
        }
    }


    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
                }
            }

        }
    }


}