package com.base.appzxhy.business.ads.admob

import android.app.Activity
import android.os.Bundle
import com.base.appzxhy.MyApplication
import com.base.appzxhy.bean.config.AdConfigBean
import com.base.appzxhy.business.ads.AdEvent
import com.base.appzxhy.business.ads.SolarEngineEvent
import com.base.appzxhy.business.ads.taichiPref
import com.base.appzxhy.business.ads.taichiSharedPreferencesEditor
import com.base.appzxhy.business.helper.EventUtils
import com.base.appzxhy.utils.LogEx
import com.facebook.appevents.AppEventsConstants
import com.facebook.appevents.AppEventsLogger
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdValue
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.OnPaidEventListener
import com.google.android.gms.ads.ResponseInfo
import com.google.android.gms.ads.appopen.AppOpenAd
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.math.BigDecimal
import java.util.Currency
import kotlin.random.Random

class AdmobEvent : AdEvent {

    override val TAG: String = "AdmobEvent"

    val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

    constructor(adUnit: String, from: String) : super() {
        this.adUnit = adUnit
        this.from = from
    }

    fun pullAd(
        responseInfo: ResponseInfo?,
        error: LoadAdError? = null,
    ) = scope.launch {
        var key = "ad_pull"
        val obj = JSONObject()
        obj.put("req_id", reqId)
        if (responseInfo != null) {
            val response = responseInfo.adapterResponses.getOrNull(0)
            if (response != null) {
                obj.put("source", response.adSourceName)

                val credentials = mapOf(
                    "placementid" to response.credentials.get("placementid"),
                    "appid" to response.credentials.get("appid"),
                    "pubid" to response.credentials.get("pubid")
                )
                obj.put("credentials", credentials.toString())
            }
            obj.put("session_id", responseInfo.responseId)
        }
        obj.put("networkname", responseInfo?.mediationAdapterClassName)
        obj.put("ad_unit", adUnit)
        obj.put("from", from)
        if (error == null) {
            obj.put("status", "1")
        } else {
            obj.put("errMsg", error.toString())
            obj.put("status", "2")
            key = "ad_pull_error"
        }
        EventUtils.event(key, ext = obj)
        LogEx.logDebug(TAG, "$key obj=$obj")
    }

    fun clickAd(responseInfo: ResponseInfo?) = scope.launch {

        val response = responseInfo?.adapterResponses?.getOrNull(0)
        val obj = JSONObject()
        obj.put("req_id", reqId)
        obj.put("source", response?.adSourceName)
        obj.put("ad_unit", adUnit)

        val credentials = mapOf(
            "placementid" to response?.credentials?.get("placementid"),
            "appid" to response?.credentials?.get("appid"),
            "pubid" to response?.credentials?.get("pubid")
        )
        obj.put("credentials", credentials.toString())
        obj.put("session_id", responseInfo?.responseId)
        obj.put("from", from)
        obj.put("networkname", responseInfo?.mediationAdapterClassName)
        if (adUnit != "nativeAd") {
            EventUtils.event("ad_click", ext = obj)
        } else {
            EventUtils.event("ad_click", ext = obj)
        }
    }

    fun showAd(responseInfo: ResponseInfo?, activity: Activity? = null) = scope.launch {
        val response = responseInfo?.adapterResponses?.getOrNull(0)
        val obj = JSONObject()
        obj.put("req_id", reqId)
        obj.put("source", response?.adSourceName)
        obj.put("ad_unit", adUnit)
        obj.put("networkname", responseInfo?.mediationAdapterClassName)
        val credentials = mapOf(
            "placementid" to response?.credentials?.get("placementid"),
            "appid" to response?.credentials?.get("appid"),
            "pubid" to response?.credentials?.get("pubid")
        )
        obj.put("credentials", credentials.toString())
        obj.put("session_id", responseInfo?.responseId)
        obj.put("from", activity?.javaClass?.simpleName)
        if (adUnit != "nativeAd") {
            EventUtils.event("ad_show", ext = obj)
        } else {
            EventUtils.event("ad_show", ext = obj)
        }
        LogEx.logDebug(TAG, "ad_show $obj")
    }

    fun adShowError(adError: AdError) = scope.launch {
        val obj = JSONObject()
        obj.put("req_id", reqId)
        obj.put("reason", adError.message)
        obj.put("code", adError.code)
        obj.put("from", from)
        obj.put("ad_unit", "openAd")
        EventUtils.event("ad_show_error", ext = obj)
        LogEx.logDebug(TAG, "ad_show_error $obj")
    }
}
