package com.adealink.weparty.account.login

import android.content.Intent
import com.adealink.frame.log.Log
import com.adealink.frame.router.Router
import com.adealink.frame.statistics.BaseStatEvent
import com.adealink.frame.util.safeToLong
import com.adealink.weparty.AppModule
import com.adealink.weparty.AppModule.Main.Companion.EXTRA_ACCOUNT_LOGIN
import com.adealink.weparty.account.constant.TAG_ACCOUNT_LOGIN
import com.adealink.weparty.account.login.data.LoginResult
import com.adealink.weparty.account.login.data.ThirdType
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.data.UserInfo

abstract class BaseLoginActivity : BaseActivity() {

    fun handleLoginSuccess(result: LoginResult, authType: ThirdType?) {
        Log.i(TAG_ACCOUNT_LOGIN, "handleLoginSuccess: $result, authType: $authType")
        BaseStatEvent.setUserId(AccountModule.uid.safeToLong())
        dismissLoading()
        when {
            needGoRegister(result.userInfo) -> {
                //需要注册，跳转注册页
                Router.build(this, Account.Register.PATH)
                    .putExtra(Account.Common.EXTRA_FROM, "login")
                    .putExtras(intent)
                    .start()
            }

            else -> {
                Router.build(this, AppModule.Main.PATH)
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
                    .putExtras(intent)
                    .putExtra(EXTRA_ACCOUNT_LOGIN, true)
                    .start()
            }
        }
    }

    private fun needGoRegister(userInfo: UserInfo): Boolean {
        return !userInfo.isComplete()
    }

}