package cn.yyxx.eyuangame.core.linking

import android.app.Activity
import android.app.Application
import android.content.Context
import cn.yyxx.eyuangame.base.entity.SdkEvent
import cn.yyxx.eyuangame.core.internal.IEventObserver
import cn.yyxx.eyuangame.core.linking.channel.LinkingAdjustImpl
import cn.yyxx.eyuangame.core.linking.channel.LinkingFacebookImpl
import cn.yyxx.eyuangame.core.linking.channel.LinkingFirebaseImpl

/**
 * @author #Suyghur.
 * Created on 2021/06/17
 */
class Linking private constructor() {

    private val observers: MutableList<IEventObserver> = mutableListOf()

    fun initialize(application: Application) {
        observers.add(LinkingAdjustImpl())
        observers.add(LinkingFirebaseImpl())
        observers.add(LinkingFacebookImpl())

        for (ob in observers) {
            ob.onInitialize(application)
        }
    }


    fun release() {
        if (!observers.isNullOrEmpty()) {
            observers.clear()
        }
    }


    fun login(context: Context) {
        for (ob in observers) {
            ob.onLogin(context)
        }
    }

    fun register(context: Context) {
        for (ob in observers) {
            ob.onRegister(context)
        }
    }

    fun charge(context: Context, amount: Int) {
        for (ob in observers) {
            ob.onCharge(context, amount)
        }
    }

    fun roleCreate(context: Context) {
        for (ob in observers) {
            ob.onRoleCreate(context)
        }
    }

    fun roleLauncher(context: Context) {
        for (ob in observers) {
            ob.onRoleLauncher(context)
        }
    }

    fun create(activity: Activity) {
        for (ob in observers) {
            ob.onCreate(activity)
        }
    }

    fun resume(activity: Activity) {
        for (ob in observers) {
            ob.onResume(activity)
        }
    }

    fun pause(activity: Activity) {
        for (ob in observers) {
            ob.onPause(activity)
        }
    }

    fun sdkEvent(context: Context, sdkEvent: SdkEvent) {
        for (ob in observers) {
            ob.onSdkEvent(context, sdkEvent)
        }
    }

    fun extEvent(context: Context, sdkEvent: SdkEvent) {
        for (ob in observers) {
            ob.onExtEvent(context, sdkEvent)
        }
    }


    companion object {
        val instance: Linking by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            Linking()
        }
    }
}