package com.joyful.euphoria.board

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.ProgressBar
import com.ad.tradpluslibrary.TPAdManager
import com.joyful.euphoria.board.environment.MainActivity2
import com.joyful.euphoria.board.environment.hy.IdProvider
import com.joyful.euphoria.board.utils.Normal
import com.joyful.euphoria.board.xmlactivity.HomePageXML
import kotlin.math.roundToInt

/**
 * 不要修改启动页继承Activity这点
 */
class SplashActivity : Activity() {

    private lateinit var progressBar: ProgressBar
    private var countTime = 13000L
    private var timer: CountDownTimer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_begin_xml)
        if (IdProvider().getId() == 0L) {
            Normal.initFullScreen(this, true)
            progressBar = findViewById<ProgressBar>(R.id.nova_progress)
            init()
        } else {
            startActivity(Intent(this, MainActivity2::class.java))
            finish()
        }
    }

    private fun init() {
        TPAdManager.init(
            this,
            EuphoriaBoard.TAG,
            "0050AC0F82A15B4B5CCEC0E7A5B07A11",
            "5C25F9904EF67322F495A33F1303DB12",
            "02E0F4693311CCA2990355F2BEF35E12",
            "5E4C791BFA1E30844C2FE209130B2912"
        ) {

        }
        timer = TPAdManager.showWelcomeAd(this@SplashActivity, countTime, { millisUntilFinished ->
            val percentage: Float = 100 - millisUntilFinished.toFloat() / countTime * 100
            val round = percentage.roundToInt()
            progressBar.progress = round
        }) {
            progressBar.progress = 100
            toHome()
        }
        timer?.start()

    }

    private fun toHome() {
        startActivity(Intent(this, HomePageXML::class.java))
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (timer != null) {
            timer?.cancel()
        }
    }


}