package com.quiescent.wallpaper.collection.test.ui

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.applovin.mediation.ads.MaxInterstitialAd
import com.quiescent.wallpaper.collection.test.InterstitialManager
import com.quiescent.wallpaper.collection.test.InterstitialManager.setMyCallBack
import com.quiescent.wallpaper.collection.test.WallpaperCollection
import com.quiescent.wallpaper.collection.test.databinding.ActivityLauncherBinding
import java.util.Timer
import java.util.TimerTask

class ActivityLauncher : BaseActivity<ActivityLauncherBinding>() {

    private lateinit var localBroadcastManager: LocalBroadcastManager

    private lateinit var timer: Timer
    private var count: Long = 0L

    private var countTime: Long = 0L

    private var interval: Long = 500L

    private var hasShowAd = false

    private lateinit var adList: List<MaxInterstitialAd>
    override fun getViewBinding(): ActivityLauncherBinding {
        return ActivityLauncherBinding.inflate(layoutInflater)
    }

    override fun onViewUi() {
        timer = Timer()
        localBroadcastManager = LocalBroadcastManager.getInstance(this)
        checkSDKInit()
    }


    private fun checkSDKInit() {
        if (!WallpaperCollection.INIT_SUCCESS) {
            localBroadcastManager.registerReceiver(object : BroadcastReceiver() {
                override fun onReceive(context: Context?, intent: Intent?) {
                    loadAd()
                }

            }, IntentFilter(WallpaperCollection.INIT_SUCCESS_ACTION))

        } else {
            loadAd()
        }
    }

    private fun loadAd() {
        Log.d("-------------","-----------load")
        adList = InterstitialManager.startLoadAd(this@ActivityLauncher)
        timer.schedule(object : TimerTask() {
            override fun run() {
                count++
                countTime = count * interval
                if (!hasShowAd) {
                    val readyAd = InterstitialManager.getReadyAd(adList)
                    if (readyAd == null) {
                        if (countTime >= 12000) {
                            Log.d("-------------","-----------countTime=$countTime")
                            timer.cancel()
                            goMain()
                        }
                    } else {
                        hasShowAd = true
                        timer.cancel()
                        readyAd.setMyCallBack(object : InterstitialManager.MyCallBack {
                            override fun adShowFail() {
                                Log.d("-------------","-----------adShowFail=")
                                goMain()
                            }

                            override fun adShowHidden() {
                                Log.d("-------------","-----------adShowHidden=")
                                goMain()
                            }

                        })
                        readyAd.showAd()
                    }
                }
            }

        }, 0, interval)
    }

    private fun goMain() {
        startActivity(Intent(this@ActivityLauncher, MainActivity::class.java))
        finish()
    }


}