package xn.xxp.utils;

import android.media.MediaPlayer;
import android.util.Log;

import androidx.core.util.Consumer;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;

/**
 * 播放音频
 */
public class AudioPlayer {
    private static AudioPlayer instance;
    private MediaPlayer mediaPlayer;
    private int currentAudioResId = 0;
    private String url;

    private AudioPlayer() {
        // 私有构造函数，防止外部创建实例
    }

    public static synchronized AudioPlayer getInstance() {
        if (instance == null) {
            instance = new AudioPlayer();
        }
        return instance;
    }

    public synchronized void play(int audioResId) {
        if (mediaPlayer != null && currentAudioResId == audioResId) {
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer.start();
            }
            return;
        }
        stop();
        try {
            mediaPlayer = MediaPlayer.create(Utils.getApp(), audioResId);
            if (mediaPlayer != null) {
                mediaPlayer.start();
                currentAudioResId = audioResId;
                mediaPlayer.setOnCompletionListener(mp -> stop());
            } else {
                LogUtils.e("Failed to create MediaPlayer for resource ID: " + audioResId);
            }
        } catch (Exception e) {
            LogUtils.e("Error playing audio resource: " + audioResId, Log.getStackTraceString(e));
        }
    }

    public synchronized void play(int audioResId, Consumer<Boolean> consumer) {
        if (mediaPlayer != null && currentAudioResId == audioResId) {
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer.start();
            }
            return;
        }
        stop();
        try {
            mediaPlayer = MediaPlayer.create(Utils.getApp(), audioResId);
            if (mediaPlayer != null) {
                mediaPlayer.start();
                currentAudioResId = audioResId;
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        consumer.accept(true);
                        stop();
                    }
                });
            } else {
                LogUtils.e("Failed to create MediaPlayer for resource ID: " + audioResId);
            }
        } catch (Exception e) {
            LogUtils.e("Error playing audio resource: " + audioResId, Log.getStackTraceString(e));
        }
    }

    public synchronized void play(String url) {
        stop();
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(url);
            if (mediaPlayer != null) {
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                    }
                });
                mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        LogUtils.e("播放失败", url, what, extra);
                        stop();
                        return true;
                    }
                });
                mediaPlayer.setOnCompletionListener(mp -> stop());
            } else {
                LogUtils.e("Failed to create MediaPlayer for url: " + url);
            }
        } catch (Exception e) {
            LogUtils.e("Error playing audio resource: " + url, Log.getStackTraceString(e));
        }
    }

    public void stop() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
            mediaPlayer = null;
            currentAudioResId = 0;
        }
    }

    public boolean isPlaying() {
        return mediaPlayer != null && mediaPlayer.isPlaying();
    }
}
