package com.yidianling.muse.helper

import android.content.Context
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.MediaPlayer
import com.yidianling.muse.helper.MediaPlayerManager.MediaPlayCallBack.Companion.TYPE_LIST
import kotlin.properties.Delegates

class MediaPlayerManager private constructor() {

    private var listener:OnMediaPlayerManagerListener?=null

    private var mCurrentListIndex = 0
    private var mMediaPlayCallBack:MediaPlayCallBack?=null
    private var mAudioPaths = mutableListOf<String>()

    fun setAudioPath(path: String,isLoop:Boolean = false){

        val attrs = AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setLegacyStreamType(AudioManager.STREAM_MUSIC)
            .build()

        mPath = path
        mMediaPlayer?.reset()
        mMediaPlayer?.setAudioAttributes(attrs)
        mMediaPlayer?.setDataSource(path)
        mMediaPlayer?.prepareAsync()
        mMediaPlayer?.setOnPreparedListener {
            listener?.onPrepared(it)
        }

//        mMediaPlayer?.setOnCompletionListener {
//            if (it !=null){
//                it.stop()
//                it.release()
//            }
//
//            mMediaPlayer = null
//            if (mAudioPaths!=null){
//                mCurrentListIndex++
//                if(mCurrentListIndex<mAudioPaths.size){
//                    mMediaPlayCallBack?.mediaPlayCallBack(TYPE_LIST,
//                        MediaPlayCallBack.STATE_CUT, mCurrentListIndex)
//                    setAudioPath(mAudioPaths[mCurrentListIndex])
//                }else{
//                    mMediaPlayCallBack?.mediaPlayCallBack(TYPE_LIST,
//                        MediaPlayCallBack.STATE_STOP,mAudioPaths.size
//                    )
//                }
//            }
//
//        }

    }

    fun getMediaPlayer():MediaPlayer?  = mMediaPlayer

    fun getAudioPath():String? = mPath

    fun isPlaying() = mMediaPlayer?.isPlaying

    fun play() {
        if (mMediaPlayer?.isPlaying == true) return
        mMediaPlayer?.start()
    }

    fun pause() {
        if (mMediaPlayer?.isPlaying == true) mMediaPlayer?.pause()
    }

    fun seekTo(position: Int) {
        mMediaPlayer?.seekTo(position)
    }

    fun stop() {
        mMediaPlayer?.reset()
    }

    fun release() {
        mMediaPlayer?.reset()
    }

    fun setOnPreparedListener(listener: OnMediaPlayerManagerListener){
        this.listener = listener
    }

    fun startPlayList(audioPaths:ArrayList<String>,
                      playCallBack: MediaPlayCallBack,startIndex:Int){
        if(audioPaths.isNullOrEmpty() || startIndex>=mAudioPaths.size){
            return
        }
        mMediaPlayCallBack = playCallBack
        mAudioPaths = audioPaths
        mCurrentListIndex = startIndex

        mMediaPlayCallBack?.mediaPlayCallBack(TYPE_LIST,
            MediaPlayCallBack.STATE_START,mCurrentListIndex)

        setAudioPath(mAudioPaths[mCurrentListIndex])

    }

    companion object {

        private var mContext: Context by Delegates.notNull()

        private var mMediaPlayer:MediaPlayer? = null

        private var mPath:String? = null

        @Volatile
        private var instance: MediaPlayerManager? = null

        fun getInstance(context: Context): MediaPlayerManager? {
            return when{
                instance != null -> instance
                else -> synchronized(this) {
                    if (instance == null){
                        mContext = context
                        mMediaPlayer = MediaPlayer()
                        instance = MediaPlayerManager()
                    }
                    instance
                }
            }
        }
    }


    interface OnMediaPlayerManagerListener{
        fun onPrepared(mediaPlayer: MediaPlayer)
    }

    interface MediaPlayCallBack {

        companion object{
            const val STATE_START = 0
            const val STATE_PLAY = 1
            const val STATE_PAUSE = 2
            const val STATE_STOP = 3
            const val STATE_CUT = 4


            const val TYPE_SINGLE = 0
            const val TYPE_LIST = 1

        }

        fun mediaPlayCallBack(type:Int,state:Int,position:Int){}

    }

}