package com.zxhyis.toolss.ui.page

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.zxhyis.toolss.ad.AdmobUtils
import com.zxhyis.toolss.ad.ConfigHelper
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.cx.FileHelps
import com.zxhyis.toolss.databinding.ActivityLayoutCleaningBinding
import com.zxhyis.toolss.ui.AFunOb
import com.zxhyis.toolss.ui.page.result.ResultActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import java.io.File
import kotlin.random.Random

class CleaningActivity : TopActivity<ActivityLayoutCleaningBinding>() {

    override fun getViewBinding1(): ActivityLayoutCleaningBinding {
        return ActivityLayoutCleaningBinding.inflate(layoutInflater)
    }

    private val intentSize by lazy {
        intent.getLongExtra("size", -1)
    }
    private val listPath by lazy {
        intent.getStringArrayExtra("list") ?: arrayOf()
    }
    var isDeleSuc = false
    override fun init() {

        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        ConfigHelper.junkSizeClean = intentSize
        vb.root.updatePadding(top = BarUtils.getStatusBarHeight())
        MainScope().launch(Dispatchers.IO) {
            try {
                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 {
                vb.idYuan.rotation = it.animatedValue as Float
            }
            start()
        }
        ValueAnimator.ofInt(0, 100).run {
            duration = Random.nextLong(7000, 8000)
            interpolator = LinearInterpolator()
            addUpdateListener {
                vb.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)
                    )
                    // startActivity<ResultActivity>("from" to "clean", "clean_size" to x)
                    finish()
                }

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