package com.design.wallpapergallery.view

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import androidx.appcompat.app.AppCompatActivity
import com.ad.tradpluslibrary.TPAdManager
import com.design.wallpapergallery.BasicActivity
import com.design.wallpapergallery.IApplication
import com.design.wallpapergallery.databinding.ActivityStartProgressBinding

class StartProgressActivity : AppCompatActivity() {
    lateinit var binding: ActivityStartProgressBinding
    private var countDownTimer: CountDownTimer? = null
    private var time = 13000L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityStartProgressBinding.inflate(layoutInflater)
        setContentView(binding.root)
        TPAdManager.init(
            this@StartProgressActivity,
            IApplication.TAG,
            "70E883D08C8D3ACA863B20E0C5CBF011",
            "7B5ACED11900515030BB9A3C0D6F6812",
            "D42B81ACACC02652DBD080DF0069B812",
            "FA341D48027596C8C4F86B3446A7FA12"
        ) {
            null
        }

        countDownTimer = TPAdManager.showWelcomeAd(
            this@StartProgressActivity,
            time,
            { aLong ->
                val progressPercentage = (100 * aLong) / time
                val percentage = 100 - progressPercentage
                binding.progressbar.progress = percentage.toInt()
            },
            {
                val intent= Intent(
                    this@StartProgressActivity,
                    BasicActivity::class.java
                )
                startActivity(intent)
                finish()
            }
        )
        countDownTimer?.start()

    }

    override fun onDestroy() {
        super.onDestroy()

        if (countDownTimer != null) {
            countDownTimer!!.cancel()
        }
    }
}