package com.abbidot.tracker.ui.activity.account

import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import coil.load
import coil.transform.BlurTransformation
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.databinding.ActivityAddEmailValidBinding
import com.abbidot.tracker.ui.fragment.account.EmailValidFragment

open class AddEmailValidActivity :
    BaseActivity<ActivityAddEmailValidBinding>(ActivityAddEmailValidBinding::inflate) {

    lateinit var mFragmentList: MutableList<Fragment>
    lateinit var mEmailValidFragment: EmailValidFragment

    var mIndex = 0

    //用户邮箱
    var mUserEmail = ""

    override fun getTopBar() = mViewBinding.forgotPasswordV2TopBar.titleTopBar

    override fun initData() {
        super.initData()
        mViewBinding.apply {
            setLeftBackImage(R.drawable.icon_black_back_svg)
            root.setBackgroundResource(R.color.transparent)
            ivForgotPasswordV2BlurBg.load(R.drawable.icon_login_v2_bg) {
                transformations(BlurTransformation(mContext, 25f))
            }
            setListenKeyboardChange(750, methodType = ConstantInt.Type1)
        }

        switchFragment(mIndex)
        mEmailValidFragment.setEmail(mUserEmail)
    }

    fun setToEmail(email: String) {
        mUserEmail = email
        mEmailValidFragment.setEmail(mUserEmail)
    }

    fun switchFragment(index: Int) {
        mIndex = index

        supportFragmentManager.commit {

            for (fragment in mFragmentList) {
                hideFragment(this, fragment)
            }

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

            mFragmentList[index].let {
                LogUtil.e("switchFragment--->$index,${it.isAdded},${it.isHidden}")
                if (it.isAdded || it.isHidden) {
                    show(it)
                } else {
                    add(R.id.fc_forgot_password_v2_frame, it)
                }
            }
        }
    }

    private fun popBack() {
        if (mIndex > 0) {
            mIndex--
            switchFragment(mIndex)
        } else finish()
    }

    override fun leftBackOnClick() {
        popBack()
    }

    override fun listenBackPressed() {
        popBack()
    }
}