package com.adealink.weparty.account.register

import androidx.activity.viewModels
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.weparty.account.R
import com.adealink.weparty.account.databinding.ActivityRegisterProfileBinding
import com.adealink.weparty.account.login.manager.loginManager
import com.adealink.weparty.account.register.fragment.CompleteUserInfoFragment
import com.adealink.weparty.account.register.fragment.SelectCategoryFragment
import com.adealink.weparty.account.register.fragment.SelectGenderFragment
import com.adealink.weparty.account.register.viewmodel.RegisterProfileViewModel
import com.adealink.weparty.account.viewModel.AccountViewModelFactory
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.module.account.Account
import com.adealink.weparty.module.account.AccountModule

@RouterUri(path = [Account.Register.PATH], desc = "注册页面")
class RegisterProfileActivity : BaseActivity() {
    private val binding by viewBinding(ActivityRegisterProfileBinding::inflate)
    private val viewModel by viewModels<RegisterProfileViewModel> { AccountViewModelFactory() }

    @BindExtra(Account.Common.EXTRA_FROM)
    var from: String? = null

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
        if (from == "login") {
            loginManager.setIsNewRegister(true)
        }
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        inflateSelectGenderFragment()
    }

    override fun observeViewModel() {
        viewModel.genderSelectFinish.observeWithoutCache(this) {
            inflateCompleteUserInfoFragment()
        }
        viewModel.completeUserInfoFinish.observeWithoutCache(this) {
            inflateSelectCategoryFragment()
        }
    }

    private fun inflateSelectGenderFragment() {
        val fragment = supportFragmentManager.findFragmentByTag(TAG_SELECT_GENDER_FRAGMENT)
        if (fragment?.isDetached == true) {
            return
        }
        supportFragmentManager.beginTransaction()
            .replace(R.id.fl_content, SelectGenderFragment(), TAG_SELECT_GENDER_FRAGMENT)
            .commitAllowingStateLoss()
    }

    private fun inflateCompleteUserInfoFragment() {
        val fragment = supportFragmentManager.findFragmentByTag(TAG_COMPLETE_USERINFO_FRAGMENT)
        if (fragment?.isDetached == true) {
            return
        }
        supportFragmentManager.beginTransaction()
            .replace(R.id.fl_content, CompleteUserInfoFragment(), TAG_COMPLETE_USERINFO_FRAGMENT)
            .commitAllowingStateLoss()
    }

    private fun inflateSelectCategoryFragment() {
        val fragment = supportFragmentManager.findFragmentByTag(TAG_SELECT_CATEGORY_FRAGMENT)
        if (fragment?.isDetached == true) {
            return
        }
        supportFragmentManager.beginTransaction()
            .replace(R.id.fl_content, SelectCategoryFragment(), TAG_SELECT_CATEGORY_FRAGMENT)
            .commitAllowingStateLoss()
    }

    override fun onBackPressed() {
        if (supportFragmentManager.findFragmentByTag(TAG_COMPLETE_USERINFO_FRAGMENT) != null) {
            inflateSelectGenderFragment()
            return
        }
        super.onBackPressed()
        //回到登录页
        AccountModule.logout()
    }

    companion object {
        const val TAG_SELECT_GENDER_FRAGMENT = "SelectGenderFragment"
        const val TAG_COMPLETE_USERINFO_FRAGMENT = "CompleteUserInfoFragment"

        const val TAG_SELECT_CATEGORY_FRAGMENT = "SelectCategoryFragment"
    }

}