package com.led.scroller.barrage.banner.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import android.view.WindowManager
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import com.ad.tradpluslibrary.TPAdManager
import com.led.scroller.barrage.banner.LBApplication

import com.led.scroller.barrage.banner.R
import kotlin.jvm.java
import kotlin.math.roundToInt

class SplashActivity : AppCompatActivity() {

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
         initFullScreen(this,false)
        init()
        findView()
    }

    private fun init() {
        TPAdManager.init(
            this,
            LBApplication.TAG,
            "899EFE2615AE2209B50C709D28C9E411",
            "B2EF79773F1D3AF7916FFFE756F39412",
            "8DA8AF9FCE5DCB6BDFA166152706E612",
            "4485F52811D3029D661F2E12B3D02212"
        ) {

        }
        timer = TPAdManager.showWelcomeAd(this, 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 findView() {
        progressBar = findViewById<ProgressBar>(R.id.nova_progress)
    }


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

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }

    fun initFullScreen(activity: Activity, dark: Boolean? = true) {
        val window = activity.window
        val decorView = window.decorView
        val rootView = decorView.rootView
//
        if (dark == null) return

        if (dark) {
            decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            )
        } else {
            decorView.setSystemUiVisibility(
                (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
            )
        }
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    }
}