package com.adealink.weparty.account.login.auth

import android.app.Activity
import android.content.Intent
import com.adealink.weparty.account.login.data.ThirdType
import java.lang.ref.WeakReference

class AuthManager(private val authCallback: IAuthCallback?) {
    private var googleAuth: GoogleAuth? = null

    fun startAuth(authType: ThirdType, activity: Activity, secret: String? = null) {
        when (authType) {
            ThirdType.GOOGLE -> {
                initGoogleAuth(activity)
                googleAuth?.auth()
            }
            else -> {
                //ntd.
            }
        }
    }

    private fun initGoogleAuth(activity: Activity) {
        if (googleAuth == null) {
            googleAuth = GoogleAuth(WeakReference(activity), authCallback)
        }
    }

    fun handleOnActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
        when (requestCode) {
            REQ_GOOGLE_ONE_TAP, REQ_GOOGLE_SIGN_IN -> {
                googleAuth?.onActivityResult(requestCode, resultCode, intent)
            }

            else -> {

            }
        }
    }

    fun logout(authType: String, activity: Activity) {
        when (authType) {
            ThirdType.GOOGLE.value -> {
                initGoogleAuth(activity)
                googleAuth?.logout()
            }
            else -> {
                //ntd.
            }
        }
    }

    fun onNewIntent(intent: Intent?) {
    }

    companion object {
        const val REQ_GOOGLE_ONE_TAP = 1001
        const val REQ_GOOGLE_SIGN_IN = 1002
    }
}