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

import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Handler
import android.provider.Settings
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.fastcleanerjunk.activity.PermissionTripActivity
import com.test.basd.fastcleanerjunk.databinding.ActivityStartCompressionPhotoBinding
import com.test.basd.fastcleanerjunk.helps.BaseActivity
import com.test.basd.fastcleanerjunk.helps.PermissionHelp.checkStorePermission
import com.test.basd.fastcleanerjunk.helps.PermissionHelp.requestStorePermission
import com.test.basd.fastcleanerjunk.helps.ads.AdmobUtils
import com.test.basd.fastcleanerjunk.view.DialogViews
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()
            }
        })

        if (checkStorePermission()) {
            playLottie()
        } else {
            DialogViews.showGerPermission(this, {
                requestStorePermission(launcher, result = { flag ->
                    if (flag) {
                        playLottie()
                    } else {
                        finishToMain()
                    }
                }, resolveNoAction = { finishToMain() })
                PermissionTripActivity.launch(this)
            }, {
                finishToMain()
            })
        }

    }

    private fun playLottie() {
        binding.idLottie1.imageAssetsFolder = "ya_smax_suo/images/"
        binding.idLottie1.setAnimation("ya_smax_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))
    }


}