package com.gh.gamecenter.setting.view

import android.os.Bundle
import android.view.View
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.utils.setSwitchAnimation
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.setting.R
import com.gh.gamecenter.setting.databinding.FragmentVideoSettingBinding

class VideoSettingFragment : ToolbarFragment() {

    private var mBinding: FragmentVideoSettingBinding? = null
    private var mContentVideoOptionSelected = ""
    private var mHomeOrDetailVideoOptionSelected = ""

    override fun getLayoutId() = 0

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
        initStatus()
    }

    private fun initView() {
        mBinding?.contentVideoTitle?.root?.text = "内容视频"
        mBinding?.homeOrDetailVideoTitle?.root?.text = "首页/游戏详情页视频"
        mBinding?.contentVideoOptionAllItem?.run {
            titleTv.text = getString(R.string.all_network_auto_play)
            selectedIv.visibility = View.VISIBLE
            root.setOnClickListener {
                if (!isCurrentContentStatus(Constants.VIDEO_OPTION_ALL)) {
                    setContentVideoOption(Constants.VIDEO_OPTION_ALL)
                    SPUtils.setString(SP_CONTENT_VIDEO_OPTION, Constants.VIDEO_OPTION_ALL)
                }
            }
        }
        mBinding?.contentVideoOptionWifiItem?.run {
            titleTv.text = getString(R.string.only_wifi_auto_play)
            selectedIv.visibility = View.VISIBLE
            root.setOnClickListener {
                if (!isCurrentContentStatus(Constants.VIDEO_OPTION_WIFI)) {
                    setContentVideoOption(Constants.VIDEO_OPTION_WIFI)
                    SPUtils.setString(SP_CONTENT_VIDEO_OPTION, Constants.VIDEO_OPTION_WIFI)
                }
            }
        }
        mBinding?.contentVideoOptionCloseItem?.run {
            titleTv.text = getString(R.string.close_auto_play)
            selectedIv.visibility = View.VISIBLE
            root.setOnClickListener {
                if (!isCurrentContentStatus(Constants.VIDEO_OPTION_CLOSE)) {
                    setContentVideoOption(Constants.VIDEO_OPTION_CLOSE)
                    SPUtils.setString(SP_CONTENT_VIDEO_OPTION, Constants.VIDEO_OPTION_CLOSE)
                }
            }
        }
        mBinding?.homeOrDetailVideoOptionAllItem?.run {
            titleTv.text = getString(R.string.all_network_auto_play)
            selectedIv.visibility = View.VISIBLE
            root.setOnClickListener {
                if (!isCurrentHomeOrDetailStatus(Constants.VIDEO_OPTION_ALL)) {
                    setHomeOrDetailVideoOption(Constants.VIDEO_OPTION_ALL)
                    SPUtils.setString(SP_HOME_OR_DETAIL_VIDEO_OPTION, Constants.VIDEO_OPTION_ALL)
                }
            }
        }
        mBinding?.homeOrDetailVideoOptionWifiItem?.run {
            titleTv.text = getString(R.string.only_wifi_auto_play)
            selectedIv.visibility = View.VISIBLE
            root.setOnClickListener {
                if (!isCurrentHomeOrDetailStatus(Constants.VIDEO_OPTION_WIFI)) {
                    setHomeOrDetailVideoOption(Constants.VIDEO_OPTION_WIFI)
                    SPUtils.setString(SP_HOME_OR_DETAIL_VIDEO_OPTION, Constants.VIDEO_OPTION_WIFI)
                }
            }
        }
        mBinding?.homeOrDetailVideoOptionCloseItem?.run {
            titleTv.text = getString(R.string.close_auto_play)
            selectedIv.visibility = View.VISIBLE
            root.setOnClickListener {
                if (!isCurrentHomeOrDetailStatus(Constants.VIDEO_OPTION_CLOSE)) {
                    setHomeOrDetailVideoOption(Constants.VIDEO_OPTION_CLOSE)
                    SPUtils.setString(SP_HOME_OR_DETAIL_VIDEO_OPTION, Constants.VIDEO_OPTION_CLOSE)
                }
            }
        }
        mBinding?.muteItem?.run {
            titleTv.text = getString(R.string.setting_mute)
            tipsTv.text = getString(R.string.setting_mute_hint)
            tipsTv.visibility = View.VISIBLE
            switchLottie.visibility = View.VISIBLE
            root.setOnClickListener {
                if (switchLottie.isAnimating) return@setOnClickListener
                val status = SPUtils.getBoolean(SP_VIDEO_PLAY_MUTE, true)
                switchLottie.setSwitchAnimation(status)
                switchLottie.playAnimation()
                SPUtils.setBoolean(SP_VIDEO_PLAY_MUTE, !status)
            }
        }
    }

    private fun initStatus() {
        mContentVideoOptionSelected =
            SPUtils.getString(SP_CONTENT_VIDEO_OPTION, Constants.VIDEO_OPTION_WIFI) ?: Constants.VIDEO_OPTION_WIFI
        mHomeOrDetailVideoOptionSelected =
            SPUtils.getString(SP_HOME_OR_DETAIL_VIDEO_OPTION, Constants.VIDEO_OPTION_WIFI)
                ?: Constants.VIDEO_OPTION_WIFI
        setContentVideoOption(mContentVideoOptionSelected)
        setHomeOrDetailVideoOption(mHomeOrDetailVideoOptionSelected)
        mBinding?.muteItem?.switchLottie?.setSwitchAnimation(SPUtils.getBoolean(SP_VIDEO_PLAY_MUTE, true))
    }

    private fun setContentVideoOption(status: String) {
        mBinding?.run {
            when (status) {
                Constants.VIDEO_OPTION_ALL -> {
                    contentVideoOptionAllItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_selected)
                    contentVideoOptionWifiItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                    contentVideoOptionCloseItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                }

                Constants.VIDEO_OPTION_WIFI -> {
                    contentVideoOptionAllItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                    contentVideoOptionWifiItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_selected)
                    contentVideoOptionCloseItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                }

                Constants.VIDEO_OPTION_CLOSE -> {
                    contentVideoOptionAllItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                    contentVideoOptionWifiItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                    contentVideoOptionCloseItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_selected)
                }
            }
        }
    }

    private fun setHomeOrDetailVideoOption(status: String) {
        mBinding?.run {
            when (status) {
                Constants.VIDEO_OPTION_ALL -> {
                    homeOrDetailVideoOptionAllItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_selected)
                    homeOrDetailVideoOptionWifiItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                    homeOrDetailVideoOptionCloseItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                }

                Constants.VIDEO_OPTION_WIFI -> {
                    homeOrDetailVideoOptionAllItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                    homeOrDetailVideoOptionWifiItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_selected)
                    homeOrDetailVideoOptionCloseItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                }

                Constants.VIDEO_OPTION_CLOSE -> {
                    homeOrDetailVideoOptionAllItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                    homeOrDetailVideoOptionWifiItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_default)
                    homeOrDetailVideoOptionCloseItem.selectedIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_selector_selected)
                }
            }
        }
    }

    private fun isCurrentContentStatus(status: String): Boolean {
        return status == SPUtils.getString(
            SP_CONTENT_VIDEO_OPTION,
            Constants.VIDEO_OPTION_WIFI
        ) ?: Constants.VIDEO_OPTION_WIFI
    }

    private fun isCurrentHomeOrDetailStatus(status: String): Boolean {
        return status == SPUtils.getString(
            SP_HOME_OR_DETAIL_VIDEO_OPTION,
            Constants.VIDEO_OPTION_WIFI
        ) ?: Constants.VIDEO_OPTION_WIFI
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mBinding?.root?.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_background.toColor(requireContext()))
        mBinding?.muteItem?.switchLottie?.setSwitchAnimation(SPUtils.getBoolean(SP_VIDEO_PLAY_MUTE, true))
        setContentVideoOption(mContentVideoOptionSelected)
        setHomeOrDetailVideoOption(mHomeOrDetailVideoOptionSelected)
    }

    companion object {
        const val SP_CONTENT_VIDEO_OPTION = Constants.SP_CONTENT_VIDEO_OPTION
        const val SP_HOME_OR_DETAIL_VIDEO_OPTION = Constants.SP_HOME_OR_DETAIL_VIDEO_OPTION
        const val SP_VIDEO_PLAY_MUTE = Constants.SP_VIDEO_PLAY_MUTE
    }
}