package com.gogame.minesweeper.ad

import android.app.Activity
import android.util.Log
import com.thinkup.core.api.AdError
import com.thinkup.core.api.TUAdInfo
import com.thinkup.interstitial.api.TUInterstitial
import com.thinkup.interstitial.api.TUInterstitialListener

class AdInstLoad {
    private var mPlace: String
    private var adLoadListener: LoadListener? = null
    private var activity: Activity? = null

    constructor(activity: Activity, place: String, listener: LoadListener?) {
        this.mPlace = place
        this.adLoadListener = listener
        this.activity = activity
        init()
    }

    constructor(place: String, listener: LoadListener?) {
        this.mPlace = place
        this.adLoadListener = listener
        init()
    }

    private fun init() {
        val mInterstitialAd = TUInterstitial(activity,mPlace)
        mInterstitialAd.setAdListener(object : TUInterstitialListener{
            override fun onInterstitialAdLoaded() {
                Log.d("ocean", "load ad loaded")
                val info = mInterstitialAd.checkAdStatus().tuTopAdInfo
                if (info != null) {
                    InstAdCacheManager.instance.setAdCache(mPlace, mInterstitialAd)
                    adLoadListener?.loaded(info)
                } else {
                    adLoadListener?.loadFailed("AdInfo null")
                }
            }

            override fun onInterstitialAdLoadFail(adError: AdError?) {
                adLoadListener?.loadFailed("code:${adError?.code}")
                Log.d("ocean", "load ad onError->${adError.toString()}")
            }

            override fun onInterstitialAdClicked(p0: TUAdInfo?) {
            }

            override fun onInterstitialAdShow(p0: TUAdInfo?) {
            }

            override fun onInterstitialAdClose(p0: TUAdInfo?) {
            }

            override fun onInterstitialAdVideoStart(p0: TUAdInfo?) {
            }

            override fun onInterstitialAdVideoEnd(p0: TUAdInfo?) {
            }

            override fun onInterstitialAdVideoError(p0: AdError?) {
            }

        })
        mInterstitialAd.load()
    }
}