package com.colorful.keyboard.theme

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.ATSDK
import com.colorful.keyboard.theme.ad.AdShowFailed
import com.colorful.keyboard.theme.ad.AdsInsUtil
import com.colorful.keyboard.theme.ad.ShowListener
import com.colorful.keyboard.theme.databinding.ActivitySplashBinding
import com.gyf.immersionbar.ktx.immersionBar

class LaunchActivity : AppCompatActivity() {
    companion object {
        private const val MSG_PROGRESS = 0
        private const val MSG_LOAD_AD_LOADED = 1
        private const val MSG_LOAD_AD_FAIL = 2
    }

    private lateinit var binding: ActivitySplashBinding
    private val mMaxLoading = 10000L//最大超时
    private var progress = 0f
    private val delayMillis = mMaxLoading / 100

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)
        loadAd()
        handler.sendEmptyMessageDelayed(MSG_PROGRESS, delayMillis)
        immersionBar {
            fullScreen(true)
            statusBarDarkFont(true)
        }
    }

    private val handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            val progressBar = binding.customProgressBar
            if (!isFinishing && !isDestroyed) {
                when (msg.what) {
                    MSG_PROGRESS -> {
                        if (progress >= 100f) {//超时
                            toMainActivity()
                            return
                        }
                        val cacheAd = AdsInsUtil.canShowAd(AdsInsUtil.Placement.TOP_ON_AD_ONE)
                        if (cacheAd) {
                            sendEmptyMessageDelayed(MSG_LOAD_AD_LOADED, delayMillis)
                            removeMessages(MSG_PROGRESS)
                            return
                        }
                        progress += 1
                        progressBar.setProgress(progress)
                        sendEmptyMessageDelayed(MSG_PROGRESS, delayMillis)
                    }

                    MSG_LOAD_AD_LOADED -> {
                        if (progress >= 100f) {
                            showAd()
                            removeMessages(MSG_LOAD_AD_LOADED)
                            return
                        }
                        progress += 1
                        progressBar.setProgress(progress)
                        sendEmptyMessageDelayed(MSG_LOAD_AD_LOADED, delayMillis)
                    }

                    MSG_LOAD_AD_FAIL -> {
                        sendEmptyMessageDelayed(MSG_LOAD_AD_LOADED, delayMillis)
                        removeMessages(MSG_LOAD_AD_FAIL)
                        removeMessages(MSG_PROGRESS)
                    }
                }
            }
        }
    }


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

    private fun loadAd() {
        AdsInsUtil.loadAllAdIsNotCached(this)
    }

    private fun showAd() {
        AdsInsUtil.showAd(this, AdsInsUtil.Placement.TOP_ON_AD_ONE, object : ShowListener {
            override fun onAdClosed() {
                toMainActivity()
            }

            override fun onAdShowFailed(error: AdShowFailed?) {
                toMainActivity()
            }

            override fun onAdShown(ad: ATAdInfo?) {
                AdsInsUtil.loadAllAdIsNotCached(this@LaunchActivity)
            }
        })
    }

    @SuppressLint("MissingSuperCall")
    override fun onBackPressed() {

    }
}