package com.halo.assistant.fragment.user

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.gh.common.util.DialogUtils
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.ToolbarFragment
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.FragmentBbsCertificationBinding
import com.gh.gamecenter.feature.entity.MobileAuthEntity
import com.halo.assistant.fragment.user.BbsCertificationViewModel.Companion.TYPE_BBS_CERT
import com.halo.assistant.fragment.user.BbsCertificationViewModel.Companion.TYPE_BIND_PHONE
import com.halo.assistant.fragment.user.BbsCertificationViewModel.Companion.TYPE_REAL_NAME_CERT

class BbsCertificationFragment : ToolbarFragment() {

    private val mViewModel: BbsCertificationViewModel by viewModels()
    private val mBinding by lazy { FragmentBbsCertificationBinding.inflate(layoutInflater) }

    override fun getLayoutId() = 0

    override fun getInflatedLayout() = mBinding.root

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mViewModel.unBindLiveData.observeNonNull(viewLifecycleOwner) {
            toast("解除成功")
            requireActivity().finish()
        }
        initView(mViewModel.getMobileAuth())
    }

    fun initView(entity: MobileAuthEntity?) {
        mBinding.run {
            val tempType = mViewModel.getCertType()
            if (tempType == TYPE_BBS_CERT) {
                (certIv.layoutParams as ViewGroup.MarginLayoutParams).apply {
                    topMargin = 24F.dip2px()
                    certIv.layoutParams = this
                }
                certTv.text = R.string.bbs_cert_complete.toResString()
                relieveTitle.text = "关联手机号"
                relievePhoneTv.visibility = View.VISIBLE
                relievePhoneTv.text = entity?.number
                historyContainer.visibility = View.GONE
                relieveTv.visibility = View.VISIBLE
            } else {
                if (tempType == TYPE_REAL_NAME_CERT) certTv.text = R.string.bbs_cert_real_name_complete.toResString()
                if (tempType == TYPE_BIND_PHONE) certTv.text = R.string.bbs_cert_bind_phone_complete.toResString()
                relieveTitle.goneIf(entity?.number.isNullOrBlank())
                historyContainer.goneIf(entity?.number.isNullOrBlank())
                historyTimeTv.text = entity?.time
                historyPhoneTv.text = entity?.number
            }
            entity?.remark?.run {
                noticeTv.text = title
                linkTv.text = text
                linkTv.setOnClickListener {
                    DirectUtils.directToLinkPage(requireContext(), this, "社区交流身份认证-说明文案", "")
                }
            }

            relieveTv.setOnClickListener {
                if (mViewModel.getCertType() != TYPE_BBS_CERT) {
                    DialogHelper.showDialog(
                        requireContext(),
                        "解除关联",
                        "确定要解除账号与${entity?.number}的关联吗？",
                        "解除关联",
                        "以后再说",
                        { mViewModel.unBindPhone() }
                    )
                } else {
                    DialogUtils.showRelievePhoneDialog(requireContext())
                }
            }
        }
    }
}