package rallyedto.finishedto.exampleedto

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.AdvantageedtoBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import rallyedto.battleedto.acresedto.Fireplaceedto.array2String
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.farewelledto.Affectedto
import rallyedto.farewelledto.Technologyedto.showPinReEnterDialog
import rallyedto.policeedto.huntedto.Snuggleedto.privacyPinPassword
import rallyedto.sunedto.tumouredto.majorityedto.loseedto.Togetheredto
import rallyedto.talledto.blewedto.Islandedto
import rallyedto.talledto.blewedto.Musicaledto


class Constitutionedto : Lemonedto<AdvantageedtoBinding>() {

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

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


    override fun initView() {
        Islandedto.setStatusBarLightMode(this, true)
        Islandedto.setStatusBarColor(this, Color.TRANSPARENT)
        Togetheredto.showNativeAd(this,binding.jeweledto)
        firstInput = intent.extras?.getString("FirstInput") ?: "null"
        Musicaledto.logDebug(TAG, "firstInput=$firstInput")
    }

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

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

    private fun warmPassword() {
        binding.wineedto.tag = true
        binding.wineedto.background = ContextCompat.getDrawable(this, R.drawable.myedto)
        binding.opponentedto.background = ContextCompat.getDrawable(this, R.drawable.myedto)
        binding.lagoonedto.background = ContextCompat.getDrawable(this, R.drawable.myedto)
        binding.southernedto.background = ContextCompat.getDrawable(this, R.drawable.myedto)
        binding.excitingedto.setTextColor(ContextCompat.getColor(this, R.color.legislatureedto))
        binding.chordedto.setTextColor(ContextCompat.getColor(this, R.color.legislatureedto))
        binding.employeeedto.setTextColor(ContextCompat.getColor(this, R.color.legislatureedto))
        binding.suburbedto.setTextColor(ContextCompat.getColor(this, R.color.legislatureedto))

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

    private fun cancelWarmPassword() {
        if (binding.wineedto.tag == true) {
            binding.wineedto.background = ContextCompat.getDrawable(this, R.drawable.portraitedto)
            binding.opponentedto.background = ContextCompat.getDrawable(this, R.drawable.portraitedto)
            binding.lagoonedto.background = ContextCompat.getDrawable(this, R.drawable.portraitedto)
            binding.southernedto.background = ContextCompat.getDrawable(this, R.drawable.portraitedto)
            binding.excitingedto.setTextColor(ContextCompat.getColor(this, R.color.snapedto))
            binding.chordedto.setTextColor(ContextCompat.getColor(this, R.color.snapedto))
            binding.employeeedto.setTextColor(ContextCompat.getColor(this, R.color.snapedto))
            binding.suburbedto.setTextColor(ContextCompat.getColor(this, R.color.snapedto))
            binding.wineedto.tag = false
        }

    }

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

    }

    private fun setInput() {
        binding.excitingedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.wineedto.isSelected = hasFocus
        }
        binding.chordedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.opponentedto.isSelected = hasFocus
        }
        binding.employeeedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.lagoonedto.isSelected = hasFocus
        }
        binding.suburbedto.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.southernedto.isSelected = hasFocus
        }
        binding.excitingedto.transformationMethod = Affectedto()
        binding.chordedto.transformationMethod = Affectedto()
        binding.employeeedto.transformationMethod = Affectedto()
        binding.suburbedto.transformationMethod = Affectedto()

        binding.excitingedto.requestFocus()

        binding.excitingedto.addTextChangedListener {
            val text = it.toString()
            Musicaledto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[0] = text
                binding.chordedto.requestFocus()
            }
        }
        binding.chordedto.addTextChangedListener {
            val text = it.toString()
            Musicaledto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[1] = text
                binding.employeeedto.requestFocus()
            }
        }
        binding.employeeedto.addTextChangedListener {
            val text = it.toString()
            Musicaledto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[2] = text
                binding.suburbedto.requestFocus()
            }
        }
        binding.suburbedto.addTextChangedListener {
            val text = it.toString()
            Musicaledto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[3] = text
                if (firstInput == secondInput.array2String()) {
                    privacyPinPassword = firstInput
                    startJump()
                } else {
                    warmPassword()
                }
            }
        }
    }


    private fun setDeleteKey() {
        arrayOf(binding.excitingedto, binding.chordedto, binding.employeeedto, binding.suburbedto).forEach {
            it.setOnKeyListener { v, actionId, event ->

                if ((event.action == KeyEvent.ACTION_DOWN) && (event.keyCode == KeyEvent.KEYCODE_DEL)) {
                    if (binding.excitingedto.hasFocus()) {
                        binding.excitingedto.setText("")
                    }
                    if (binding.chordedto.hasFocus()) {
                        binding.chordedto.setText("")
                        binding.excitingedto.requestFocus()
                    }
                    if (binding.employeeedto.hasFocus()) {
                        binding.employeeedto.setText("")
                        binding.chordedto.requestFocus()
                    }
                    if (binding.suburbedto.hasFocus()) {
                        binding.suburbedto.setText("")
                        binding.employeeedto.requestFocus()
                    }
                    true
                } else {
                    false
                }
            }

        }
    }


}