package com.halo.assistant.fragment

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import com.gh.download.server.BrowserInstallHelper
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.toColor
import com.gh.gamecenter.common.utils.updateStatusBarColor
import com.gh.gamecenter.common.view.CustomLinkMovementMethod
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.core.utils.SpanBuilder
import com.gh.gamecenter.databinding.FragmentSwitchInstallMethodBinding
import java.util.*

/**
 * 切换安装方式
 */
class SwitchInstallMethodFragment : ToolbarFragment() {

    private var mBinding: FragmentSwitchInstallMethodBinding? = null
    private var mUseBrowserToInstall = false

    override fun getLayoutId() = 0

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        requireActivity().updateStatusBarColor(R.color.background_white, R.color.background_white)

        initView()
        changeSwitch(SPUtils.getBoolean(Constants.SP_USE_BROWSER_TO_INSTALL))

        mBinding?.toolbar?.normalTitle?.text = "切换安装方式"
        mBinding?.toolbar?.normalToolbar?.setNavigationOnClickListener { requireActivity().finish() }

        val manufacturer = Build.MANUFACTURER.toUpperCase(Locale.CHINA)

        var defaultHintText: CharSequence
        var browserHintText: CharSequence =
            "1.光环助手下载应用后，通过跳转默认浏览器安装已下载的应用（不会重复消耗流量），即可完成免密码安装\n2.内存较大的游戏在浏览器解压时间较长，需要等待片刻，您也可以通过浏览器的“下载记录”查看解压进度"

        if (manufacturer == "OPPO" || manufacturer == "VIVO") {
            val clickText: CharSequence = "前往手机设置-指纹、面部与密码>"
            val endText =
                if (manufacturer == "OPPO") " 设置锁屏密码，再点击[添加指纹]录入指纹，将指纹用于[安装验证]；或者点击[添加面部]录入面部，将面部用于[安装验证]，即可实现快速安装" else "开启锁屏密码，再点击[添加指纹]录入指纹，将指纹用于[解锁]，即可实现快速安装"
            defaultHintText =
                "1.直接使用光环助手下载应用，但${manufacturer}手机自身安全性检测，下载后需要进行验证账户密码、指纹等解锁后安装\n2.您可以 $clickText$endText"
            defaultHintText = SpanBuilder(defaultHintText)
                .click(
                    requireContext(),
                    defaultHintText.length - clickText.length - endText.length,
                    defaultHintText.length - endText.length,
                    R.color.theme_font,
                    true
                ) {
                    requireContext().startActivity(Intent(Settings.ACTION_SETTINGS))
                }
                .build()

            val clickableText: CharSequence = "可查看使用教程>"
            browserHintText = "$browserHintText\n\n以上未能解决安装需要密码的，$clickableText"
            browserHintText = SpanBuilder(browserHintText)
                .click(
                    requireContext(),
                    browserHintText.length - clickableText.length,
                    browserHintText.length,
                    R.color.theme_font,
                    true
                ) {
                    val url = SPUtils.getString(
                        Constants.SP_BROWSER_HINT_URL,
                        if (manufacturer == "OPPO") Constants.DEFAULT_OPPO_BROWSER_HINT_URL else Constants.DEFAULT_VIVO_BROWSER_HINT_URL
                    )
                    requireContext().startActivity(WebActivity.getQAIntent(requireContext(), url, "安装问题", false, 1))
                }
                .build()
        } else {
            defaultHintText = "直接使用光环助手下载应用，但部分手机可能存在无法安装的问题"
        }

        mBinding?.defaultInstallDes?.root?.text = defaultHintText
        mBinding?.browserInstallDes?.root?.text = browserHintText
        mBinding?.defaultInstallDes?.root?.movementMethod = CustomLinkMovementMethod.getInstance()
        mBinding?.browserInstallDes?.root?.movementMethod = CustomLinkMovementMethod.getInstance()
    }

    private fun initView() {
        mBinding?.defaultInstallItem?.run {
            titleTv.text = "助手安装"
            selectedIv.visibility = View.VISIBLE
            root.setOnClickListener {
                changeSwitch(false)
            }
        }
        mBinding?.browserInstallItem?.run {
            titleTv.text = "浏览器安装"
            selectedIv.visibility = View.VISIBLE
            root.setOnClickListener {
                changeSwitch(true)
            }
        }
    }

    private fun changeSwitch(useBrowserToInstall: Boolean) {
        mUseBrowserToInstall = useBrowserToInstall
        if (useBrowserToInstall) {
            mBinding?.defaultInstallItem?.selectedIv?.setImageResource(R.drawable.ic_selector_default)
            mBinding?.browserInstallItem?.selectedIv?.setImageResource(R.drawable.ic_selector_selected)
        } else {
            mBinding?.defaultInstallItem?.selectedIv?.setImageResource(R.drawable.ic_selector_selected)
            mBinding?.browserInstallItem?.selectedIv?.setImageResource(R.drawable.ic_selector_default)
        }

        if (SPUtils.getBoolean(Constants.SP_USE_BROWSER_TO_INSTALL) != useBrowserToInstall) {
            mBinding?.confirmBtn?.isEnabled = true
            mBinding?.confirmBtn?.alpha = 1F
        } else {
            mBinding?.confirmBtn?.isEnabled = false
            mBinding?.confirmBtn?.alpha = 0.4F
        }

        mBinding?.confirmBtn?.setOnClickListener {
            SPUtils.setBoolean(Constants.SP_USE_BROWSER_TO_INSTALL, useBrowserToInstall)
            requireActivity().finish()

            if (useBrowserToInstall) {
                BrowserInstallHelper.logOrdinaryBrowserEvent(BrowserInstallHelper.Type.SWITCH_INSTALL_SETTING_WEB)
            } else {
                BrowserInstallHelper.logOrdinaryBrowserEvent(BrowserInstallHelper.Type.SWITCH_INSTALL_SETTING_APP)
            }
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        requireActivity().updateStatusBarColor(R.color.background_white, R.color.background_white)
        mBinding?.toolbar?.run {
            normalToolbar.setBackgroundColor(R.color.background_white.toColor(requireContext()))
            backBtn.setImageResource(R.drawable.ic_bar_back)
            normalTitle.setTextColor(R.color.text_title.toColor(requireContext()))
        }
        changeSwitch(mUseBrowserToInstall)
    }
}