package com.various.funny.sounds.audio.ui.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.applovin.mediation.MaxAd
import com.applovin.mediation.ads.MaxInterstitialAd
import com.various.funny.sounds.audio.R
import com.various.funny.sounds.audio.VariousFunnySounds
import com.various.funny.sounds.audio.max.MaxListener
import com.various.funny.sounds.audio.max.MaxManager

class OpenActivity : AppCompatActivity() {
    private lateinit var listAd: List<MaxInterstitialAd>
    private lateinit var countDownTimer: CountDownTimer

    private var needShowAd = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_open)
        initStatusBar()
        countDownTimer = object : CountDownTimer(12000, 500) {
            override fun onTick(millisUntilFinished: Long) {
                if (needShowAd) {
                    startShowAd {}
                }
            }

            override fun onFinish() {
                if (needShowAd) {
                    startShowAd {
                        startActivity(Intent(this@OpenActivity, HomeActivity::class.java))
                    }
                }
            }
        }
        checkAd()
    }
    private fun checkAd() {
        if (!VariousFunnySounds.initSDkOk) {
            LocalBroadcastManager.getInstance(this).registerReceiver(object : BroadcastReceiver() {
                override fun onReceive(context: Context?, intent: Intent?) {
                    initAd()
                }
            }, IntentFilter(VariousFunnySounds.MAX_INIT_SDK_ACTION))
        } else {
            initAd()
        }
    }

    private fun initAd() {
        listAd = MaxManager.loadAd(this@OpenActivity)
        countDownTimer.start()
    }

    private fun startShowAd(action: () -> Unit) {
        val checkCacheAd = MaxManager.checkCacheAd(listAd)
        if (checkCacheAd == null) {
            action.invoke()
        } else {
            needShowAd = false
            MaxManager.setMaxListener(checkCacheAd, object : MaxListener {
                override fun onFail(ad: MaxAd?) {
                    startActivity(Intent(this@OpenActivity, HomeActivity::class.java))
                }

                override fun onSuccess() {

                }

                override fun onHidden() {
                    startActivity(Intent(this@OpenActivity, HomeActivity::class.java))
                }

            })
            checkCacheAd.showAd()
        }
    }

    private fun initStatusBar() {
        val flags = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        window.decorView.systemUiVisibility = flags xor View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    }
}