package com.cr.common

import android.content.Context
import android.media.AudioManager
import android.media.MediaPlayer
import android.speech.tts.TextToSpeech
import com.cr.cruav.CrApplication
import com.cr.data.CrUtil
import java.util.*

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/8/4 09:11
 * 描述：音频播放类
 */
class CrAudioUtil {
    private var tts:TextToSpeech?= null // define: 2023/8/17 tts语音
    private var isSuccess:Boolean = false  // define: 2023/8/17 是否初始化完成
    /**
     * 用于单例
     */
    object InstanceHelper{
        var self = CrAudioUtil()
    }

    /**
     * tts监听
     */
    private val ttsListener = TextToSpeech.OnInitListener {
        if(it == TextToSpeech.SUCCESS){
            // todo: 2023/8/17 设置播放语言
            var result = tts?.setLanguage(Locale.CHINESE)
            if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
                CrUtil.showToast("不支持中文！")
            }else if(result == TextToSpeech.LANG_AVAILABLE){
                CrUtil.showToast("语音TTS初始化完成！")
                isSuccess = true
            }
        }else{
            CrUtil.showToast("TTS初始化失败！")
        }
    }

    /**
     * 初始化
     */
    init {
        tts = TextToSpeech(CrApplication.getContext(),ttsListener)
    }


    /**
     * 播放语音
     * @param ttsMessage String
     */
    fun play(ttsMessage:String){
        if(isSuccess){
            tts?.speak(ttsMessage,TextToSpeech.QUEUE_ADD,null,"A")
        }else{
            CrUtil.showToast("TTS初始化异常！")
        }

    }

    /**
     * 静态方法
     */
    companion object {
        private var MIN_RATIO: Float = 0.3f

        // define: 2023/8/4 播放器
        private var player: MediaPlayer? = null

        /**
         * 播放音频
         * @param context Context 上下文
         * @param resID Int 音频资源
         */
        fun playSound(context: Context, resID: Int){
            playSound(context,resID,false)
        }

        fun getInstance() = InstanceHelper.self

        /**
         * 播放声音
         * @param context Context 上下文
         * @param resID Int 音频资源Id
         * @param ignoreWhenBusy Boolean
         */
        private fun playSound(context: Context, resID: Int, ignoreWhenBusy: Boolean) {
            try {
                // todo: 2023/8/5 如果不为null 则至null
                if (player != null) {
                    player?.let {
                        // todo: 2023/8/4 如果正在播放则停止
                        if (it.isPlaying) {
                            if (ignoreWhenBusy) {
                                return
                            }
                            it.stop()
                        }
                        it.release()
                        player = null
                    }
                }
                // todo: 2023/8/5 创建播放器
                player = MediaPlayer.create(context, resID)
                player?.setOnCompletionListener { mp ->
                    {
                        player?.let {
                            it.release()
                            player = null
                        }
                    }
                }
                var am = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
                var audioMaxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
                var audioCurrentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC)
                var volumeRatio = (audioCurrentVolume/audioMaxVolume)*1f
                if(volumeRatio < MIN_RATIO){
                    volumeRatio = MIN_RATIO
                }
                player?.setVolume(volumeRatio,volumeRatio)
                player?.start()
            } catch (e: java.lang.Exception) {
                CrUtil.print("播放错误:${e.message}")
            }
        }
    }
}