package com.wall.photography.wallpaper.activivty

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import com.wall.photography.wallpaper.MyApp
import com.wall.photography.wallpaper.R
import com.wall.photography.wallpaper.databinding.ActivityMainBinding
import com.wall.photography.wallpaper.databinding.ActivityWelcomeBinding
import com.wall.photography.wallpaper.topon.AdManager
import kotlin.math.roundToInt

class MyWelComeActivity : AppCompatActivity() {

    private lateinit var vb: ActivityWelcomeBinding
    private var goTimer: CountDownTimer? = null
    private val countTime = 11000L
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        vb = ActivityWelcomeBinding.inflate(layoutInflater)
        setContentView(vb.root)
        init()
    }

    private fun init() {

        goTimer =
            AdManager.showWelcomeAd(this@MyWelComeActivity, countTime, { millisUntilFinished ->
                val percentage: Float = 100 - millisUntilFinished.toFloat() / countTime * 100
                val round = percentage.roundToInt()
                vb.progressbar.progress = round
                if (AdManager.place1LoadFail && AdManager.place2LoadFail && AdManager.place3LoadFail) {
                    goTimer?.cancel()
                    enterMain()
                    Log.d(MyApp.TAG, "-------------cancel")
                }
            }) {
                enterMain()
            }

        goTimer?.start()
    }

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

    override fun onDestroy() {
        super.onDestroy()
        goTimer?.cancel()
    }

}