package com.gh.gamecenter.teenagermode

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.common.view.CustomLinkMovementMethod
import com.gh.gamecenter.core.utils.ClickUtils
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.core.utils.SpanBuilder
import com.gh.gamecenter.databinding.FragmentTeenagerModeBinding

class TeenagerModeFragment : ToolbarFragment() {

    private var mBinding: FragmentTeenagerModeBinding? = null

    override fun getLayoutId(): Int = 0

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

    override fun onResume() {
        super.onResume()
        val bundle = Bundle()
        if (SPUtils.getBoolean(Constants.SP_TEENAGER_MODE, false)) {
            bundle.putString(TeenagerModeActivity.TYPE, TeenagerModeActivity.TYPE_DISABLE)
            showDisable()
        } else {
            bundle.putString(TeenagerModeActivity.TYPE, TeenagerModeActivity.TYPE_ENABLE)
            showEnable()
        }
        mBinding?.include?.normalToolbar?.setBackgroundColor(Color.TRANSPARENT)
        mBinding?.switchTv?.setOnClickListener {
            if (!ClickUtils.isFastDoubleClick(R.id.switchTv)) {
                val mContainerFragment =
                    requireActivity().supportFragmentManager.findFragmentByTag(PasswordSettingFragment::class.java.name)
                        ?: PasswordSettingFragment().with(bundle)
                requireActivity().supportFragmentManager.beginTransaction().run {
                    addToBackStack(null)
                    add(
                        R.id.placeholder,
                        mContainerFragment!!,
                        PasswordSettingFragment::class.java.name
                    ).commitAllowingStateLoss()
                }
            }
        }
    }

    private fun showEnable() {
        mBinding?.hintTv?.visibility = View.VISIBLE
        mBinding?.changePwdTv?.visibility = View.GONE
        mBinding?.titleTv?.text = "儿童/青少年模式"
        mBinding?.switchTv?.text = "开启儿童/青少年模式"
        val clickText = "《儿童/青少年使用须知》"
        val hintText = "更多信息可阅读 $clickText"
        mBinding?.hintTv?.text = SpanBuilder(hintText).click(
            requireContext(),
            hintText.length - clickText.length,
            hintText.length,
            com.gh.gamecenter.common.R.color.text_theme,
            false
        ) {
            startActivity(WebActivity.getWebIntent(requireContext(), "儿童/青少年使用须知", Constants.TEEN_MODE_RULE))
        }.build()
        mBinding?.hintTv?.movementMethod = CustomLinkMovementMethod.getInstance()
    }

    private fun showDisable() {
        mBinding?.hintTv?.visibility = View.GONE
        mBinding?.changePwdTv?.visibility = View.VISIBLE
        mBinding?.titleTv?.text = "儿童/青少年模式已开启"
        mBinding?.switchTv?.text = "关闭儿童/青少年模式"
        mBinding?.switchTv?.layoutParams = (mBinding?.switchTv?.layoutParams as ConstraintLayout.LayoutParams).apply {
            setMargins(
                0,
                0,
                0,
                68F.dip2px()
            )
        }
        mBinding?.changePwdTv?.setOnClickListener {
            val bundle = Bundle().apply { putString(TeenagerModeActivity.TYPE, TeenagerModeActivity.TYPE_CHANGE_PWD) }
            val mContainerFragment =
                requireActivity().supportFragmentManager.findFragmentByTag(PasswordSettingFragment::class.java.name)
                    ?: PasswordSettingFragment().with(bundle)
            requireActivity().supportFragmentManager.beginTransaction().run {
                addToBackStack(null)
                add(
                    R.id.placeholder,
                    mContainerFragment!!,
                    PasswordSettingFragment::class.java.name
                ).commitAllowingStateLoss()
            }
        }
    }

    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()))
            desTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(requireContext()))
            hintTv.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(requireContext()))
            changePwdTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(requireContext()))
            switchTv.background = com.gh.gamecenter.common.R.drawable.download_button_normal_style.toDrawable(requireContext())
        }
    }
}