package com.cartoon.wallpapers.live.topon

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.cartoon.wallpapers.live.MyWallpaperApp


object AdManager {

    //上次广告展示时刻
    var LAST_AD_SHOW = 0L

    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

    /**
    Cartoon_INTER_3
    n684be051d24b6
    Cartoon_INTER_2
    n684be04164754
    Cartoon_INTER_1
    n684be034def39
     */

    const val place1Id = "n684be051d24b6"
    const val place2Id = "n684be04164754"
    const val place3Id = "n684be034def39"


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

    val list = mutableListOf<ATInterstitial>()
     val mContext = MyWallpaperApp.getApplication()
    val mTAG = MyWallpaperApp.TAG
    @JvmStatic
    fun loadAllAd() {
        if (list.size <= 0) {
            val mInterstitialAd1 = ATInterstitial(mContext, place1Id)
            val mInterstitialAd2 = ATInterstitial(mContext, place2Id)
            val mInterstitialAd3 = ATInterstitial(mContext, place3Id)
            list.add(mInterstitialAd1)
            list.add(mInterstitialAd2)
            list.add(mInterstitialAd3)
        }
        for (ad in list) {
            if (!ad.isAdReady) {
                setCallBack(ad, object : AdListener {
                    override fun loadFail(placeId: String) {
                        if (placeId == place1Id) {
                            place1LoadFail = true
                        }
                        if (placeId == place2Id) {
                            place2LoadFail = true
                        }
                        if (placeId == place3Id) {
                            place3LoadFail = true
                        }
                    }

                    override fun showSuccess() {

                    }

                    override fun showFail() {

                    }

                    override fun showClose() {

                    }

                })
                ad.load()
            }
        }


    }

    @JvmStatic
    fun getReadyAd(): ATInterstitial? {
        list.shuffle()
        for (ad in list) {
            if (ad.isAdReady) {
                Log.d(mTAG, "-cache------------")
                return ad
            }
        }
        Log.d(mTAG, "-no cache------------")
        return null
    }


    @JvmStatic
    fun showWelcomeAd(
        activity: Activity,
        totalTim: Long,
        countAction: (millisUntilFinished: Long) -> Unit,
        goMain: () -> Unit
    ): CountDownTimer {
        var alreadyShow = false
        var timer = object : CountDownTimer(totalTim, 100) {
            override fun onTick(millisUntilFinished: Long) {
                countAction.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(mTAG, "LoadLoaded  ${ad.mPlacementId}")
            }

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

            override fun onInterstitialAdClicked(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdShow(p0: ATAdInfo?) {
                Log.d(mTAG, "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
    private fun showAD(activity: Activity, action: (type: Int) -> Unit) {
        val readyAd = getReadyAd()
        if (readyAd != null) {
            Log.d(mTAG, "readyAd  ${readyAd.mPlacementId}   ")
            action.invoke(type_has_cache)
            setCallBack(readyAd, object : AdListener {
                override fun loadFail(placeId: String) {

                }

                override fun showSuccess() {
                    action.invoke(type_show_success)
                    LAST_AD_SHOW = System.currentTimeMillis()
                }

                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)

        }
    }

    @JvmStatic
    fun showTopOn(activity: Activity, listener: onActionListener? = null) {
        showAD(activity) { type ->
            if (type == type_no_cache || type == type_show_close || type == type_show_fail) {
                listener?.onAction()
            }
        }
    }
}