package com.abbidot.tracker.deprecated.ui.activity

import androidx.appcompat.widget.AppCompatCheckBox
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.databinding.ActivityLoginRegisterCnBinding
import com.abbidot.tracker.deprecated.ui.fragment.forgot.ForgetPasswordCNFinishFragment
import com.abbidot.tracker.deprecated.ui.fragment.forgot.ForgetPasswordCNStartFragment
import com.abbidot.tracker.deprecated.ui.fragment.login.BindPhoneCNFragment
import com.abbidot.tracker.deprecated.ui.fragment.login.LoginCNAccountFragment
import com.abbidot.tracker.deprecated.ui.fragment.login.LoginCNCodeFragment
import com.abbidot.tracker.deprecated.ui.fragment.login.LoginCNFirstFragment
import com.abbidot.tracker.deprecated.ui.fragment.register.RegisterCNFinishFragment
import com.abbidot.tracker.deprecated.ui.fragment.register.RegisterCNStartFragment

/**
 * 国内登录界面
 */
class LoginRegisterCNActivity :
    BaseActivity<ActivityLoginRegisterCnBinding>(ActivityLoginRegisterCnBinding::inflate) {

    var mWXId = ""
    var mPhone = ""

    //区分忘记密码 和找回密码
    var mType = ConstantInt.Close

    companion object {
        const val Fragment_First = 0
        const val Fragment_Account_Login = 1
        const val Fragment_Code_Login = 2
        const val Fragment_Register_Start = 3
        const val Fragment_Register_Finish = 4
        const val Fragment_Bind_Phone = 5
        const val Fragment_Forget_Password_Start = 6
        const val Fragment_Forget_Password_Finish = 7
    }


    private lateinit var mFragmentList: MutableList<Fragment>

    override fun getTopBar() = mViewBinding.loginCnTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTranslucentTopBar()
        mViewBinding.loginCnTopBar.titleTopBarLayout.setBackgroundResource(R.color.btn_yellow_color)

        mFragmentList = mutableListOf(
            LoginCNFirstFragment.newInstance(mContext),
            LoginCNAccountFragment.newInstance(mContext),
            LoginCNCodeFragment.newInstance(mContext),
            RegisterCNStartFragment.newInstance(mContext),
            RegisterCNFinishFragment.newInstance(mContext),
            BindPhoneCNFragment.newInstance(mContext),
            ForgetPasswordCNStartFragment.newInstance(mContext),
            ForgetPasswordCNFinishFragment.newInstance(mContext)
        )

        var index = 0
        intent.extras?.let {
            index = it.getInt(ConstantString.Index, Fragment_First)
            mType = it.getInt(ConstantString.Type, ConstantInt.Close)
        }
        switchFragment(index)
    }

    fun switchFragment(index: Int) {

        setTopTitle(getString(R.string.login_txt_welcome))
        setTopContent(getString(R.string.login_txt_seeyou))

        supportFragmentManager.commit {

            hideFragment(this, mFragmentList[Fragment_First])
            hideFragment(this, mFragmentList[Fragment_Account_Login])
            hideFragment(this, mFragmentList[Fragment_Code_Login])
            hideFragment(this, mFragmentList[Fragment_Register_Start])
            hideFragment(this, mFragmentList[Fragment_Register_Finish])
            hideFragment(this, mFragmentList[Fragment_Bind_Phone])
            hideFragment(this, mFragmentList[Fragment_Forget_Password_Start])
            hideFragment(this, mFragmentList[Fragment_Forget_Password_Finish])

            //为FragmentTransaction添加指定的自定义属性动画
            setCustomAnimations(
                R.anim.slide_in_right,
                R.anim.slide_out_left,
                R.anim.slide_still,
                R.anim.slide_out_right
            )

            if (mFragmentList[index].isAdded) {
                show(mFragmentList[index])
            } else {
                add(R.id.login_cn_steps_frame, mFragmentList[index])
            }
        }
    }

    fun setTopTitle(title: String) {
        mViewBinding.loginCnTopTitle.text = title
    }

    fun setTopContent(content: String) {
        mViewBinding.loginCnTopContent.text = content
    }

    /**
     * 是否同意协议
     */
    fun isAgreePolicy(checkBox: AppCompatCheckBox): Boolean {
        if (!checkBox.isChecked) {
            val tips = getString(R.string.register_txt_agree1) + getString(
                R.string.register_txt_policy
            ) + getString(R.string.register_txt_agree2) + getString(
                R.string.register_txt_terms
            )
            showToast(tips)
        }
        return checkBox.isChecked
    }
}