package com.develop.food.base.utils;

import android.content.Context;
import android.media.MediaPlayer;

import java.io.File;
import java.io.IOException;

/**
 * 播放背景音等较长或者较大的文件
 */
public class MusicBackPlayerUtil {

    private MediaPlayer bgPlayer, btPlayer, soundPlayer;

    public MusicBackPlayerUtil() {
        bgPlayer = new MediaPlayer();
        btPlayer = new MediaPlayer();
    }

    /**
     * 判断背景音乐是否在播放
     *
     * @return
     */
    public boolean isBGPlaying() {
        return bgPlayer != null && bgPlayer.isPlaying();
    }

    /**
     * 播放背景音
     */
    public void playBgSound(Context context, int ResID) {
        try {
            if (ResID != 0) {
                if (bgPlayer != null) {
                    if (bgPlayer.isPlaying()) {
                        bgPlayer.stop();
                    }
                    bgPlayer.release();
                    bgPlayer = null;
                }
                bgPlayer = MediaPlayer.create(context, ResID);
            }
            //mPlayer.setDataSource(resPath);
        } catch (IllegalArgumentException | IllegalStateException e) {
            e.printStackTrace();
        }
        bgPlayer.setLooping(true);
        try {
            bgPlayer.prepare();
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        bgPlayer.start();
    }

    /**
     * 播放背景音
     */
    public void playBgSound(String soundPath) {
        try {
            if (!soundPath.isEmpty() && new File(soundPath).exists()) {
                if (bgPlayer != null) {
                    if (bgPlayer.isPlaying()) {
                        bgPlayer.stop();
                    }
                    bgPlayer.release();
                    bgPlayer = null;
                }
                bgPlayer = new MediaPlayer();
                bgPlayer.setDataSource(soundPath);
            }
        } catch (IllegalArgumentException | IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        try {
            bgPlayer.prepare();
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        bgPlayer.start();
    }


    //背景声音大小
    public void setBgVolume(float leftVolume, float RightVolume) {
        if (bgPlayer != null) {
            bgPlayer.setVolume(leftVolume, RightVolume);
        }
    }

    /**
     * 播放按钮音
     */
    public void playSound(String path) throws Exception {
        if (btPlayer == null) {
            btPlayer = new MediaPlayer();
        }
        btPlayer.reset();
        btPlayer.setDataSource(path);
        btPlayer.prepare();
        btPlayer.setLooping(false);
        btPlayer.start();
    }


    public void playSound(Context context, int ResID, boolean isLoop) {
        try {
            if (ResID != 0) {
                if (soundPlayer != null) {
                    if (soundPlayer.isPlaying()) {
                        soundPlayer.stop();
                    }
                    soundPlayer.release();
                    soundPlayer = null;
                }
                soundPlayer = MediaPlayer.create(context, ResID);
            }
            //mPlayer.setDataSource(resPath);
        } catch (IllegalArgumentException | IllegalStateException e) {
            e.printStackTrace();
        }
        soundPlayer.setLooping(isLoop);
        try {
            soundPlayer.prepareAsync();
//			soundPlayer.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
        soundPlayer.start();
    }


    public MediaPlayer getBgPlayer() {
        return this.bgPlayer;
    }

    public MediaPlayer getBtPlayer() {
        return this.btPlayer;
    }

    public MediaPlayer getPlayer() {
        return this.soundPlayer;
    }

    /**
     * 停止播放背景音乐
     */
    public void stopBgSound() {
        if (bgPlayer == null)
            return;
        if (bgPlayer.isPlaying())
            bgPlayer.stop();
    }

    /**
     * 停止按钮声音播放
     */
    public void stopBtSound() {
        if (btPlayer == null)
            return;
        try {
            if (btPlayer.isPlaying()) {
                btPlayer.stop();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止声音播放
     */
    public void stopSound() {
        if (soundPlayer == null)
            return;
        try {
            if (soundPlayer.isPlaying()) {
                soundPlayer.stop();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放设备
     */
    public void releaseMedia() {
        if (bgPlayer != null) {
            try {
                if (bgPlayer.isPlaying())
                    bgPlayer.stop();
                bgPlayer.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
            bgPlayer = null;
        }
        if (btPlayer != null) {
            try {
                if (btPlayer.isPlaying())
                    btPlayer.stop();
                btPlayer.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
            btPlayer = null;
        }
        if (soundPlayer != null) {
            try {
                if (soundPlayer.isPlaying())
                    soundPlayer.stop();
                soundPlayer.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
            soundPlayer = null;
        }
    }
}