package com.example.prettywallpapers.toponad

import android.app.Activity
import android.os.CountDownTimer
import android.util.Log
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.anythink.interstitial.api.ATInterstitial
import com.anythink.interstitial.api.ATInterstitialListener
import com.example.prettywallpapers.PrettyWallPaper
import com.example.prettywallpapers.myvalue.MyValues


object AdManager {


    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 = "n66a247222a867"
    const val place2Id = "n66a24721a7080"
    const val place3Id = "n66a247212ee08"

    val list = mutableListOf<ATInterstitial>()


    @JvmStatic
    fun loadAllAd() {
        if (list.size <= 0) {
            val mInterstitialAd1 = ATInterstitial(MyValues.prettyWallPaper, place1Id)
            val mInterstitialAd2 = ATInterstitial(MyValues.prettyWallPaper, place2Id)
            val mInterstitialAd3 = ATInterstitial(MyValues.prettyWallPaper, place3Id)
            list.add(mInterstitialAd1)
            list.add(mInterstitialAd2)
            list.add(mInterstitialAd3)
        }
        for (ad in list) {
            if (!ad.isAdReady) {
                setCallBack(ad,object :AdListener{
                    override fun showSuccess() {

                    }

                    override fun showFail() {

                    }

                    override fun showClose() {

                    }

                })
                ad.load()
            }
        }


    }
    @JvmStatic
    fun getReadyAd(): ATInterstitial? {
        list.shuffle()
        for (ad in list) {
            if (ad.isAdReady) {
                return ad
            }
        }
        return null
    }


    @JvmStatic
    fun showWelcomeAd(activity: Activity,totalTim: Long,action:(Long)->Unit, goMain: () -> Unit): CountDownTimer {
        var alreadyShow = false
        var timer = object : CountDownTimer(totalTim, 100) {
            override fun onTick(millisUntilFinished: Long) {
                action.invoke(millisUntilFinished)
                if (!alreadyShow) {
                    showAD(activity) {
                        if (it == type_has_cache) {
                            alreadyShow = true
                        }
                        if (it == type_show_close || it == type_show_fail) {
                            goMain.invoke()
                        }

                    }
                }

            }

            override fun onFinish() {
                if (!alreadyShow) {
                    showAD(activity) {
                        if (it == type_show_close || it == type_show_fail || it == type_no_cache) {
                            goMain.invoke()
                        }
                    }
                }
            }

        }
        return timer
    }

    private fun setCallBack(ad: ATInterstitial, listener: AdListener) {
        ad.setAdListener(object : ATInterstitialListener {
            override fun onInterstitialAdLoaded() {
                Log.d(PrettyWallPaper.TAG, "LoadLoaded  ${ad.mPlacementId}")
            }

            override fun onInterstitialAdLoadFail(p0: AdError?) {
                Log.d(PrettyWallPaper.TAG, "LoadFail:${p0?.code}  ${p0?.desc}")
            }

            override fun onInterstitialAdClicked(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdShow(p0: ATAdInfo?) {
                Log.d(PrettyWallPaper.TAG, "AdShow  ${p0?.showId}   ")
                listener.showSuccess()
                ad.load()
            }

            override fun onInterstitialAdClose(p0: ATAdInfo?) {
                listener.showClose()
            }

            override fun onInterstitialAdVideoStart(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdVideoEnd(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdVideoError(p0: AdError?) {
                listener.showFail()
            }

        })
    }


    @JvmStatic
    fun showAD(activity: Activity, action: (type: Int) -> Unit) {
        val readyAd = getReadyAd()
        if (readyAd!= null) {
            Log.d(PrettyWallPaper.TAG, "readyAd  ${readyAd.mPlacementId}   ")
            action.invoke(type_has_cache)
            setCallBack(readyAd,object : AdListener {
                override fun showSuccess() {
                    action.invoke(type_show_success)
                }

                override fun showFail() {
                    action.invoke(type_show_fail)
                }

                override fun showClose() {
                    action.invoke(type_show_close)
                }

            })
            readyAd.show(activity)
        } else {
            action.invoke(type_no_cache)

        }
    }
}