package com.adealink.weparty.account.register

import android.content.Intent
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.AppModule
import com.adealink.weparty.account.R
import com.adealink.weparty.account.databinding.ActivityRegisterProfileBinding
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
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.Gender

enum class Page {
    SelectGender,
    CompleteUserInfo,
    SelectCategory
}

@RouterUri(path = [Account.Register.PATH], desc = "注册页面")
class RegisterProfileActivity : BaseActivity() {
    companion object {

        const val TAG_SELECT_GENDER_FRAGMENT = "SelectGenderFragment"
        const val TAG_COMPLETE_USERINFO_FRAGMENT = "CompleteUserInfoFragment"

        const val TAG_SELECT_CATEGORY_FRAGMENT = "SelectCategoryFragment"
    }

    private val binding by viewBinding(ActivityRegisterProfileBinding::inflate)
    private val editViewModel by viewModels<RegisterProfileViewModel> { AccountViewModelFactory() }

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

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

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

    override fun observeViewModel() {
        editViewModel.pageLD.observeWithoutCache(this) { page ->
            when (page) {
                Page.SelectGender -> {
                    inflateSelectGenderFragment()
                }

                Page.SelectCategory -> {
                    inflateSelectCategoryFragment()
                }

                Page.CompleteUserInfo -> {
                    inflateCompleteUserInfoFragment()
                }
            }
        }
    }

    override fun loadData() {
        super.loadData()
        editViewModel.loadRandomProfile()

        val userInfo = ProfileModule.getMyUserInfo()
//        editViewModel.initEditUserInfo(userInfo)
        if (userInfo == null) {
            editViewModel.goPage(Page.SelectGender)
        } else if (userInfo.gender == null || userInfo.gender == Gender.UNKNOWN.gender) {
            editViewModel.goPage(Page.SelectGender)
        } else if (userInfo.nickName.isNullOrEmpty() || userInfo.avatar.isNullOrEmpty() || userInfo.birthday == null || userInfo.birthday == 0L) {
            editViewModel.goPage(Page.CompleteUserInfo)
        } else if (userInfo.interests.isNullOrEmpty()) {
            editViewModel.goPage(Page.SelectCategory)
        } else {
            goHome()
        }
    }

    private fun inflateSelectGenderFragment() {
        val fragment = supportFragmentManager.findFragmentByTag(TAG_SELECT_GENDER_FRAGMENT)
        if (fragment?.isAdded == 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?.isAdded == true) {
            return
        }
        supportFragmentManager.beginTransaction()
            .replace(R.id.fl_content, SelectCategoryFragment(), TAG_SELECT_CATEGORY_FRAGMENT)
            .commitAllowingStateLoss()
    }

    private fun goHome() {
        Router.build(this, AppModule.Main.PATH)
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
            .putExtras(intent)
            .start()
    }

    override fun onBackPressed() {
        val currentPage = editViewModel.pageLD.value
        when (currentPage) {
            Page.SelectCategory -> {
                editViewModel.goPage(Page.CompleteUserInfo)
                return
            }

            Page.CompleteUserInfo -> {
                editViewModel.goPage(Page.SelectGender)
                return
            }

            else -> {

            }
        }
        super.onBackPressed()
        //回到登录页
        AccountModule.logout(true)
    }

}