package com.test.basd.smartjunkcleaner.activity.photocompress.photo

import android.content.Intent
import android.graphics.Color
import android.os.Handler
import androidx.activity.OnBackPressedCallback
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ToastUtils
import com.test.basd.smartjunkcleaner.databinding.ActivityStartCompressionPhotoBinding
import com.test.basd.smartjunkcleaner.helps.BaseActivity
import com.test.basd.smartjunkcleaner.helps.ads.AdmobUtils
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()
            }
        })

    }

    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 = "ya_sjunk_suo/images/"
        binding.idLottie1.setAnimation("ya_sjunk_suo/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 {
                    ToastUtils.showShort("Album photos not detected, no compression required.")
                }
            }
        },Random.nextLong(3000,4000))
    }


}