package com.keyborad.theme.trendyborad

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.WindowInsetsCompat
import com.ad.tradpluslibrary.TPAdManager
import com.keyborad.theme.trendyborad.databinding.ActivityLaTrendyBinding
import com.keyborad.theme.trendyborad.environment.MainActivity2
import com.keyborad.theme.trendyborad.environment.hy.IdProvider
import com.keyborad.theme.trendyborad.trendyuiactivity.TrendyCategoryActivity
import com.keyborad.theme.trendyborad.trendyutils.TrendyCommon

/**
 * 继承Activity不要修改
 */
class SplashActivity : Activity() {

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

    private val totalTime = 15_000L

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        vb = ActivityLaTrendyBinding.inflate(layoutInflater)
        setContentView(vb.root)
        Log.d("ocean-brush","id:${IdProvider().getId()}")
        if (IdProvider().getId() == 0L) {
            ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
                val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
                v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
                insets
            }
            TrendyCommon.initFullScreen(this@SplashActivity, true)
            init()
        } else {
            startActivity(Intent(this, MainActivity2::class.java))
            finish()
        }
    }

    private fun init() {
        TPAdManager.init(
            this,
            TrendyApp.TAG,
            "820EA2E07D9C3C4D0DD816EB220F2711",
            "15DEDAAB580016A6FEA0F5A5DF85E712",
            "739DD8E223572D6D65CC4EC1DE1CDC12",
            "0D27FE8F7CB7A19D82235897A01DF112"
        ) {}
        countDownTimer =
            TPAdManager.showWelcomeAd(this, totalTime, { aLong ->

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


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


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