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.facebook.FacebookSdk
import com.facebook.LoggingBehavior
import com.facebook.appevents.AppEventsConstants
import com.facebook.appevents.AppEventsLogger
import java.math.BigDecimal
import java.util.*

/**
 * @author #Suyghur.
 * Created on 2021/07/06
 */
class LinkingFacebookImpl : IEventObserver {

    private lateinit var fbLogger: AppEventsLogger
    private var isInitSuccess = false

    override fun onInitialize(application: Application) {

        FacebookSdk.setIsDebugEnabled(true)
        FacebookSdk.addLoggingBehavior(LoggingBehavior.APP_EVENTS)

        fbLogger = AppEventsLogger.newLogger(application)

        if (MMKVUtils.instance.eventKV.decodeBool("facebook_activities")) {
            isInitSuccess = true
            return
        }
        fbLogger.logEvent("open_app")
        Logger.d("facebook log open app success")
        MMKVUtils.instance.eventKV.encode("facebook_activities", true)
        isInitSuccess = true
    }

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

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

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

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

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

        fbLogger.logEvent(AppEventsConstants.EVENT_NAME_COMPLETED_REGISTRATION)
        Logger.d("facebook log sign up success")
    }

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

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

        if (!MMKVUtils.instance.eventKV.decodeBool("facebook_first_purchase")) {
            fbLogger.logEvent("first_purchase", amount.toDouble() / 100)
            Logger.d("facebook log first charge success")
            MMKVUtils.instance.eventKV.encode("facebook_first_purchase", true)
        }

        fbLogger.logPurchase(BigDecimal.valueOf(amount.toDouble() / 100), Currency.getInstance("USD"))
        Logger.d("facebook log charge success")
    }

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

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

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

        if (TextUtils.isEmpty(SdkBackLoginInfo.instance.userId)) {
            Logger.e("facebook log failed , user is null")
            return
        }
        fbLogger.logEvent("enter_game")
        Logger.d("facebook log create player success")
    }

    override fun onCreate(activity: Activity) {
        if (!isInitSuccess) {
            Logger.e("facebook log failed , component initialize failed")
            return
        }
        AppEventsLogger.activateApp(activity.application)
    }

    override fun onResume(activity: Activity) {
    }

    override fun onPause(activity: Activity) {
    }

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

    override fun onExtEvent(context: Context, sdkEvent: SdkEvent) {
        if (!isInitSuccess) {
            Logger.e("facebook log failed , component initialize failed")
            return
        }
        if (sdkEvent.standard) {
            val eventName = when (sdkEvent.fbAliasName) {
                "fb_mobile_tutorial_completion" -> AppEventsConstants.EVENT_NAME_COMPLETED_TUTORIAL
                "fb_mobile_level_achieved" -> AppEventsConstants.EVENT_NAME_ACHIEVED_LEVEL
                "fb_mobile_spent_credits" -> AppEventsConstants.EVENT_NAME_SPENT_CREDITS
                "fb_mobile_add_payment_info" -> AppEventsConstants.EVENT_NAME_ADDED_PAYMENT_INFO
                "fb_mobile_initiated_checkout" -> AppEventsConstants.EVENT_NAME_INITIATED_CHECKOUT
                "fb_mobile_add_to_cart" -> AppEventsConstants.EVENT_NAME_ADDED_TO_CART
                "fb_mobile_add_to_wishlist" -> AppEventsConstants.EVENT_NAME_ADDED_TO_WISHLIST
                "fb_mobile_achievement_unlocked" -> AppEventsConstants.EVENT_NAME_UNLOCKED_ACHIEVEMENT
                "fb_mobile_search" -> AppEventsConstants.EVENT_NAME_SEARCHED
                "fb_mobile_rate" -> AppEventsConstants.EVENT_NAME_RATED
                else -> ""
            }
            fbLogger.logEvent(eventName)
        } else {
            fbLogger.logEvent(sdkEvent.eventName)
        }
        Logger.d("facebook log ext event ${sdkEvent.eventName} success")
    }
}

