package com.apps.quicksketchtrace.ad

import android.app.Activity
import android.os.CountDownTimer
import android.util.Log
import com.tradplus.ads.base.bean.TPAdError
import com.tradplus.ads.base.bean.TPAdInfo
import com.tradplus.ads.open.interstitial.InterstitialAdListener
import com.tradplus.ads.open.interstitial.TPInterstitial


object TPManager {


    const val type_no_cache = 0
    const val type_has_cache = 1
    const val type_show_success = 2
    const val type_show_close = 3
    const val type_show_fail = 4



    const val place1Id = "04F2A22A78D2075FF033C215C592982C"
    const val place2Id = "7B2314DD74A89187490A4E8A85A5D750"
    const val place3Id = "1657ABADE1AB9F07D1D4E8D9ACFEA5C2"


    var place1LoadFail = false
    var place2LoadFail = false
    var place3LoadFail = false

    val list = mutableListOf<TPInterstitial>()

    val mTAG = "TP AD"


    fun loadAllAd(activity: Activity) {
        if (list.size <= 0) {
            place1LoadFail = false
            place2LoadFail = false
            place3LoadFail = false
            val mInterstitialAd1 = TPInterstitial(activity, place1Id)
            val mInterstitialAd2 = TPInterstitial(activity, place2Id)
            val mInterstitialAd3 = TPInterstitial(activity, place3Id)
            list.add(mInterstitialAd1)
            list.add(mInterstitialAd2)
            list.add(mInterstitialAd3)
        }
        for (ad in list) {
            if (!ad.isReady) {
                setCallBack(ad, object : AdListener {
                    override fun loadFail() {

                    }
                })
                ad.loadAd()
            }
        }
    }


    fun getReadyAd(): TPInterstitial? {
        list.shuffle()
        for (ad in list) {
            if (ad.isReady) {
                Log.d(mTAG, "-cache------------")
                return ad
            }
        }
        return null
    }


    fun showWelcomeAd(
        activity: Activity,
        totalTim: Long,
        countAction: (millisUntilFinished: Long) -> Unit,
        goMain: () -> Unit
    ): CountDownTimer {
        var alreadyShow = false
        var timer: CountDownTimer? = null
        timer = object : CountDownTimer(totalTim, 100) {
            override fun onTick(millisUntilFinished: Long) {
                countAction(millisUntilFinished)
                Log.d(mTAG, "onTick ${Thread.currentThread().name}  timer=${timer}")
                if (!alreadyShow) {
                    showCheck(activity) {
                        if (it == type_has_cache) {
                            alreadyShow = true
                        }
                        if (it == type_show_close || it == type_show_fail) {
                            Log.d(
                                mTAG,
                                "goMain 111 ${Thread.currentThread().name}   "
                            )
                            goMain()
                        }

                    }
                }
            }

            override fun onFinish() {
                Log.d(mTAG, "onFinish()  ${Thread.currentThread().name}   ")
                if (!alreadyShow) {
                    showCheck(activity) {
                        if (it == type_show_close || it == type_show_fail || it == type_no_cache) {
                            Log.d(
                                mTAG,
                                "goMain 222 ${Thread.currentThread().name}   "
                            )
                            goMain()
                        }
                    }
                }
            }
        }
        return timer
    }


    private fun setCallBack(ad: TPInterstitial, listener: AdListener) {
        ad.setAdListener(object : InterstitialAdListener {
            override fun onAdLoaded(p0: TPAdInfo?) {
                Log.d(mTAG, "----onAdLoaded  ${p0?.tpAdUnitId}")

            }

            override fun onAdFailed(p0: TPAdError?) {
                listener.loadFail()
                Log.d(mTAG, "----onAdFailed  code=${p0?.errorCode}  msg=${p0?.errorMsg}")
            }

            override fun onAdImpression(p0: TPAdInfo?) {
                listener.showSuccess()
                Log.d(mTAG, "----onAdImpression  ${p0?.tpAdUnitId}")
            }

            override fun onAdClicked(p0: TPAdInfo?) {

            }

            override fun onAdClosed(p0: TPAdInfo?) {
                listener.showClose()
            }

            override fun onAdVideoError(
                p0: TPAdInfo?,
                p1: TPAdError?
            ) {

            }

            override fun onAdVideoStart(p0: TPAdInfo?) {

            }

            override fun onAdVideoEnd(p0: TPAdInfo?) {

            }


        })
    }


    private fun showCheck(activity: Activity, action: (type: Int) -> Unit) {
        val readyAd = getReadyAd()
        if (readyAd != null) {
            Log.d(mTAG, "readyAd    ")
            action(type_has_cache)
            setCallBack(readyAd, object : AdListener {
                override fun loadFail() {

                }

                override fun showSuccess() {
                    action(type_show_success)
                }

                override fun showFail() {
                    action(type_show_fail)
                }

                override fun showClose() {
                    action(type_show_close)
                }

            })
            if (readyAd.isReady) {
                readyAd.showAd(activity, null)
            }
        } else {
            action(type_no_cache)
        }
    }


    fun showTPAD(activity: Activity, listener: () -> Unit) {
        showCheck(activity) { type ->
            if (type == type_no_cache || type == type_show_close || type == type_show_fail) {
                listener()
            }
        }
    }
}