package com.mytatvarnsdk.myView.Media;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.PowerManager;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import cgmblelib.utils.MyLog;

import java.io.IOException;

/**
 * 自定义的媒体播放类
 *
 * @author minyuchun
 */
public class CustomMediaPlayer implements SurfaceHolder.Callback
        , MediaPlayer.OnPreparedListener,
        MediaPlayer.OnErrorListener,
        MediaPlayer.OnCompletionListener {

    private MediaPlayer mMediaPlayer;

    private AssetFileDescriptor fd;

    private Context mContext;

    private PlayerListener listener;

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        MyLog.i("surfaceCreated 界面成功创建");
        initMediaPlayer();
        if (mMediaPlayer != null) {
            mMediaPlayer.setDisplay(holder);
            try {
                mMediaPlayer.setDataSource(fd);
                mMediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        MyLog.i("surfaceChanged 界面修改");
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        MyLog.d("surfaceDestroyed 界面销毁");
        release();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        MyLog.i(" onCompletion 加载完成");
        //保持CPU唤醒

    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        MyLog.e("what = " + what +", extra = " + extra);
        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        MyLog.i("onPrepared");
        mp.start();
        mp.setScreenOnWhilePlaying(true);
        mp.setLooping(true);
        if (mContext != null) {
            mp.setWakeMode(mContext, PowerManager.PARTIAL_WAKE_LOCK);
        }
        if (listener != null) {
            listener.loadFinish();
        }
    }

    private static class SingletonInstance {
        private static final CustomMediaPlayer INSTANCE = new CustomMediaPlayer();
    }

    public static CustomMediaPlayer getInstance() {
        return SingletonInstance.INSTANCE;
    }

    public void init(@NonNull Activity activity, SurfaceView surfaceView, int resId) {
        mContext = activity;
        fd = activity.getResources().openRawResourceFd(resId);
        if (surfaceView != null) {
            SurfaceHolder surfaceHolder = surfaceView.getHolder();
            if (surfaceHolder != null) {
                surfaceHolder.addCallback(this);
            }
        }
    }

    private void initMediaPlayer() {
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
        }
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnCompletionListener(this);
    }

    public void onPause() {
        if (mMediaPlayer != null) {
            mMediaPlayer.pause();
//            msec = mMediaPlayer.getCurrentPosition();
        }
    }

    public void onPlay() {
        if (mMediaPlayer != null) {
            mMediaPlayer.start();
//            msec = mMediaPlayer.getCurrentPosition();
        }
    }

    public void release() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    public void setSound(boolean open) {
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            if (open) {
                mMediaPlayer.setVolume(0.5f, 0.5f);
            } else {
                mMediaPlayer.setVolume(0, 0);
            }

        }
    }

    public void setPlayerListener(PlayerListener listener) {
        this.listener = listener;
    }

    public interface PlayerListener {
        void loadFinish();
    }
}
