package cn.yyxx.eyuangame.core.linking.channel

import android.app.Activity
import android.app.Application
import android.content.Context
import android.text.TextUtils
import cn.yyxx.eyuangame.base.entity.SdkEvent
import cn.yyxx.eyuangame.base.utils.Logger
import cn.yyxx.eyuangame.core.entity.LoginType
import cn.yyxx.eyuangame.core.entity.SdkBackLoginInfo
import cn.yyxx.eyuangame.core.internal.IEventObserver
import cn.yyxx.eyuangame.core.utils.MMKVUtils
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.analytics.ktx.logEvent
import com.google.firebase.ktx.Firebase

/**
 * @author #Suyghur.
 * Created on 2021/06/17
 */
class LinkingFirebaseImpl : IEventObserver {

    private var isInitSuccess = false

    override fun onInitialize(application: Application) {
        if (MMKVUtils.instance.eventKV.decodeBool("firebase_activities")) {
            isInitSuccess = true
            return
        }
        Firebase.analytics.logEvent("open_app", null)
        Logger.d("firebase log open app success")
        MMKVUtils.instance.eventKV.encode("firebase_activities", true)
        isInitSuccess = true
    }

    override fun onLogin(context: Context) {
        if (!isInitSuccess) {
            Logger.e("firebase log failed , component initialize failed")
            return
        }

        if (TextUtils.isEmpty(SdkBackLoginInfo.instance.userId)) {
            Logger.e("firebase log failed , user is null")
            return
        }

        when (SdkBackLoginInfo.instance.loginType) {
            LoginType.TYPE_GUEST_LOGIN -> {
                Firebase.analytics.logEvent("login_success_quick", null)
                Logger.d("firebase log guest login success")
            }
            LoginType.TYPE_FACEBOOK_LOGIN -> {
                Firebase.analytics.logEvent("login_success_fb", null)
                Logger.d("firebase log facebook login success")
            }
            LoginType.TYPE_GOOGLE_LOGIN -> {
                Firebase.analytics.logEvent("login_success_google", null)
                Logger.d("firebase log google login success")
            }
            LoginType.TYPE_ACCOUNT_LOGIN -> {
                Firebase.analytics.logEvent("login_success_account", null)
                Logger.d("firebase log account login success")
            }
        }
    }

    override fun onRegister(context: Context) {
        if (!isInitSuccess) {
            Logger.e("firebase log failed , component initialize failed")
            return
        }

        if (TextUtils.isEmpty(SdkBackLoginInfo.instance.userId)) {
            Logger.e("firebase log failed , user is null")
            return
        }

        Firebase.analytics.logEvent("sign_up", null)
        Logger.d("firebase log sign up success")
    }

    override fun onCharge(context: Context, amount: Int) {
        if (!isInitSuccess) {
            Logger.e("firebase log failed , component initialize failed")
            return
        }

        if (TextUtils.isEmpty(SdkBackLoginInfo.instance.userId)) {
            Logger.e("firebase log failed , user is null")
            return
        }

        if (!MMKVUtils.instance.eventKV.decodeBool("firebase_first_purchase")) {
            Firebase.analytics.logEvent("first_purchase") {
                param(FirebaseAnalytics.Param.PRICE, (amount.toDouble() / 100).toString())
            }
            Logger.d("firebase log first charge success")
            MMKVUtils.instance.eventKV.encode("firebase_first_purchase", true)
        }

        Firebase.analytics.logEvent("ecommerce_purchase") {
            param(FirebaseAnalytics.Param.PRICE, (amount.toDouble() / 100).toString())
        }
        Logger.d("firebase log charge success")

    }

    override fun onRoleCreate(context: Context) {
        if (!isInitSuccess) {
            Logger.e("firebase log failed , component initialize failed")
            return
        }

        if (TextUtils.isEmpty(SdkBackLoginInfo.instance.userId)) {
            Logger.e("firebase log failed , user is null")
            return
        }
        Firebase.analytics.logEvent("create_player", null)
        Logger.d("firebase log create player success")
    }

    override fun onRoleLauncher(context: Context) {
        if (!isInitSuccess) {
            Logger.e("firebase log failed , component initialize failed")
            return
        }

        if (TextUtils.isEmpty(SdkBackLoginInfo.instance.userId)) {
            Logger.e("firebase log failed , user is null")
            return
        }
        Firebase.analytics.logEvent("enter_game", null)
        Logger.d("firebase log enter game success")
    }

    override fun onCreate(activity: Activity) {
    }

    override fun onResume(activity: Activity) {
    }

    override fun onPause(activity: Activity) {
    }


    override fun onSdkEvent(context: Context, sdkEvent: SdkEvent) {
        if (!isInitSuccess) {
            Logger.e("firebase log failed , component initialize failed")
            return
        }
        Firebase.analytics.logEvent(sdkEvent.eventName, null)
        Logger.d("firebase log sdk event ${sdkEvent.eventName} success")
    }

    override fun onExtEvent(context: Context, sdkEvent: SdkEvent) {
        if (!isInitSuccess) {
            Logger.e("firebase log failed , component initialize failed")
            return
        }
        Firebase.analytics.logEvent(sdkEvent.eventName, null)
        Logger.d("firebase log ext event ${sdkEvent.eventName} success")
    }
}