package com.soundapp.soundgags.ui

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import androidx.appcompat.app.AppCompatActivity
import com.ad.tradpluslibrary.TPAdManager
import com.soundapp.soundgags.ReloadApplication
import com.soundapp.soundgags.databinding.ActivityJokeViewBinding
import com.soundapp.soundgags.setting.TopBarUtils

class JokeViewerActivity : AppCompatActivity() {
    private lateinit var binding : ActivityJokeViewBinding
    private var handler: Handler = Handler()
    private var countDownTimer: CountDownTimer? = null
    private  var time = 14000L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        TopBarUtils.setStatusBar(this.window)
        TopBarUtils.setLightStatusBar(this.window, true)
        binding = ActivityJokeViewBinding.inflate(layoutInflater)
        setContentView(binding.root)
        TPAdManager.init(
            this@JokeViewerActivity,
            ReloadApplication.TAG,
            "7798CDAEFBC2B1541B8B57EC7CC47F11",
            "8E399B2604C0EF55A6625E21F4B46712",
            "CE1D9ADA9743A8C426104487344AF712",
            "8641E7001CB301CE14BAE457B5570712"
        ) {
            null
        }
        countDownTimer = TPAdManager.showWelcomeAd(
            this@JokeViewerActivity,
            time,
            { aLong ->
                val progressPercentage = (100 * aLong) / time
                val percentage = 100 - progressPercentage
                binding.loadingPb.progress = percentage.toInt()
            },
            {
                binding.loadingPb.progress = 100
                val intent= Intent(
                    this@JokeViewerActivity,
                    CompleteListActivity::class.java
                )
                startActivity(intent)
                finish()
            }
        )
        countDownTimer?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)

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