package com.gh.gamecenter.teenagermode

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.view.VerifyEditText
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.MainActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.databinding.FragmentPasswordSettingBinding
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.utils.toDrawable
import com.lightgame.utils.AppManager

class PasswordSettingFragment : ToolbarFragment() {

    private var mBinding: FragmentPasswordSettingBinding? = null
    private var mViewModel: TeenagerModeViewModel? = null

    private var mPassword = ""
    private var mLastPassword = ""

    override fun getLayoutId(): Int = 0

    override fun getInflatedLayout() =
        FragmentPasswordSettingBinding.inflate(layoutInflater).apply { mBinding = this }.root

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewModel = ViewModelProvider(this).get(TeenagerModeViewModel::class.java)
        mBinding?.include?.normalToolbar?.setBackgroundColor(Color.TRANSPARENT)
        mBinding?.resetPwdTv?.setOnClickListener {
            startActivity(WebActivity.getWebIntent(requireContext(), "找回密码", Constants.TEEN_MODE_RESET_PASSWORD))
        }
        when (requireArguments().getString(TeenagerModeActivity.TYPE)) {
            TeenagerModeActivity.TYPE_ENABLE -> showEnable()
            TeenagerModeActivity.TYPE_DISABLE -> showDisable()
            TeenagerModeActivity.TYPE_CHANGE_PWD -> showChangePwd()
        }
    }

    private fun showEnable() {
        mBinding?.titleTv?.text = "设置密码"
        mBinding?.hintTv?.text = "启动儿童/青少年模式，需要先设置独立密码"
        mBinding?.resetPwdTv?.visibility = View.GONE
        mBinding?.verifyEt?.run {
            requestFocus()
            addInputListener(object : VerifyEditText.InputListener {
                override fun onChange(content: String) {
                    mBinding?.nextTv?.alpha = if (content.length < 4) 0.4F else 1F
                    if (content.length < 4) mBinding?.nextTv?.setOnClickListener(null)
                }
            })
            addInputCompleteListener(object : VerifyEditText.InputCompleteListener {
                override fun onComplete(content: String) {
                    mBinding?.nextTv?.alpha = 1F
                    mBinding?.nextTv?.setOnClickListener {
                        mPassword = content
                        clearContent()
                        mBinding?.titleTv?.text = "确认密码"
                        mBinding?.hintTv?.text = "再次输入密码"
                        mBinding?.nextTv?.alpha = 0.4F
                        it.setOnClickListener(null)
                        addInputCompleteListener(object : VerifyEditText.InputCompleteListener {
                            override fun onComplete(content: String) {
                                mBinding?.nextTv?.alpha = 1F
                                it.setOnClickListener {
                                    if (content == mPassword) {
                                        mViewModel?.postTeenModeLock(mPassword) {
                                            SPUtils.setBoolean(Constants.SP_TEENAGER_MODE, true)
                                            toast("儿童/青少年模式已开启")
                                            directToSplash()
                                        }
                                    } else {
                                        mBinding?.hintTv?.text = "与第一次输入的密码不一致"
                                        mBinding?.hintTv?.setTextColor(com.gh.gamecenter.common.R.color.secondary_red.toColor(requireContext()))
                                        clearContent()
                                    }
                                }
                            }
                        })
                    }
                }
            })
        }
    }

    private fun showDisable() {
        mBinding?.titleTv?.text = "关闭儿童/青少年模式"
        mBinding?.hintTv?.text = "请输入密码确认"
        mBinding?.resetPwdTv?.visibility = View.VISIBLE
        mBinding?.verifyEt?.run {
            requestFocus()
            addInputListener(object : VerifyEditText.InputListener {
                override fun onChange(content: String) {
                    mBinding?.nextTv?.alpha = if (content.length < 4) 0.4F else 1F
                    if (content.length < 4) mBinding?.nextTv?.setOnClickListener(null)
                }
            })
            addInputCompleteListener(object : VerifyEditText.InputCompleteListener {
                override fun onComplete(content: String) {
                    mBinding?.nextTv?.alpha = 1F
                    mBinding?.nextTv?.setOnClickListener {
                        mViewModel?.postTeenModeLogout(content, {
                            SPUtils.setBoolean(Constants.SP_TEENAGER_MODE, false)
                            toast("儿童/青少年模式已关闭")
                            directToSplash()
                        }, {
                            clearContent()
                            mBinding?.hintTv?.text = "密码错误请重新输入"
                            mBinding?.hintTv?.setTextColor(com.gh.gamecenter.common.R.color.secondary_red.toColor(requireContext()))
                        })
                    }
                }
            })
        }
    }

    private fun showChangePwd() {
        mBinding?.titleTv?.text = "修改密码"
        mBinding?.hintTv?.text = "请输入当前密码"
        mBinding?.resetPwdTv?.visibility = View.VISIBLE
        mBinding?.verifyEt?.run {
            requestFocus()
            addInputListener(object : VerifyEditText.InputListener {
                override fun onChange(content: String) {
                    mBinding?.nextTv?.alpha = if (content.length < 4) 0.4F else 1F
                    if (content.length < 4) mBinding?.nextTv?.setOnClickListener(null)
                }
            })
            addInputCompleteListener(object : VerifyEditText.InputCompleteListener {
                override fun onComplete(content: String) {
                    mBinding?.nextTv?.alpha = 1F
                    mBinding?.nextTv?.setOnClickListener {
                        mViewModel?.postTeenModeUnlock(content, {
                            mLastPassword = content
                            mBinding?.resetPwdTv?.visibility = View.GONE
                            clearContent()
                            mBinding?.titleTv?.text = "请输入新密码"
                            mBinding?.hintTv?.text = "设置新的独立密码"
                            mBinding?.hintTv?.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(requireContext()))
                            addInputCompleteListener(object : VerifyEditText.InputCompleteListener {
                                override fun onComplete(content: String) {
                                    mBinding?.nextTv?.alpha = 1F
                                    mBinding?.nextTv?.setOnClickListener {
                                        mPassword = content
                                        clearContent()
                                        mBinding?.titleTv?.text = "确认密码"
                                        mBinding?.hintTv?.text = "再次输入密码"
                                        mBinding?.nextTv?.alpha = 0.4F
                                        it.setOnClickListener(null)
                                        addInputCompleteListener(object : VerifyEditText.InputCompleteListener {
                                            override fun onComplete(content: String) {
                                                mBinding?.nextTv?.alpha = 1F
                                                it.setOnClickListener {
                                                    if (content == mPassword) {
                                                        mViewModel?.putTeenModePwd(mLastPassword, mPassword, {
                                                            toast("设置成功")
                                                            requireActivity().onBackPressed()
                                                        }, {
                                                            toast("网络异常")
                                                        })
                                                    } else {
                                                        mBinding?.hintTv?.text = "与第一次输入的密码不一致"
                                                        mBinding?.hintTv?.setTextColor(
                                                            com.gh.gamecenter.common.R.color.secondary_red.toColor(
                                                                requireContext()
                                                            )
                                                        )
                                                        clearContent()
                                                    }
                                                }
                                            }
                                        })
                                    }
                                }
                            })
                        }, {
                            clearContent()
                            mBinding?.hintTv?.text = "密码错误请重新输入"
                            mBinding?.hintTv?.setTextColor(com.gh.gamecenter.common.R.color.secondary_red.toColor(requireContext()))
                        })
                    }
                }
            })
        }
    }

    private fun directToSplash() {
        AppManager.getInstance().finishActivity(AppManager.getInstance().findActivity(MainActivity::class.java))
        AppManager.getInstance().finishActivity(AppManager.getInstance().findActivity(GameDetailActivity::class.java))
        startActivity(MainActivity.getMainIntent(requireActivity()).apply {
            putExtra(MainActivity.SHOW_AD, true)
        })
        requireActivity().finish()
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mBinding?.run {
            root.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
            include.backBtn.setImageResource(com.gh.gamecenter.common.R.drawable.ic_bar_back)
            titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(requireContext()))
            hintTv.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(requireContext()))
            resetPwdTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(requireContext()))
            nextTv.background = com.gh.gamecenter.common.R.drawable.download_button_normal_style.toDrawable(requireContext())
            verifyEt.setTextViewStyle(
                R.drawable.bg_verify_password.toDrawable(requireContext()),
                R.drawable.bg_verify_password_select.toDrawable(requireContext()),
                com.gh.gamecenter.common.R.color.text_primary.toColor(requireContext())
            )
        }
    }
}