package com.base.easyfilemanager.activity.photocompress.photo

import android.content.Intent
import android.graphics.Color
import android.os.Handler
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.core.view.updatePadding
import com.base.easyfilemanager.databinding.ActivityStartCompressionPhotoBinding
import com.base.easyfilemanager.helps.BaseActivity
import com.base.easyfilemanager.helps.ads.AdmobUtils
import com.base.easyfilemanager.utils.BarUtils
import kotlin.random.Random


class StartCompressionPhotoActivity : BaseActivity<ActivityStartCompressionPhotoBinding>() {

    override val binding: ActivityStartCompressionPhotoBinding by lazy {
        ActivityStartCompressionPhotoBinding.inflate(layoutInflater)
    }


    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                finishToMain()
            }
        })

        AdmobUtils.showNativeAd(this, binding.flAd)
    }

    override fun onStart() {
        super.onStart()
        checkPermission()
    }

    var isplay = 0
    override fun onPermissionsResult(isGranted: Boolean) {
        if (isGranted) {
            isplay += 1
            if (isplay == 1) {
                playLottie()
            }
        } else {
            finishToMain()
        }
    }

    private fun playLottie() {
        binding.idLottie1.imageAssetsFolder = "easy_imgcompress/images/"
        binding.idLottie1.setAnimation("easy_imgcompress/data.json")
        binding.idLottie1.playAnimation()
        Handler().postDelayed({
            AdmobUtils.showInterstitialAd(this) {
                val mList = ImagesMediaUtils.getAlbums(this)
                if (mList.isNotEmpty()) {
                    startActivity(Intent(this, CompressionPhotoListActivity::class.java))
                    finish()
                } else {
                    Toast.makeText(this, "Album photos not detected, no compression required.", Toast.LENGTH_SHORT).show()
                }
            }
        }, Random.nextLong(2500, 4000))
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.idLottie1.clearAnimation()
    }
}