package com.base.datarecovery.activity.junkclean

import android.animation.ValueAnimator
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.datarecovery.activity.ResultActivity
import com.base.datarecovery.ads.AdmobMaxHelper
import com.base.datarecovery.ads.admob.AdmobInterstitialUtils
import com.base.datarecovery.bean.ConstObject.JUNK_CLEANER
import com.base.datarecovery.databinding.ActivityLayoutCleanupingBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.utils.MediaStoreUtils
import com.base.datarecovery.utils.NewFileUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
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 { NewFileUtils.delete(it) }
                MediaStoreUtils.updateMediaStore(this@CleaningActivity, 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 {
                AdmobMaxHelper.admobMaxShowInterstitialAd(this@CleaningActivity){
                    startActivity(
                        Intent(this@CleaningActivity, ResultActivity::class.java).putExtra("from", JUNK_CLEANER)
                            .putExtra("clean_size", intentSize)
                    )
                    finish()
                }
            }
            start()
        }
    }
}