package com.gh.gamecenter.beiziad

import android.content.Context
import android.view.View
import android.widget.FrameLayout
import com.beizi.fusion.AdListener
import com.beizi.fusion.BeiZiCustomController
import com.beizi.fusion.BeiZis
import com.beizi.fusion.SplashAd
import com.bun.miitmdid.core.MdidSdkHelper
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.exposure.meta.MetaUtil
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.SPUtils
import com.lightgame.utils.Utils
import java.lang.ref.WeakReference

object BeiziAdHelper {

    private const val BEIZI_APP_ID = "21425"
    private const val BEIZI_START_UP_AD_ID = "105706"
    private const val START_UP_AD_TOTAL_TIME = 3000L

    private val mChannelList = listOf("GH_206")
    private var mSplashAd: WeakReference<SplashAd>? = null

    fun initBeiziSDK(context: Context) {
        BeiZis.setSupportPersonalized(SPUtils.getBoolean(Constants.SP_PERSONAL_RECOMMEND, true))
        BeiZis.init(context, BEIZI_APP_ID, object : BeiZiCustomController() {
            override fun isCanUsePhoneState(): Boolean = false
            override fun isCanUseGaid(): Boolean = false
        })
    }

    fun initOAID(context: Context, callback: (String) -> Unit) {
        MdidSdkHelper.InitSdk(context, true) { _, idSupplier ->
            Utils.log("OAID: ${idSupplier.oaid}")
            callback(idSupplier.oaid)
        }
    }

    fun initBeiziStartUpAd(startAdContainer: View, adsFl: FrameLayout, showAd: Boolean, hideCallback: () -> Unit) {
        startAdContainer.visibility = View.VISIBLE
        mSplashAd = WeakReference(SplashAd(startAdContainer.context, null, BEIZI_START_UP_AD_ID, object : AdListener {
            override fun onAdLoaded() {
                if (showAd) {
                    mSplashAd?.get()?.show(adsFl)
                }
            }

            override fun onAdShown() {
                // do nothing
            }

            override fun onAdFailedToLoad(i: Int) {
                hideCallback()
            }

            override fun onAdClosed() {
                hideCallback()
            }

            override fun onAdTick(l: Long) {
                // do nothing
            }

            override fun onAdClicked() {
                // do nothing
            }
        }, START_UP_AD_TOTAL_TIME))
        mSplashAd?.get()?.loadAd(DisplayUtils.getScreenWidth(), DisplayUtils.getScreenHeight())
    }

    fun cancelBeiziStartUpAd(context: Context) {
        mSplashAd?.get()?.cancel(context)
    }

    fun shouldEnableSDK(channel: String) = mChannelList.contains(channel)
}