package com.gh.gamecenter.setting.view.security

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.eventbus.EBReuse
import com.gh.gamecenter.common.utils.EnvHelper
import com.gh.gamecenter.core.provider.IDirectProvider
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.setting.R
import com.gh.gamecenter.setting.databinding.FragmentSecurityBinding
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class SecurityFragment : ToolbarFragment() {

    private val mBinding by lazy { FragmentSecurityBinding.inflate(layoutInflater) }

    override fun getLayoutId() = 0

    override fun getInflatedLayout() = mBinding.root

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        UserManager.getInstance().userInfoEntity?.run {
            mBinding.registerWayItem.contentTv.text = when (registerType) {
                "qq" -> "QQ"
                "wechat" -> "微信"
                "weibo" -> "微博"
                "douyin" -> "抖音"
//                "mobile" -> "手机"
                else -> "手机"
            }

            mBinding.bindPhoneItem.textMoreTv.text = if (loginMobile.isNullOrEmpty()) "立即绑定" else loginMobile
        }
    }

    private fun initView() {
        val directUtils = ARouter.getInstance().build(RouteConsts.provider.directUtils).navigation() as? IDirectProvider
        mBinding.securityContact.setOnClickListener {
            directUtils?.directToQqConversation(requireContext(), "800177318")
        }
        if (arguments?.getString(DISPLAY_TYPE) == DisplayType.DEFAULT.value) {
            mBinding.registerWayItem.run {
                titleTv.text = getString(R.string.register_way_key)
                contentTv.visibility = View.VISIBLE
            }
        } else {
            mBinding.safeCenterItem.root.visibility = View.GONE
            mBinding.bindPhoneItem.root.visibility = View.GONE
            mBinding.divider.root.visibility = View.GONE
            mBinding.registerWayItem.run {
                titleTv.text = "注销帐号"
                textMoreTv.visibility = View.VISIBLE
                root.setOnClickListener {
                    if (EnvHelper.isDevEnv) {
                        directUtils?.directToWebView(requireContext(), Constants.LOGOUT_ADDRESS_DEV, mEntrance)
                    } else {
                        directUtils?.directToWebView(requireContext(), Constants.LOGOUT_ADDRESS, mEntrance)
                    }
                }
            }

            setNavigationTitle("账户安全中心")
        }
        mBinding.bindPhoneItem.run {
            titleTv.text = getString(R.string.bind_phone_key)
            textMoreTv.visibility = View.VISIBLE
            root.setOnClickListener {
                ARouter.getInstance().build(RouteConsts.activity.bindPhoneActivity)
                    .withBoolean(EntranceConsts.KEY_FROM_LOGIN, false)
                    .withBoolean(EntranceConsts.KEY_CHANGE_PHONE, !UserManager.getInstance().userInfoEntity?.loginMobile.isNullOrEmpty())
                    .navigation(requireActivity(), INSERT_MOBILE_CODE)
            }
        }
        mBinding.safeCenterItem.run {
            titleTv.text = getString(R.string.account_security_center)
            textMoreTv.visibility = View.VISIBLE
            root.setOnClickListener {
                requireContext().startActivity(SecurityActivity.getIntent(requireContext(), mEntrance, true))
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == INSERT_MOBILE_CODE) {
            mBinding.bindPhoneItem.textMoreTv.text =
                if (UserManager.getInstance().userInfoEntity?.loginMobile.isNullOrEmpty()) {
                    "立即绑定"
                } else {
                    UserManager.getInstance().userInfoEntity?.loginMobile
                }
        }
    }

    // 登录事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(reuse: EBReuse) {
        if (reuse.type == Constants.LOGOUT_TAG) {
            activity?.finish()
        }
    }

    enum class DisplayType(var value: String) {
        DEFAULT("default"),

        LOGOUT("logout")
    }

    companion object {
        const val INSERT_MOBILE_CODE = 411

        const val DISPLAY_TYPE = "display_type"
    }
}