package com.app.downloadstream1;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.util.Log;

public class StreamMediaPlayer extends MediaPlayer {
	public static final String TAG = "DownloadS";
	
	private LocalHttpServer mServer;
	
	protected OnBufferingUpdateListener mBufferingUpdateListener;
	protected OnCompletionListener mCompletionListener;
	protected OnErrorListener mErrorListener;
	protected OnPreparedListener mPreparedListener;
	protected OnSeekCompleteListener mSeekCompleteListener;
	
	protected boolean mUsed;
	
	public StreamMediaPlayer() {
		super();
		mUsed = false;	
	}

	@Override
	public void setDataSource(String path) throws IOException,
			IllegalArgumentException, IllegalStateException {
		Log.d(TAG, "setDataSource(path=" + path + ")");
		try {
			super.setDataSource(path);
		}catch(IllegalStateException e) {
			super.reset();
			super.setOnBufferingUpdateListener(mBufferingUpdateListener);
			super.setOnCompletionListener(mCompletionListener);
			super.setOnErrorListener(mErrorListener);
			super.setOnPreparedListener(mPreparedListener);
			super.setOnSeekCompleteListener(mSeekCompleteListener);
			
			Log.d(TAG, "setDataSource(path=" + path + ") *AGAIN*");
			super.setDataSource(path);
		}
	}
	
	public void setDataSource(TailStream in) throws IllegalArgumentException, IllegalStateException, IOException {
		Log.d(TAG, "setDataSource(RandomAccessStream)");

		if (mServer != null)
			mServer.reset(in);
		else
		{
			mServer = new LocalHttpServer(1024, in);
			mServer.start();
		}
		setDataSource(mServer.makeUri());
	}
	
	private void resetInternalListeners()
	{
		mBufferingUpdateListener = null;
		mCompletionListener = null;
		mErrorListener = null;
		mPreparedListener = null;
		mSeekCompleteListener = null;		
	}
	
	public void reset() {
		Log.d(TAG, "reset");
		resetInternalListeners();
		super.reset();
		if (mServer != null)
			mServer.reset(null);
	}
	
	public void release() {
		Log.e(TAG, "release");
		super.release();
		mServer.reset(null);
		this.resetInternalListeners();
	}
	
	@Override
	public void setOnBufferingUpdateListener(OnBufferingUpdateListener l)
	{
		mBufferingUpdateListener = l;
		super.setOnBufferingUpdateListener(l);
	}

	@Override
	public void setOnCompletionListener(OnCompletionListener l)
	{
		mCompletionListener = l;
		super.setOnCompletionListener(l);
	}

	@Override
	public void setOnErrorListener(OnErrorListener l)
	{
		mErrorListener = l;
		super.setOnErrorListener(l);
	}

	@Override
	public void setOnPreparedListener(OnPreparedListener l)
	{
		mPreparedListener = l;
		super.setOnPreparedListener(l);
	}

	@Override
	public void setOnSeekCompleteListener(OnSeekCompleteListener l)
	{
		mSeekCompleteListener = l;
		super.setOnSeekCompleteListener(l);
	}
	
	

}
