package com.base.datarecovery.activity.privacyspace

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.bean.ConstObject.privacyPinPassword
import com.base.datarecovery.databinding.ActivityPrivacyPinTwoBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.KotlinExt.array2String
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.utils.LogEx
import com.base.datarecovery.view.AsteriskPasswordTransformationMethod
import com.base.datarecovery.view.DialogViews.showPinReEnterDialog
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class PrivacyPinTwoActivity : BaseActivity<ActivityPrivacyPinTwoBinding>() {

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

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


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        firstInput = intent.extras?.getString("FirstInput") ?: "null"
        LogEx.logDebug(TAG, "firstInput=$firstInput")
    }

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

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

    private fun warmPassword() {
        binding.fl1.tag = true
        binding.fl1.background = ContextCompat.getDrawable(this, R.drawable.bg_border_pin_warm)
        binding.fl2.background = ContextCompat.getDrawable(this, R.drawable.bg_border_pin_warm)
        binding.fl3.background = ContextCompat.getDrawable(this, R.drawable.bg_border_pin_warm)
        binding.fl4.background = ContextCompat.getDrawable(this, R.drawable.bg_border_pin_warm)
        binding.edit1.setTextColor(ContextCompat.getColor(this, R.color.color_fa020b))
        binding.edit2.setTextColor(ContextCompat.getColor(this, R.color.color_fa020b))
        binding.edit3.setTextColor(ContextCompat.getColor(this, R.color.color_fa020b))
        binding.edit4.setTextColor(ContextCompat.getColor(this, R.color.color_fa020b))

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

    private fun cancelWarmPassword() {
        if (binding.fl1.tag == true) {
            binding.fl1.background = ContextCompat.getDrawable(this, R.drawable.bg_border_pin)
            binding.fl2.background = ContextCompat.getDrawable(this, R.drawable.bg_border_pin)
            binding.fl3.background = ContextCompat.getDrawable(this, R.drawable.bg_border_pin)
            binding.fl4.background = ContextCompat.getDrawable(this, R.drawable.bg_border_pin)
            binding.edit1.setTextColor(ContextCompat.getColor(this, R.color.black))
            binding.edit2.setTextColor(ContextCompat.getColor(this, R.color.black))
            binding.edit3.setTextColor(ContextCompat.getColor(this, R.color.black))
            binding.edit4.setTextColor(ContextCompat.getColor(this, R.color.black))
            binding.fl1.tag = false
        }

    }

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

    }

    private fun setInput() {
        binding.edit1.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.fl1.isSelected = hasFocus
        }
        binding.edit2.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.fl2.isSelected = hasFocus
        }
        binding.edit3.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.fl3.isSelected = hasFocus
        }
        binding.edit4.setOnFocusChangeListener { v, hasFocus ->
            cancelWarmPassword()
            binding.fl4.isSelected = hasFocus
        }
        binding.edit1.transformationMethod = AsteriskPasswordTransformationMethod()
        binding.edit2.transformationMethod = AsteriskPasswordTransformationMethod()
        binding.edit3.transformationMethod = AsteriskPasswordTransformationMethod()
        binding.edit4.transformationMethod = AsteriskPasswordTransformationMethod()

        binding.edit1.requestFocus()

        binding.edit1.addTextChangedListener {
            val text = it.toString()
            LogEx.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[0] = text
                binding.edit2.requestFocus()
            }
        }
        binding.edit2.addTextChangedListener {
            val text = it.toString()
            LogEx.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[1] = text
                binding.edit3.requestFocus()
            }
        }
        binding.edit3.addTextChangedListener {
            val text = it.toString()
            LogEx.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[2] = text
                binding.edit4.requestFocus()
            }
        }
        binding.edit4.addTextChangedListener {
            val text = it.toString()
            LogEx.logDebug(TAG, "text=$text")
            if (text.isNotEmpty()) {
                secondInput[3] = text
                if (firstInput == secondInput.array2String()) {
                    privacyPinPassword = firstInput
                    startJump()
                } else {
                    warmPassword()
                }
            }
        }
    }


    private fun setDeleteKey() {
        arrayOf(binding.edit1, binding.edit2, binding.edit3, binding.edit4).forEach {
            it.setOnKeyListener { v, actionId, event ->

                if ((event.action == KeyEvent.ACTION_DOWN) && (event.keyCode == KeyEvent.KEYCODE_DEL)) {
                    if (binding.edit1.hasFocus()) {
                        binding.edit1.setText("")
                    }
                    if (binding.edit2.hasFocus()) {
                        binding.edit2.setText("")
                        binding.edit1.requestFocus()
                    }
                    if (binding.edit3.hasFocus()) {
                        binding.edit3.setText("")
                        binding.edit2.requestFocus()
                    }
                    if (binding.edit4.hasFocus()) {
                        binding.edit4.setText("")
                        binding.edit3.requestFocus()
                    }
                    true
                } else {
                    false
                }
            }

        }
    }


}