package com.xty.base.view

import android.media.MediaRecorder
import java.io.File
import java.io.IOException
import java.util.*

/**
 * 语音说话管理
 */
class AudioRecorderManager {
    private var mMediaRecorder: MediaRecorder? = null
    private var mCurrentFilePath: String? = null
    private var isPrepared = false
    private var mDir: String? = null

    companion object {

        @Volatile
        private var instance: AudioRecorderManager? = null


        fun getInstance(): AudioRecorderManager {
            val i = instance
            if (i != null) return i

            return synchronized(this) {
                val i2 = instance
                if (i2 != null) {
                    i2
                } else {
                    val created = AudioRecorderManager()
                    instance = created
                    created
                }
            }
        }
    }

    /**
     * 回调准备完毕
     */
    interface AudioStateListener {
        fun wellPrepared()
    }

    var mListener: AudioStateListener? = null

    fun setOnAudioStateListener(listener: AudioStateListener?) {
        mListener = listener
    }

    /**
     * 准备
     */
    fun prepareAudio(savePath: String) {
        try {
            isPrepared = false
            mDir = savePath
            val dir = File(mDir)
            if (!dir.exists()) {
                dir.mkdir()
            }
            val fileName = generateFileName()
            val file = File(dir, fileName)
            mCurrentFilePath = file.absolutePath
            mMediaRecorder = MediaRecorder()
            //设置输出文件
            mMediaRecorder!!.setOutputFile(file.absolutePath)
            //设置MediaRecorder的音频源为麦克风
            mMediaRecorder!!.setAudioSource(MediaRecorder.AudioSource.MIC)
            //设置音频格式
            mMediaRecorder!!.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
            //设置音频的格式为amr
            mMediaRecorder!!.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
            mMediaRecorder!!.prepare()
            mMediaRecorder!!.start()
            //准备结束
            isPrepared = true
            if (mListener != null) {
                mListener!!.wellPrepared()
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    //    生成UUID唯一标示符
    //    算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成GUID
    //    .amr音频文件
    private fun generateFileName(): String {
        return UUID.randomUUID().toString() + ".amr"
    }

    fun getVoiceLevel(maxLevel: Int): Int {
        if (isPrepared) {
            //获得最大的振幅getMaxAmplitude() 1-32767
            try {
                return maxLevel * mMediaRecorder!!.maxAmplitude / 32768 + 1
            } catch (e: Exception) {
            }
        }
        return 1
    }

    fun release() {
        if(mMediaRecorder == null){
            return
        }

        mMediaRecorder!!.release()
        mMediaRecorder = null
    }

    fun cancel() {
        release()
        if (mCurrentFilePath != null) {
            val file = File(mCurrentFilePath)
            file.delete()
            mCurrentFilePath = null
        }
    }

    fun getCurrentFilePath(): String? {
        return mCurrentFilePath
    }
}