package com.test.basd.onecleanmaster.activity

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.test.basd.onecleanmaster.R
import com.test.basd.onecleanmaster.databinding.ActivityLayoutSpeakerCleaningBinding
import com.test.basd.onecleanmaster.helps.BaseActivity
import com.test.basd.onecleanmaster.helps.ads.AdmobUtils


class SpeakCleaningActivity : BaseActivity<ActivityLayoutSpeakerCleaningBinding>() {

    private val zAudio by lazy {
        MediaPlayer.create(this, R.raw.z)
    }
    override val binding: ActivityLayoutSpeakerCleaningBinding by lazy {
        ActivityLayoutSpeakerCleaningBinding.inflate(layoutInflater)
    }

    override fun initView() {
        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)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        binding.idSpeakCleanLottie.imageAssetsFolder = "one_speak_clean/images/"
        binding.idSpeakCleanLottie.setAnimation("one_speak_clean/data.json")
        binding.idSpeakCleanLottie.playAnimation()
        binding.root.postDelayed({
            zAudio.stop()
            binding.idLlP1.isVisible = false
            binding.idLlP2.isVisible = true
            playComplete()
        }, 30000)
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                ToastUtils.showShort("Cleaning,Please wait a moment")
            }
        })
    }

    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()
    }



    private fun playComplete() {

        binding.idLottieComplete.playAnimation()

        binding.idLottieComplete.addLottieOnCompositionLoadedListener {

        }
        binding.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) {

            }

        })
    }

}