package com.datarecovery.master.utils;

import android.media.MediaPlayer;

import com.atmob.common.logging.AtmobLog;

import java.io.IOException;

public class SafeMediaPlayer extends MediaPlayer {

    private static final String TAG = "SafeMediaPlayer";

    private OnErrorListener onErrorListener;

    private OnPreparedListener onPreparedListener;

    private volatile boolean isPrepared = false;

    public SafeMediaPlayer() {
        super.setOnErrorListener((mp, what, extra) -> {
            AtmobLog.e(TAG, "onError() called with: mp = [" + mp + "], what = [" + what + "], extra = [" + extra + "]");
            if (onErrorListener != null) {
                onErrorListener.onError(mp, what, extra);
            }
            return true;
        });
        super.setOnPreparedListener(mp -> {
            AtmobLog.d(TAG, "onPrepared() called with: mp = [" + mp + "]");
            isPrepared = true;
            if (onPreparedListener != null) {
                onPreparedListener.onPrepared(mp);
            }
        });
    }

    @Override
    public void setOnErrorListener(OnErrorListener listener) {
        this.onErrorListener = listener;
    }

    @Override
    public void setOnPreparedListener(OnPreparedListener listener) {
        onPreparedListener = listener;
    }

    @Override
    public void start() {
        Thread.currentThread().getStackTrace();
        AtmobLog.i(TAG, "%s start() called" + this);
        try {
            super.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stop() {
        AtmobLog.i(TAG, "%s stop() called" + this);
        try {
            super.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void pause() {
        AtmobLog.i(TAG, "%s pause() called" + this);
        try {
            super.pause();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isPlaying() {
        try {
            return super.isPlaying();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void setVolume(float leftVolume, float rightVolume) {
        AtmobLog.i(TAG, "setVolume() called with: leftVolume = [" + leftVolume + "], rightVolume = [" + rightVolume + "]");
        try {
            super.setVolume(leftVolume, rightVolume);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void prepare() throws IOException, IllegalStateException {
        AtmobLog.i(TAG, "prepare() called");
        try {
            super.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void prepareAsync() throws IllegalStateException {
        AtmobLog.i(TAG, "prepareAsync() called");
        try {
            super.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isPrepared() {
        return isPrepared;
    }
}
