package com.dlc.eboard.mqtt

import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.Binder
import android.os.IBinder
import android.util.Log
import com.rc.httpcore.net.DownloadListener
import com.rc.httpcore.net.DownloadTask
import java.io.File
import java.io.FileOutputStream

/**
 * info
 *
 * @author ReiChin_
 */
class AudioService : Service() {

    private val mBinder by lazy {
        AudioBinder()
    }

    private val mMediaPlayer by lazy {
        MediaPlayer()
    }

    override fun onBind(intent: Intent?): IBinder {
        return mBinder
    }

    inner class AudioBinder : Binder() {
        fun startPlayer(url: String) {
            this@AudioService.startPlayer(url)
//            this@AudioService.downloadMp3File(url)
        }

        fun release() {
            this@AudioService.release()
        }
    }

    private fun startPlayer(url: String) {
        mMediaPlayer.reset()

        mMediaPlayer.setDataSource(url)
        mMediaPlayer.setOnErrorListener { mp, what, extra ->
            Log.e("MediaPlayer", "what:${what}, extra:${extra}, mp:${mp}")
            false
        }
        mMediaPlayer.setOnPreparedListener {
            mMediaPlayer.start()
        }
        mMediaPlayer.prepareAsync()
    }

    private fun release() {
        mMediaPlayer.release()
    }

    override fun onDestroy() {
        mMediaPlayer.release()
        super.onDestroy()
    }

    fun downloadMp3File(downloadUrl: String) {
        val fileName = "aaa.mp3"
        val mp3File = File(cacheDir, fileName)
        if (mp3File.exists()) {
            mp3File.delete()
        }
        val downloadTask = DownloadTask(
            listener = object : DownloadListener {
                override fun onProgress(progress: Int) {
                    Log.i("MediaPlayer", "download mp3 file, progress:$progress")
                }

                override fun onSuccess() {
                    Log.i("MediaPlayer", "download mp3 file success.")
                    mMediaPlayer.reset()

                    val fos = FileOutputStream(mp3File)
                    mMediaPlayer.setDataSource(fos.fd)
                    mMediaPlayer.setOnErrorListener { mp, what, extra ->
                        Log.e("MediaPlayer", "what:${what}, extra:${extra}, mp:${mp}")
                        false
                    }
                    mMediaPlayer.setOnPreparedListener {
                        mMediaPlayer.start()
                    }
                    mMediaPlayer.prepareAsync()
                }

                override fun onFailed(errMsg: String?) {
                    Log.e("MediaPlayer", "download mp3 file failed, errMsg:$errMsg")
                }

                override fun onPaused() {
                }

                override fun onCanceled() {
                }

            }, dlPath = cacheDir.path,
            fileName = fileName,
            headers = mapOf("Accept-Encoding" to "identity")
        )
        downloadTask.execute(downloadUrl)
    }

}