package com.draw.painting.test.ironad

import android.app.Activity
import android.os.CountDownTimer
import android.util.Log
import com.ironsource.mediationsdk.IronSource
import com.ironsource.mediationsdk.adunit.adapter.utility.AdInfo
import com.ironsource.mediationsdk.logger.IronSourceError
import com.ironsource.mediationsdk.sdk.LevelPlayInterstitialListener

object IronSourceAd {

    const val NO_CACHE = 0
    const val HAS_CACHE = 1
    const val SHOW_FAIL = 2
    const val SHOW_SUCCESS = 3
    const val SHOW_CLOSE = 4
    const val TAG = "-================="
    const val adPlace1 = ""
    const val adPlace2 = ""
    const val adPlace3 = ""


   private fun setAdCallBack(listener: IronListener) {
        IronSource.setLevelPlayInterstitialListener(object : LevelPlayInterstitialListener {
            // Invoked when the interstitial ad was loaded successfully.
            // AdInfo parameter includes information about the loaded ad
            override fun onAdReady(adInfo: AdInfo) {
                listener.onLoaded()
                Log.d(TAG, "----onAdReady adInfo=${adInfo.instanceName}")

            }

            // Indicates that the ad failed to be loaded
            override fun onAdLoadFailed(error: IronSourceError) {
                listener.onLoadFail()
                Log.d(TAG, "----onAdLoadFailed error=${error.errorMessage}")
            }

            // Invoked when the Interstitial Ad Unit has opened, and user left the application screen.
            // This is the impression indication.
            override fun onAdOpened(adInfo: AdInfo) {
//                Log.d(TAG, "----onAdOpened adInfo=${adInfo.adUnit}")
            }

            // Invoked when the interstitial ad closed and the user went back to the application screen.
            override fun onAdClosed(adInfo: AdInfo) {
                listener.onClose()
//                Log.d(TAG, "----onAdClosed adInfo=${adInfo.adUnit}")
            }

            // Invoked when the ad failed to show
            override fun onAdShowFailed(error: IronSourceError, adInfo: AdInfo) {
                listener.onShowFail()
//                Log.d(TAG, "----onAdShowFailed error=${error.errorMessage}")
            }

            // Invoked when end user clicked on the interstitial ad
            override fun onAdClicked(adInfo: AdInfo) {
//                Log.d(TAG, "----onAdClicked adInfo=${adInfo.adUnit}")
            }

            // Invoked before the interstitial ad was opened, and before the InterstitialOnAdOpenedEvent is reported.
            // This callback is not supported by all networks, and we recommend using it only if
            // it's supported by all networks you included in your build.
            override fun onAdShowSucceeded(adInfo: AdInfo) {
                listener.onShowSuccess()
//                Log.d(TAG, "----onAdShowSucceeded adInfo=${adInfo.adUnit}")
            }
        })
    }


    @JvmStatic
    fun loadAd() {
        IronSource.loadInterstitial()
    }

    @JvmStatic
    fun showWelComeAd(totalTim: Long, activity: Activity, place: String, actionGoMain: () -> Unit):CountDownTimer {
        var needShow = true
        val myTimer = object : CountDownTimer(totalTim, 100) {
            override fun onTick(millisUntilFinished: Long) {
                if (needShow) {
                    showAd(activity, place) {
                        if (it == HAS_CACHE) {
                            needShow = false
                        }
                        if (it == SHOW_FAIL) {
                            needShow = true
                        }
                        if (it == SHOW_CLOSE) {
                            actionGoMain.invoke()
                        }
                    }
                }

            }

            override fun onFinish() {
                if (needShow) {
                    showAd(activity, place) {
                        if (it == SHOW_FAIL || it == NO_CACHE || it == SHOW_CLOSE) {
                            actionGoMain.invoke()
                        }

                    }
                }

            }
        }
        myTimer.start()

     return myTimer


    }

    @JvmStatic
    fun showAd(activity: Activity, place: String, action: (result: Int) -> Unit) {
        if (IronSource.isInterstitialReady()) {
            action.invoke(HAS_CACHE)
            Log.d(TAG, "----isInterstitialReady")
            setAdCallBack(object : IronListener {
                override fun onLoaded() {
                }

                override fun onLoadFail() {
                }

                override fun onClose() {
                    Log.d(TAG, "----onClose=" + Thread.currentThread().name)
                    loadAd()
                    action.invoke(SHOW_CLOSE)

                }

                override fun onShowFail() {
                    action.invoke(SHOW_FAIL)
                    Log.d(TAG, "----onShowFail=" + Thread.currentThread().name)
                }

                override fun onShowSuccess() {
                    action.invoke(SHOW_SUCCESS)
                }

            })
            IronSource.showInterstitial(activity, place)
        } else {
            Log.d(TAG, "----no cache")
            action.invoke(NO_CACHE)
        }


    }

}