package rallyedto.finishedto.exampleedto

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.CampaignedtoBinding
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 Designedto : Lemonedto<CampaignedtoBinding>() {

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

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


    override fun initView() {
        Islandedto.setStatusBarLightMode(this, true)
        Islandedto.setStatusBarColor(this, Color.TRANSPARENT)
        Togetheredto.showNativeAd(this,binding.jeweledto)
    }

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

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

        setInput()

        setDeleteKey()


    }

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

        }

    }

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

    }

    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()) {
                firstInput[0] = text
                binding.chordedto.requestFocus()
            }
        }
        binding.chordedto.addTextChangedListener {
            val text = it.toString()
            Musicaledto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[1] = text
                binding.employeeedto.requestFocus()
            }
        }
        binding.employeeedto.addTextChangedListener {
            val text = it.toString()
            Musicaledto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[2] = text
                binding.suburbedto.requestFocus()
            }
        }
        binding.suburbedto.addTextChangedListener {
            val text = it.toString()
            Musicaledto.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                firstInput[3] = text
                startJump()
            }
        }
    }

    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
        }

    }

}