package com.base.filerecoveryrecyclebin.activity.privacyspace

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

class PrivacyPinOneActivity : BaseActivity<ActivityPrivacyPinOneBinding>() {

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

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


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

    }

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

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

        setInput()

        setDeleteKey()


    }

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

        }

    }

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

    }

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

    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
        }

    }

}