package com.cooleshow.base.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;

public class MusicService extends Service {
    MusicPlayContract contract;


    public String path = "";

    private MediaPlayer mMediaPlayer;
    private PlaybackParams playParam;

    @Override
    public IBinder onBind(Intent intent) {
        //当执行完了onCreate后，就会执行onBind把操作歌曲的方法返回

        return new MyBinder();
    }


    @Override
    public void onCreate() {
        super.onCreate();
        //这里只执行一次，用于准备播放器
        Log.e("服务", "准备播放音乐");
    }

    //该方法包含关于歌曲的操作
    public class MyBinder extends Binder {

        //判断是否处于播放状态
        public boolean isPlaying() {
            return null == mMediaPlayer ? false : mMediaPlayer.isPlaying();
        }


        public void setPlayParam(PlaybackParams param){
            playParam = param;

        }
        //播放或暂停歌曲
        public void onPlay(boolean isPlaying, String musicPath) {
            if (path.equals(musicPath)) {
                if (!isPlaying) {
                    if (mMediaPlayer == null) {
                        startPlaying(musicPath);
                    } else {
                        resumePlaying();
                    }
                } else {
                    pausePlaying();
                }
            } else {
                startPlaying(musicPath); //start from beginning
            }

        }

        //播放
        public void startPlaying(String musicPath) {
            path = musicPath;
            try {
                if (mMediaPlayer == null) {
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setOnPreparedListener(mp -> {
                        mp.start();
                        if (contract != null) {
                            contract.setOnPreparedListener();
                            contract.onPlay(true);

                        }
                    });
                    mMediaPlayer.setOnErrorListener((mediaPlayer, i, i1) -> {
                        if (contract != null) {
                            contract.setOnErrorListener();
                            contract.onPlay(false);
                        }
                        return false;
                    });
                    mMediaPlayer.setOnCompletionListener(mediaPlayer -> {
                        if (contract != null) {
                            contract.setOnCompletionListener();
                            contract.onPlay(false);
                        }
                    });


                }
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(musicPath);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && playParam != null) {
                    try {
                        if (mMediaPlayer != null) {
                            mMediaPlayer.setPlaybackParams(playParam);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                //准备资源
                mMediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        public void resumePlaying() {

            if (mMediaPlayer != null) {
                mMediaPlayer.start();
                if (contract != null) {
                    contract.setOnPreparedListener();
                    contract.onPlay(true);
                }
            }

        }

        public void stopPlaying() {

            if (mMediaPlayer == null) {
                return;
            }
            mMediaPlayer.stop();
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
            if (contract != null) {
                contract.setOnCompletionListener();
                contract.onPlay(false);
            }
        }


        public void pausePlaying() {
            if (null != mMediaPlayer) {
                mMediaPlayer.pause();
                if (null != contract) {
                    contract.setOnPauseListener();
                    contract.onPlay(false);
                }
            }

        }

        //返回歌曲的长度，单位为毫秒
        public int getDuration() {
            return null == mMediaPlayer ? 0 : mMediaPlayer.getDuration();
        }

        //返回歌曲目前的进度，单位为毫秒
        public int getCurrenPostion() {
            return null == mMediaPlayer ? 0 : mMediaPlayer.getCurrentPosition();
        }

        //设置歌曲播放的进度，单位为毫秒
        public void seekTo(int mesc) {
            if (mMediaPlayer != null) {
                mMediaPlayer.seekTo(mesc);
            }
        }

        public void setCallback(MusicPlayContract callback) {
            contract = callback;
        }
    }


}

