package com.simplecleaner.app.business.ads

import com.simplecleaner.app.MyApplication
import com.simplecleaner.app.business.helper.EventUtils
import com.simplecleaner.app.utils.LogEx
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import org.json.JSONObject
import java.util.UUID


val taichiPref by lazy {
    MyApplication.appContext.getSharedPreferences("TaichiTroasCache", 0)
}

val taichiSharedPreferencesEditor by lazy {
    taichiPref.edit()
}


abstract class AdEvent {


    abstract val TAG: String

    var adUnit: String = ""
    var from: String = ""
    var reqId = UUID.randomUUID().toString()
    var isUnLimit: Boolean = false
    val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

    fun adPrepareShow() {
        val obj1 = JSONObject()
        obj1.put("ad_unit", adUnit)
        obj1.put("from", from)
        obj1.put("req_id", reqId)
        EventUtils.event("ad_prepare_show", ext = obj1)
        LogEx.logDebug(TAG, "ad_prepare_show_$adUnit $obj1")
    }

    fun adPulStart() {
        val obj = JSONObject()
        obj.put("ad_unit", adUnit)
        obj.put("from", from)
        obj.put("req_id", reqId)
        obj.put("ad_type", adUnit)
        EventUtils.event("ad_pull_start", ext = obj)
        LogEx.logDebug(TAG, "ad_pull_start_$adUnit $obj")
    }

    fun adShowError(reason: Any) {
        val obj = JSONObject()
        obj.put("ad_unit", adUnit)
        obj.put("from", from)
        obj.put("req_id", reqId)
        obj.put("reason", reason.toString())
        EventUtils.event("ad_show_error", ext = obj)
        LogEx.logDebug(TAG, "ad_show_error_$adUnit $obj")
    }

    fun adLimited(value: String) {
        val obj = JSONObject()
        obj.put("ad_unit", adUnit)
        obj.put("from", from)
        obj.put("req_id", reqId)
        EventUtils.event("ad_limit", value, obj)
        LogEx.logDebug(TAG, "ad_limit_$adUnit $obj")
    }

}