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

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.setting.R
import com.gh.gamecenter.setting.databinding.FragmentBindPhoneConflictBinding
import com.gh.gamecenter.setting.entity.ConflictUserEntity

class BindPhoneConflictFragment : ToolbarFragment() {

    private lateinit var mConflictPhone: String
    private lateinit var mConflictUser: ConflictUserEntity
    private lateinit var mPopupWindow: PopupWindow
    private lateinit var mContentView: View
    private val mBinding by lazy { FragmentBindPhoneConflictBinding.inflate(layoutInflater) }

    override fun getLayoutId() = 0

    override fun getInflatedLayout() = mBinding.root

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mConflictPhone = requireArguments().getString(EntranceConsts.KEY_CONFLICT_PHONE)!!
        mConflictUser = requireArguments().getParcelable(EntranceConsts.KEY_CONFLICT_USER)!!
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        arrayListOf(
            mBinding.bindPhoneConflictGuide,
            mBinding.changePhoneBtn,
            mBinding.bindPhoneCancelBtn
        ).forEach {
            it.setOnClickListener(this)
        }

        mBinding.bindPhoneConflictDesc.text = "手机号${mConflictPhone}已经绑定在另一个冲突号上，\n冲突号可能是你之前登录的"

        UserManager.getInstance().userInfoEntity?.run {
            var userName = name ?: ""
            if (userName.length == 2) userName = "${userName[0]}*"
            if (userName.length > 2) userName = "${userName[0]}**${userName[userName.length - 1]}"
            mBinding.itemCurrentName.text = userName
            mBinding.itemCurrentDesc.text = "注册方式：${getRegisterType(registerType)}"
            ImageUtils.display(mBinding.itemCurrentIcon, icon)
        }

        mConflictUser.run {
            mBinding.itemConflictName.text = user?.name
            mBinding.itemConflictDesc.text = "注册方式：${getRegisterType(registerType)}"
            ImageUtils.display(mBinding.itemConflictIcon, user?.icon)
        }
    }

    private fun getRegisterType(type: String?): String {
        return when (type) {
            "qq" -> "QQ"
            "wechat" -> "微信"
            "weibo" -> "微博"
            "douyin" -> "抖音"
//                    "mobile" -> "手机"
            else -> "手机"
        }
    }

    override fun onClick(v: View) {
        super.onClick(v)
        when (v.id) {
            R.id.bind_phone_conflict_guide -> showGuide(v)

            R.id.change_phone_btn -> requireActivity().finish()

            R.id.bind_phone_cancel_btn -> {
                // 点击直接回到账号安全界面
                requireActivity().setResult(BindPhoneActivity.RESULT_FINISH_CODE)
                requireActivity().finish()
            }
        }
    }

    private fun showGuide(v: View) {
        if (!::mPopupWindow.isInitialized) {
            mContentView = layoutInflater.inflate(R.layout.dialog_conflict_guide, null)
            mPopupWindow = PopupWindow(mContentView, 330f.dip2px(), ViewGroup.LayoutParams.WRAP_CONTENT)
            mPopupWindow.run {
                isOutsideTouchable = true
                isFocusable = true
            }
        }
        mPopupWindow.showAsDropDown(v, (-14f).dip2px(), (-9f).dip2px())
    }
}