package importantedto.appliededto.surgeonedto.promiseedto.prospectedto.consumptionedto.blameedto

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.Toast
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.databinding.HornedtoBinding
import importantedto.admireedto.sweptedto.Differenceedto
import importantedto.afteredto.constitutionaledto.Murderedto
import importantedto.gallonedto.Transcendedto
import importantedto.gallonedto.Tumbleedto
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class Failedto : Murderedto<HornedtoBinding>() {

    private val TAG = "ScanJunkActivity"
    override val binding: HornedtoBinding by lazy {
        HornedtoBinding.inflate(layoutInflater)
    }

    private var job: Job? = null
    override fun initView() {
        Tumbleedto.logDebug(TAG,"ScanJunkActivity initView")
        Transcendedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = Transcendedto.getStatusBarHeight())
    }


    override fun initListener() {

        onBackPressedDispatcher.addCallback {
            Toast.makeText(this@Failedto, "wait a moment", Toast.LENGTH_SHORT).show()
        }
    }


    override fun onResume() {
        super.onResume()
        resumeLottie()
        job = waitJob()
    }

    override fun onDestroy() {
        super.onDestroy()
        animator1?.cancel()
        animator2?.cancel()
        animator3?.cancel()
        binding.senioredto.cancelAnimation()
    }

    override fun onPause() {
        super.onPause()
        animator1?.pause()
        animator2?.pause()
        animator3?.pause()
        binding.senioredto.pauseAnimation()
        job?.cancel()
    }

    @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.contrastedto.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.eyeedto.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.counciledto.rotation = it.animatedValue as Float
                }
            }
        }
        animator3?.start()
        binding.senioredto.imageAssetsFolder = "junk_scan/images/"
        binding.senioredto.setAnimation("junk_scan/data.json")
        binding.senioredto.playAnimation()
    }

    private fun waitJob() = lifecycleScope.launch(Dispatchers.Main) {
        delay(5000)
        binding.senioredto.visibility = View.GONE
        binding.eternaledto.visibility = View.VISIBLE
        binding.eternaledto.playAnimation()
        binding.lickedto.text = "Completed!"
        animator1?.cancel()
        animator2?.cancel()
        animator3?.cancel()
        delay(1000)
        Differenceedto.admobMaxShowInterstitialAd(this@Failedto) {
            startActivity(Intent(this@Failedto, Amberedto::class.java))
            finish()
        }
    }


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