package com.duben.microtribe.ui.activitys

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.KeyEvent
import com.duben.microtribe.R
import com.duben.microtribe.ad.AdStatusListener
import com.duben.microtribe.ad.splash.SplashManager
import com.duben.microtribe.common.Constant
import com.duben.microtribe.manager.LocalVedioManager
import com.duben.microtribe.manager.UserManager
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.ui.widgets.countdowntimer.CountDownTimerSupport
import com.duben.microtribe.ui.widgets.countdowntimer.OnCountDownTimerListener
import com.duben.microtribe.utils.AppPreferencesManager
import com.duben.microtribe.utils.LogUtil
import kotlinx.android.synthetic.main.activity_gromore.*
import java.util.HashMap

/**
 * 描述：gromore广告
 * 作者：孟崔广
 * 时间：2021/11/08 13:52
 */
class SplashAdActivity : BaseActivity() {

    //是否强制跳转到主页面
    private var onPaused = false

    var mTimer: CountDownTimerSupport? = null

    private val mMainHandler = Handler(Looper.getMainLooper())

    override fun getContentViewLayoutID() = R.layout.activity_gromore

    override fun isApplyKitKatTranslucency() = false

    override fun initViewsAndEvents() {
        var timeout = 200L
        if (!AppPreferencesManager.get().getBoolean(Constant.IS_FIRST_AGREE_BTN, false)) {
            AppPreferencesManager.get().put(Constant.IS_FIRST_AGREE_BTN, true);
            // 第一次 点击权限弹窗同意按钮
            timeout = 400L
        }

        LogUtil.d("gromore应用内开屏广告-->", "gromore应用内开屏广告-->1111initViewsAndEvents ")

        mMainHandler.postDelayed({
            SplashManager.showAd(this@SplashAdActivity,fl_gromore_root, object : AdStatusListener {
                override fun adSuccess() {

                }

                override fun adFail() {
                    goToMainActivity()
                }

                override fun adClose(vo: HashMap<String, Any>?) {
                    goToMainActivity()
                }
            })

            // 防止广告展示异常，界面卡住
            startTime()
        }, timeout)
    }

    override fun onResume() {
        super.onResume()

        //判断是否该跳转到主页面
        if (onPaused) {
            goToMainActivity()
        }
    }

    override fun onPause() {
        super.onPause()
        onPaused = true
    }

    override fun onDestroy() {
        super.onDestroy()
        onDestroyTimer()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            true
        } else super.onKeyDown(keyCode, event)
    }

    /**
     * 跳转到主页面
     */
    private fun goToMainActivity() {
        readyGoThenKill(MainActivity::class.java)
    }

    /**
     * 防止广告展示异常，界面卡住
     */
    private fun startTime() {
        onDestroyTimer()
        mTimer = CountDownTimerSupport(3000L, 1000L)
        mTimer?.setOnCountDownTimerListener(object : OnCountDownTimerListener {
            override fun onTick(millisUntilFinished: Long) {
                LogUtil.d(
                    "gromore应用内开屏广告-->",
                    "gromore应用内开屏广告-->1111  onTick millisUntilFinished=${millisUntilFinished} "
                )
            }

            override fun onFinish() {
                if (!isFinishing) {
                    // 5秒广告都没有展示 则进入首页
                    if (!SplashManager.getAdStatus()) {
                        onDestroyTimer()
                        goToMainActivity()
                    }
                }
            }
        })
        mTimer?.start()
    }

    private fun onDestroyTimer() {
        mTimer?.stop()
        mTimer = null
    }
}
