package com.gh.gamecenter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import com.gh.ad.AdDelegateHelper
import com.gh.ad.AdDelegateHelper.requestSplashAd
import com.gh.ad.AdDelegateHelper.shouldShowStartUpAd
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.SPUtils
import java.util.*

/**
 * 开屏广告（热启动）
 */
class SplashAdActivity : BaseActivity() {
    private var mCountdownCount = 0
    override fun getLayoutId(): Int = R.layout.activity_splash_ad

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        overridePendingTransition(0, 0)
        DisplayUtils.transparentStatusBar(this)
        showSplashAd()
    }

    /**
     * 显示开屏广告
     */
    private fun showSplashAd() {
        if (shouldShowStartUpAd(true)) {
            val startAdContainer = findViewById<ViewGroup>(R.id.startAdContainer)
            val sdkStartAdContainer = findViewById<ViewGroup>(R.id.sdkStartAdContainer)
            val sdkJumpBtn = findViewById<TextView>(R.id.sdkJumpBtn)
            val adsFl = findViewById<FrameLayout>(R.id.adsFl)
            val icpContainer = findViewById<View>(R.id.sdkStartAdIcpContainer)

            icpContainer?.setOnClickListener {
                // consume click event
            }

            val screenWidthInPx = DisplayUtils.getScreenWidth(this)
            val screenHeightInPx = (DisplayUtils.getScreenHeight(this)
                    + DisplayUtils.getStatusBarHeight(this.resources)
                    - 112F.dip2px())
            val screenWidthInDp = DisplayUtils.px2dip(this, screenWidthInPx.toFloat()).toFloat()
            val screenHeightInDp = DisplayUtils.px2dip(this, screenHeightInPx.toFloat()).toFloat()
            requestSplashAd(
                this,
                screenWidthInPx,
                screenHeightInPx,
                screenWidthInDp,
                screenHeightInDp,
                startAdContainer!!,
                sdkStartAdContainer!!,
                sdkJumpBtn!!,
                adsFl!!,
                (mBaseHandler as BaseHandler),
                true
            ) {
                finishActivity()
                null
            }
        } else {
            finishActivity()
        }
    }

    private fun finishActivity() {
        finish()
        overridePendingTransition(0, 0)
    }

    override fun handleBackPressed(): Boolean {
        return true
    }

    override fun handleMessage(msg: Message) {
        super.handleMessage(msg)
        if (msg.what == MainActivity.COUNTDOWN_AD || msg.what == MainActivity.COUNTDOWN_SDK_AD) {
            mCountdownCount++
            val maxCount = if (msg.what == MainActivity.COUNTDOWN_AD) {
                COUNTDOWN_MAX_COUNT
            } else {
                MainActivity.COUNTDOWN_SDK_MAX_COUNT
            }
            if (maxCount < mCountdownCount) {
                AdDelegateHelper.isShowingSplashAd = false
                if (msg.what == MainActivity.COUNTDOWN_SDK_AD) {
                    SPUtils.setLong(Constants.SP_LAST_SPLASH_AD_SHOW_TIME, System.currentTimeMillis())
                }
                finishActivity()
            } else {
                val jumpBtn = findViewById<TextView>(if (msg.what == MainActivity.COUNTDOWN_AD) R.id.jumpBtn else R.id.sdkJumpBtn)
                jumpBtn?.text = getString(R.string.splash_jump, maxCount - mCountdownCount)
                mBaseHandler.sendEmptyMessageDelayed(msg.what, 1000)
            }
        }
    }

    companion object {
        private const val COUNTDOWN_MAX_COUNT = 3

        @JvmStatic
        fun getIntent(context: Context): Intent {
            return Intent(context, SplashAdActivity::class.java)
        }
    }
}