package com.develop.auth.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.develop.auth.R
import com.develop.auth.databinding.ActivityMemberAuthBinding
import com.develop.auth.viewmodel.FragmentTag
import com.develop.auth.viewmodel.MemberViewModel
import com.develop.base.ext.navigateTo
import com.develop.base.util.MMkvUtils
import com.develop.common.router.Screens
import com.develop.common.tag.LOGIN_TAG
import com.develop.common.ui.CommonBVMActivity

@Route(path = Screens.Auth.MEMBER)
class MemberAuthActivity : CommonBVMActivity<ActivityMemberAuthBinding, MemberViewModel>() {
    @JvmField
    @Autowired(name = "enter_from_home")
    var enterFromHome: Boolean = false

    @JvmField
    @Autowired(name = "isTokenOut")
    var isTokenOut: Boolean = false

    private val memberForgotFragment = MemberForgotFragment()
    private val memberLaunchFragment = MemberLaunchFragment()
    private val memberLoginBinding = MemberLoginFragment()
    private val memberProfileFragment = MemberProfileFragment()
    private val memberRegisterFragment = MemberRegisterFragment()
    private val fragmentMap = HashMap<FragmentTag, Fragment>()

    override fun createViewModel(): MemberViewModel {
        return getViewModel(MemberViewModel::class.java)
    }

    override fun createViewBinding(inflater: LayoutInflater): ActivityMemberAuthBinding {
        return ActivityMemberAuthBinding.inflate(inflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        initFragmentList()
        viewModel.enterFromHome = enterFromHome
        binding.tvSkip.setOnClickListener {
            navigateTo(Screens.Main.ENTRANCE_CHOSEN)
            finish()
        }
        viewModel.fragmentChangeLiveData.observe(this) {
            fragmentMap[it]?.apply {
                replaceFragment(R.id.fl_container, this)
            }
        }
        if (isTokenOut) {
            ensureFragmentAdd(
                R.id.fl_container,
                MemberLoginFragment()
            )
        } else {
            if (MMkvUtils.getBool(LOGIN_TAG)) {
                addFragment(
                    R.id.fl_container,
                    memberProfileFragment
                )
            } else {
                addFragment(
                    R.id.fl_container,
                    memberLaunchFragment
                )
            }
        }
    }

    private fun initFragmentList() {
        fragmentMap[FragmentTag.MEMBER_FORGER] = memberForgotFragment
        fragmentMap[FragmentTag.MEMBER_LAUNCH] = memberLaunchFragment
        fragmentMap[FragmentTag.MEMBER_LOGIN] = memberLoginBinding
        fragmentMap[FragmentTag.MEMBER_PROFILE] = memberProfileFragment
        fragmentMap[FragmentTag.MEMBER_REGISTER] = memberRegisterFragment
    }

    fun getRootView(): View {
        return binding.flRoot
    }

    override fun onDestroy() {
        super.onDestroy()
        fragmentMap.clear()
    }
}