package cn.yyxx.eyuangame.core.impl.center.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import cn.yyxx.eyuangame.core.entity.ClickType
import cn.yyxx.eyuangame.core.entity.LoginType
import cn.yyxx.eyuangame.core.entity.SdkBackLoginInfo
import cn.yyxx.eyuangame.core.impl.center.MemberActivity
import cn.yyxx.eyuangame.core.impl.center.MemberFragmentTag
import cn.yyxx.support.ResUtils

/**
 * @author #Suyghur.
 * Created on 2021/06/30
 */
class MainFragment : Fragment(), View.OnClickListener {

    private lateinit var mView: View
    private lateinit var memberImpl: MemberActivity

    private lateinit var ivReturn: ImageView
    private lateinit var clBindAccount: ConstraintLayout
    private lateinit var clBindPhone: ConstraintLayout
    private lateinit var clModifyPwd: ConstraintLayout

    private lateinit var ivAccountIcon: ImageView
    private lateinit var tvAccountTips: TextView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        memberImpl = requireActivity() as MemberActivity
        mView = layoutInflater.inflate(ResUtils.getResId(requireActivity(), "yyxx_member_main", "layout"), container, false)
        initView(mView)
        return mView
    }

    private fun initView(view: View) {
        ivReturn = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_iv_return", "id"))
        ivReturn.apply {
            tag = ClickType.ACTION_RETURN
            setOnClickListener(this@MainFragment)
        }

        clBindAccount = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_cl_bind_account", "id"))
        clBindAccount.apply {
            tag = ClickType.ACTION_BIND_ACCOUNT
            setOnClickListener(this@MainFragment)
        }
        clBindPhone = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_cl_bind_phone", "id"))
        clBindPhone.apply {
            tag = ClickType.ACTION_BIND_PHONE
            setOnClickListener(this@MainFragment)
        }
        clModifyPwd = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_cl_modify_pwd", "id"))
        clModifyPwd.apply {
            tag = ClickType.ACTION_MODIFY_PWD
            setOnClickListener(this@MainFragment)
        }

        ivAccountIcon = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_iv_bind_account_icon", "id"))
        tvAccountTips = view.findViewById(ResUtils.getResId(requireActivity(), "yyxx_tv_bind_account_tips", "id"))
        var img = 0
        var tips = ""
        when (SdkBackLoginInfo.instance.loginType) {
            LoginType.TYPE_GUEST_LOGIN -> {
                img = ResUtils.getResId(requireActivity(), "yyxx_guest_img", "drawable")
                tips = ResUtils.getResString(requireActivity(), "yyxx_login_type_guest")
            }
            LoginType.TYPE_FACEBOOK_LOGIN -> {
                img = ResUtils.getResId(requireActivity(), "yyxx_facebook_img", "drawable")
                tips = ResUtils.getResString(requireActivity(), "yyxx_login_type_facebook")
            }
            LoginType.TYPE_GOOGLE_LOGIN -> {
                img = ResUtils.getResId(requireActivity(), "yyxx_google_img", "drawable")
                tips = ResUtils.getResString(requireActivity(), "yyxx_login_type_google")
            }
        }
        ivAccountIcon.setBackgroundResource(img)
        tvAccountTips.text = tips

    }

    private fun showOrHideBindAccount() {
        clBindAccount.visibility = if (SdkBackLoginInfo.instance.hasBindAccount) {
            View.GONE
        } else {
            View.VISIBLE
        }
    }

    private fun showOrHideBindPhone() {
        clBindPhone.visibility = if (SdkBackLoginInfo.instance.hasBindAccount) {
            View.VISIBLE
        } else {
            View.GONE
        }
    }

    private fun showOrHideModifyPwd() {
        clModifyPwd.visibility = if (SdkBackLoginInfo.instance.hasBindAccount && !TextUtils.isEmpty(SdkBackLoginInfo.instance.phoneNum)) {
            View.VISIBLE
        } else {
            View.GONE
        }
    }

    override fun onResume() {
        super.onResume()
        showOrHideBindAccount()
        showOrHideBindPhone()
        showOrHideModifyPwd()
    }

    override fun onClick(v: View?) {
        v?.apply {
            when (tag as Int) {
                ClickType.ACTION_RETURN -> requireActivity().onBackPressed()
                ClickType.ACTION_BIND_ACCOUNT -> memberImpl.switchFragment(MemberFragmentTag.BIND_ACCOUNT)
                ClickType.ACTION_BIND_PHONE -> memberImpl.switchFragment(MemberFragmentTag.BIND_PHONE)
                ClickType.ACTION_MODIFY_PWD -> memberImpl.switchFragment(MemberFragmentTag.MODIFY_PWD)
            }
        }
    }
}