package com.base.filerecoveryrecyclebin.activity.junkclean

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.Toast
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.base.filerecoveryrecyclebin.ads.AdmobMaxHelper.admobMaxShowInterstitialAd
import com.base.filerecoveryrecyclebin.databinding.ActivityLayoutScanJunkBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.PermissionHelp.checkStorePermission
import com.base.filerecoveryrecyclebin.help.PermissionHelp.requestStorePermission
import com.base.filerecoveryrecyclebin.utils.BarUtils
import com.base.filerecoveryrecyclebin.view.DialogViews.showGerPermission
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 清理动画页面
 */
class ScanJunkActivity : BaseActivity<ActivityLayoutScanJunkBinding>() {

    private val TAG = "ScanJunkActivity"
    override val binding: ActivityLayoutScanJunkBinding by lazy {
        ActivityLayoutScanJunkBinding.inflate(layoutInflater)
    }

    private var job: Job? = null
    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        if (!checkStorePermission()) {
            showGerPermission(tittle = "Storage Permission Required",
                desc = "This feature requires access to your storage to scan your files and clean up junk files and unused APK files. We will not transmit your data to any third-party service. Please grant permission so that we can provide you with better service.",
                deny = { finishToMain() },
                allow = {
                    requestStorePermission(launcher, result = {
                        if (it) {
                        } else {
                            finishToMain()
                        }
                    })
                })
        }
    }


    override fun initListener() {

        onBackPressedDispatcher.addCallback {
            Toast.makeText(this@ScanJunkActivity, "wait a moment", Toast.LENGTH_SHORT).show()
        }
    }


    override fun onResume() {
        super.onResume()
        if (checkStorePermission()) {
            resumeLottie()
            job = waitJob()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        animator1?.cancel()
        animator2?.cancel()
        animator3?.cancel()
        binding.lottie.cancelAnimation()
    }

    override fun onPause() {
        super.onPause()
        animator1?.pause()
        animator2?.pause()
        animator3?.pause()
        binding.lottie.pauseAnimation()
        job?.cancel()
    }

    @SuppressLint("SetTextI18n")
    private fun resumeLottie() {
        if (animator1 == null) {
            animator1 = ValueAnimator.ofFloat(0f, 360f)
            animator1?.run {
                duration = 1000
                repeatMode = ValueAnimator.RESTART
                repeatCount = ValueAnimator.INFINITE
                interpolator = LinearInterpolator()
                addUpdateListener {
                    binding.idView1.rotation = it.animatedValue as Float
                }
            }
        }
        animator1?.start()
        if (animator2 == null) {
            animator2 = ValueAnimator.ofFloat(0f, 360f)
            animator2?.run {
                duration = 1000
                repeatMode = ValueAnimator.RESTART
                repeatCount = ValueAnimator.INFINITE
                interpolator = LinearInterpolator()
                addUpdateListener {
                    binding.idView2.rotation = it.animatedValue as Float
                }
            }
        }
        animator2?.start()
        if (animator3 == null) {
            animator3 = ValueAnimator.ofFloat(0f, 360f)
            animator3?.run {
                duration = 1000
                repeatMode = ValueAnimator.RESTART
                repeatCount = ValueAnimator.INFINITE
                interpolator = LinearInterpolator()
                addUpdateListener {
                    binding.idView3.rotation = it.animatedValue as Float
                }
            }
        }
        animator3?.start()
        binding.lottie.imageAssetsFolder = "junk_scan/images/"
        binding.lottie.setAnimation("junk_scan/data.json")
        binding.lottie.playAnimation()
    }

    private fun waitJob() = lifecycleScope.launch(Dispatchers.Main) {
        delay(5000)
        binding.lottie.visibility = View.GONE
        binding.lottieCompleted.visibility = View.VISIBLE
        binding.lottieCompleted.playAnimation()
        binding.tv.text = "Completed!"
        animator1?.cancel()
        animator2?.cancel()
        animator3?.cancel()
        delay(1000)
        admobMaxShowInterstitialAd(this@ScanJunkActivity) {
            startActivity(Intent(this@ScanJunkActivity, CleanJunkActivity::class.java))
            finish()
        }
    }


    private var animator1: ValueAnimator? = null
    private var animator2: ValueAnimator? = null
    private var animator3: ValueAnimator? = null
}