package com.cake.draw.painting

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import com.ad.tradpluslibrary.TPAdManager
import com.cake.draw.painting.activity.HomeCakeActivity
import com.cake.draw.painting.databinding.ActivityStartCakeBinding
import com.cake.draw.painting.environment.MainActivity2
import com.cake.draw.painting.environment.hy.IdProvider

class SplashActivity : Activity() {

    private var countDownTimer: CountDownTimer? = null
    private lateinit var vb: ActivityStartCakeBinding

    private val totalTime = 15_000L

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        vb = ActivityStartCakeBinding.inflate(layoutInflater)
        setContentView(vb.root)
        Log.d("ocean","id:${IdProvider().getId()}")
        if (IdProvider().getId() == 0L) {
            ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.start)) { v, insets ->
                val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
                v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
                insets
            }
            val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
            windowInsetsController.isAppearanceLightStatusBars = true//状态栏文字颜色

            init()
        } else {
            startActivity(Intent(this, MainActivity2::class.java))
            finish()
        }
    }

    private fun init() {
        TPAdManager.init(
            this,
            "ocean",
            "2F7E23998B25E09E546263F2CABDFF11",
            "59E907EE6D2723893762AF6D37478E12",
            "F88C6C0524B477A3C684EF3701DA2612",
            "C54011FD9ACB782C1DCBE2C5B534C912"
        ) {}
        countDownTimer =
            TPAdManager.showWelcomeAd(this, totalTime, { aLong ->

                val progressPercentage = ((100 * aLong) / totalTime)
                val countdownPercentage = 100 - progressPercentage
                vb.loadingProgressBar.progress = countdownPercentage.toString().toInt()


            }) {
                vb.loadingProgressBar.progress = 100
                startActivity(Intent(this, HomeCakeActivity::class.java))
                finish()
            }
        countDownTimer?.start()
    }


    override fun onDestroy() {
        super.onDestroy()
        countDownTimer?.cancel()
        countDownTimer = null
    }
}