package rallyedto.finishedto.hypothesisedto.vegetableedto.breakfastedto

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.databinding.NameedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.finishedto.Screamedto
import rallyedto.sunedto.tumouredto.majorityedto.loseedto.Togetheredto
import rallyedto.talledto.blewedto.Islandedto
import rallyedto.talledto.blewedto.Localedto
import rallyedto.talledto.blewedto.Sketchedto
import kotlin.random.Random

/**
 * 实际清理页面
 */
class Drugedto : Lemonedto<NameedtoBinding>() {

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

    override fun initView() {
        Islandedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = Islandedto.getStatusBarHeight())
        Togetheredto.showNativeAd(this,binding.jeweledto, isShowAdNative = Togetheredto.isShowAdNativeBottom())
        MainScope().launch(Dispatchers.IO) {
            try {
                listPath.forEach {
                    runCatching { Sketchedto.delete(it) }
                }
                Localedto.updateMediaStore(this@Drugedto, listPath)
            } catch (_: Exception) {
            } finally {

            }

        }
        playAnm()
    }

    override fun initListener() {
        onBackPressedDispatcher.addCallback {
            Toast.makeText(this@Drugedto, "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.simplicityedto.rotation = it.animatedValue as Float
            }
            start()
        }
        ValueAnimator.ofInt(0, 100).run {
            duration = Random.nextLong(7000, 8000)
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.packedto.text = "${it.animatedValue as Int}"
            }
            doOnEnd {
                startActivity(
                    Intent(this@Drugedto, Screamedto::class.java)
                        .putExtra("from", type)
                        .putExtra("clean_size", intentSize)
                )
                finish()
            }
            start()
        }
    }
}