package com.adealink.weparty.account.login

import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.fragment.app.FragmentContainerView
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.weparty.R
import com.adealink.weparty.account.login.data.ThirdType
import com.adealink.weparty.account.login.viewmodel.LoginViewModel
import com.adealink.weparty.account.viewModel.AccountViewModelFactory
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.commonui.widget.CommonDialog
import com.adealink.weparty.module.account.Account
import com.adealink.weparty.module.account.AccountModule
import com.adealink.weparty.module.account.data.UserKickReason
import com.adealink.weparty.ui.home.GuestHomeFragment
import com.adealink.weparty.util.QMUIStatusBarHelper

@RouterUri(path = [Account.Login.PATH], desc = "登录页面")
class LoginActivity : BaseLoginActivity() {

    private val loginViewModel by viewModels<LoginViewModel> { AccountViewModelFactory() }

    @BindExtra(Account.Common.EXTRA_KICK_OUT_REASON)
    var kickOutReason: Long? = null

    @BindExtra(Account.Common.EXTRA_KICK_OUT_LOGIN_TYPE)
    var kickOutLoginType: String? = null

    @BindExtra(Account.Common.EXTRA_KICK_OUT_CONTENT)
    var kickOutContent: String? = null

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

    override fun onAfterSuperCreate(): Boolean {
        loginViewModel.onNewIntent(intent)
        return super.onAfterSuperCreate()
    }

    override fun initViews() {
        super.initViews()
        window.setBackgroundDrawable(ColorDrawable(getCompatColor(R.color.white)))
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

        QMUIStatusBarHelper.setStatusBarLightMode(this)
        setContentView(FragmentContainerView(this).apply {
            id = R.id.fl_content
        })
        inflateMainFragment()
        AccountModule.showLoginDialog(supportFragmentManager)
    }

    private fun inflateMainFragment() {
        var fragment =
            supportFragmentManager.findFragmentByTag(GuestHomeFragment.TAG) as? GuestHomeFragment
        if (fragment?.isAdded == true) {
            return
        }

        fragment = fragment ?: GuestHomeFragment()
        supportFragmentManager.beginTransaction()
            .replace(R.id.fl_content, fragment, GuestHomeFragment.TAG)
            .commitAllowingStateLoss()
    }

    override fun loadData() {
        super.loadData()
        handleKickOutIfNeed()
    }

    private fun handleKickOutIfNeed() {
        if (kickOutReason == null) {
            return
        }
        when (kickOutReason) {
            UserKickReason.REASON_KICK_ONLINE_BY_MULTIPOINT_LOGIN.value -> {
                val loginType = getDisplayLoginType(kickOutLoginType)
                val message = getCompatString(
                    com.adealink.weparty.account.R.string.account_kick_off,
                    loginType,
                    loginType
                )
                CommonDialog.Builder()
                    .message(message)
                    .setShowDefaultCancel(false)
                    .build().show(supportFragmentManager)
            }

            UserKickReason.REASON_LOCAL_KICK_TOKEN_EXPIRE.value -> {
                showToast(getCompatString(com.adealink.weparty.account.R.string.account_token_expire))
            }

            else -> {

            }
        }
    }

    private fun getDisplayLoginType(thirdType: String?): String {
        return when (thirdType) {
            ThirdType.GOOGLE.value -> {
                getCompatString(com.adealink.weparty.account.R.string.account_google)
            }

            ThirdType.PHONE.value -> {
                getCompatString(com.adealink.weparty.account.R.string.account_phone)
            }

            else -> {
                ""
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        loginViewModel.handleOnActivityResult(requestCode, resultCode, data)
    }
}