package macampcorp.macamp.ui;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import java.io.FileNotFoundException;
import java.io.IOException;

import macampcorp.macamp.exceptions.ResourceFileNotFoundException;
import macampcorp.macamp.utils.files.FileManager;

/**
 * Created by ptitcois on 27/03/17.
 */
public class MediaView extends SurfaceView implements View.OnClickListener,SurfaceHolder.Callback {
    private MediaPlayer mPlayer;
    private MediaView mThis;
    private Context mContext;
    private OnStartListener mOnStart;

    public interface OnStartListener
    {
        public void onStart(MediaView mv);
    }



    public MediaView(Activity cont) {
        super(cont);
        setOnClickListener(this);
        mThis=this;
        mContext=cont;
    }

    public void surfaceCreated(SurfaceHolder holder)
    {
        if(mPlayer!=null)mPlayer.setDisplay(getHolder());
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        if(mPlayer!=null)mPlayer.setDisplay(getHolder());

    }

    public void stop() {
        if(mPlayer!=null && mPlayer.isPlaying())
            mPlayer.stop();
    }

    public void updateHolder() {
        //if(mPlayer!=null)mPlayer.setDisplay(getHolder());

    }

    public void clearPlayer() {
        mPlayer=null;

    }

    public boolean play(String path)
    {
        final MediaView x=this;
        AssetFileDescriptor afd;
        mPlayer = new MediaPlayer();
        afd = FileManager.getAfdMedia(mContext, path);
       if(afd==null) return false;
        try {
            mPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
        } catch (IOException e) {
            e.printStackTrace();

        }
        mPlayer.prepareAsync();
        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                //mp.setDisplay(mThis.getHolder());
                mp.start();
                if(mOnStart!=null)
                    mOnStart.onStart(x);
            }
        });
        return true;

    }
    @Override
    public void onClick(View view) {
        if(mPlayer!=null)
        {
            if(mPlayer.isPlaying())
            {
                mPlayer.pause();
            }else{
                mPlayer.start();
            }

        }
    }

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

    public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener o)
    {
        mPlayer.setOnSeekCompleteListener(o);
    }

    public int getDuration()
    {
        return mPlayer.getDuration();
    }

    public int getTime()
    {
        return mPlayer.getCurrentPosition();
    }

    public void start()
    {
        mPlayer.start();
    }

    public void pause()
    {
        mPlayer.pause();
    }

    public void seek(int x)
    {
        mPlayer.seekTo(x);
    }

    public void surfaceDestroyed(SurfaceHolder var1) {
        synchronized (this) {
            //mHasActiveHolder = false;
            if(mPlayer!=null) mPlayer.stop();
            mPlayer=null;
            synchronized(this)          {
                this.notifyAll();
            }
        }
    }

    public void setOnStartListener(OnStartListener mv)
    {
        mOnStart=mv;
    }
}
