package com.zxhyis.toolss.ui.page

import android.animation.Animator
import android.graphics.Color
import android.media.AudioManager
import android.media.MediaPlayer
import androidx.activity.OnBackPressedCallback
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ToastUtils
import com.zxhyis.toolss.R
import com.zxhyis.toolss.ad.AdmobUtils
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.databinding.ActivityLayoutSpeakerCleaningBinding


class SpeakCleaningActivity : TopActivity<ActivityLayoutSpeakerCleaningBinding>() {
    override fun getViewBinding1(): ActivityLayoutSpeakerCleaningBinding {
        return ActivityLayoutSpeakerCleaningBinding.inflate(layoutInflater)
    }

    private val zAudio by lazy {
        MediaPlayer.create(this, R.raw.z)
    }

    override fun onStart() {
        super.onStart()
        zAudio.start()
    }
    override fun onStop() {
        super.onStop()
        try {
            zAudio.pause()
        } catch (e: Exception) {
            zAudio.stop()
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        zAudio.release()
    }

    override fun init() {
        val audioManager = this.getSystemService(AUDIO_SERVICE) as AudioManager
        val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        vb.root.updatePadding(top = BarUtils.getStatusBarHeight())
        vb.idSpeakCleanLottie.imageAssetsFolder = "speakclean/images/"
        vb.idSpeakCleanLottie.setAnimation("speakclean/data.json")
        vb.idSpeakCleanLottie.playAnimation()
        vb.root.postDelayed({
            zAudio.stop()
            vb.idLlP1.isVisible = false
            vb.idLlP2.isVisible = true
            playComplete()
        }, 30000)
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
               ToastUtils.showShort("Cleaning,Please wait a moment")
            }
        })
    }

    private fun playComplete() {

        vb.idLottieComplete.playAnimation()

        vb.idLottieComplete.addLottieOnCompositionLoadedListener {

        }
        vb.idLottieComplete.addAnimatorListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(p0: Animator) {

            }

            override fun onAnimationEnd(p0: Animator) {
                AdmobUtils.showInterstitialAd(this@SpeakCleaningActivity) {
                    finish()
                }

            }

            override fun onAnimationCancel(p0: Animator) {

            }

            override fun onAnimationRepeat(p0: Animator) {

            }

        })
    }

}