package com.test.basd.smartjunkcleaner.activity

import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.Color
import android.view.animation.LinearInterpolator
import androidx.core.animation.doOnEnd
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.test.basd.smartjunkcleaner.databinding.ActivityLayoutCleanupingBinding
import com.test.basd.smartjunkcleaner.helps.BaseActivity
import com.test.basd.smartjunkcleaner.helps.ConfigHelper
import com.test.basd.smartjunkcleaner.helps.FileHelps
import com.test.basd.smartjunkcleaner.helps.ads.AdmobUtils
import com.test.basd.smartjunkcleaner.view.AFunOb
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 isLightMode = true
    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()
    }
    var isDeleSuc = false
    private var size = 0L
    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        ConfigHelper.junkSizeClean = ConfigHelper.junkSizeClean+intentSize
        MainScope().launch(Dispatchers.IO) {
            try {
                FileHelps.fileList.clear()
                listPath.forEach {
                    val file = File(it)
                    if (FileHelps.deleteFile(file)) {
                        isDeleSuc = true
                    }

                }
            } catch (_: Exception) {
            } finally {

            }
        }
        playAnm()
    }
    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 {
                AdmobUtils.showInterstitialAd(this@CleaningActivity) {
                    startActivity(Intent(this@CleaningActivity, ResultActivity::class.java).putExtra("from", AFunOb.JUNK_CLEANER).putExtra("clean_size", intentSize))
                    finish()
                }

            }
            start()
        }
        ValueAnimator.ofFloat(0f, 360f).run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idView1.rotation = it.animatedValue as Float
            }
            start()
        }
        ValueAnimator.ofFloat(0f, 360f).run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idView2.rotation = it.animatedValue as Float
            }
            start()
        }
        ValueAnimator.ofFloat(0f, 360f).run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.idView3.rotation = it.animatedValue as Float
            }
            start()
        }
    }
}