package com.base.pdfviewerscannerwhite.ui.cleanjunk

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.view.animation.LinearInterpolator
import android.widget.Toast
import androidx.activity.addCallback
import androidx.core.animation.doOnEnd
import androidx.core.view.updatePadding
import com.base.pdfviewerscannerwhite.ads.admob.AdmobInterstitialUtils
import com.base.pdfviewerscannerwhite.bean.ConstObject.FUNCTION_SCAM_JUNK
import com.base.pdfviewerscannerwhite.databinding.ActivityLayoutCleanupingBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.ui.result.ResultActivity
import com.base.pdfviewerscannerwhite.utils.AppPreferences
import com.base.pdfviewerscannerwhite.utils.BarUtils
import com.base.pdfviewerscannerwhite.utils.updateMediaStore
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import java.io.File
import kotlin.random.Random

/**
 * 实际清理页面
 */
class CleaningActivity : BaseActivity<ActivityLayoutCleanupingBinding>() {

    override val binding: ActivityLayoutCleanupingBinding by lazy {
        ActivityLayoutCleanupingBinding.inflate(layoutInflater)
    }
    private val intentSize by lazy {
        intent.getLongExtra("size", -1)
    }
    private val listPath by lazy {
        intent.getStringArrayExtra("list") ?: arrayOf()
    }

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

        MainScope().launch(Dispatchers.IO) {
            try {
                listPath.forEach {
                    val file = File(it)
                    runCatching { file.deleteRecursively() }
                }
                updateMediaStore(listPath)
            } catch (_: Exception) {
            } finally {

            }

        }
        playAnm()
    }

    override fun initListener() {
        onBackPressedDispatcher.addCallback {
            Toast.makeText(this@CleaningActivity, "wait a moment", Toast.LENGTH_SHORT).show()
        }
    }

    private fun playAnm() {
        ValueAnimator.ofFloat(0f, 360f).run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idYuan.rotation = it.animatedValue as Float
            }
            start()
        }
        ValueAnimator.ofInt(0, 100).run {
            duration = Random.nextLong(7000, 8000)
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idTvJd.text = "${it.animatedValue as Int}"
            }
            doOnEnd {
                val isCleanEndAdSp = AppPreferences.getInstance().getString("isCleanEndAdSp", "1").toInt()
                if (isCleanEndAdSp == 1) {
                    AdmobInterstitialUtils.showInterstitialAd(this@CleaningActivity) {
                        startActivity(
                            Intent(this@CleaningActivity, ResultActivity::class.java).apply {
                                putExtra("from", FUNCTION_SCAM_JUNK)
                                putExtra("clean_size", intentSize)
                            }

                        )
                        finish()
                    }
                } else {
                    startActivity(
                        Intent(this@CleaningActivity, ResultActivity::class.java).apply {
                            putExtra("from", FUNCTION_SCAM_JUNK)
                            putExtra("clean_size", intentSize)
                        }
                    )
                    finish()
                }
            }
            start()
        }
    }

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

    override fun onResume() {
        super.onResume()
        resumeLottie()
    }

    @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()
    }

    override fun onPause() {
        super.onPause()
        animator1?.pause()
        animator2?.pause()
        animator3?.pause()
    }

}