package com.kaltura.playersdk;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.widget.VideoView;

import com.kaltura.playersdk.events.OnPlayerStateChangeListener;
import com.kaltura.playersdk.events.OnPlayheadUpdateListener;
import com.kaltura.playersdk.events.OnProgressListener;
import com.kaltura.playersdk.types.PlayerStates;

/**
 * Created by michalradwantzor on 9/15/13.
 */
public class PlayerView extends VideoView implements VideoPlayerInterface {
    //TODO make configurable
    public static int PLAYHEAD_UPDATE_INTERVAL = 200;

    private String mVideoUrl;
    private OnPlayerStateChangeListener mPlayerStateListener;
    private MediaPlayer.OnPreparedListener mPreparedListener;
    private OnPlayheadUpdateListener mPlayheadUpdateListener;
    private OnProgressListener mProgressListener;
    private Timer mTimer;
    private int mStartPos = 0;

    public PlayerView(Context context) {
        super(context);

        super.setOnCompletionListener( new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                pause();
                seekTo( 0 );
                updateStopState();
                mPlayerStateListener.onStateChanged(PlayerStates.END);
            }
        });

        super.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mPlayerStateListener.onStateChanged(PlayerStates.START);
                mediaPlayer.setOnSeekCompleteListener( new MediaPlayer.OnSeekCompleteListener() {
                    @Override
                    public void onSeekComplete(MediaPlayer mediaPlayer) {
                    	mPlayerStateListener.onStateChanged(PlayerStates.SEEKED);
                    }
                });

                if ( mPreparedListener != null ) {
                    mPreparedListener.onPrepared(mediaPlayer);
                }

                mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                    @Override
                    public void onBufferingUpdate(MediaPlayer mp, int progress) {
                        if ( mProgressListener != null ) {

                            mProgressListener.onProgressUpdate(progress);
                        }
                    }
                });
            }
        });

    }

    @Override
    public void setStartingPoint(int point) {
        mStartPos = point;
    }

    @Override
    public String getVideoUrl() {
    	return mVideoUrl;
    }

    @Override
    public void setVideoUrl(String url) {
        mVideoUrl = url;
        super.setVideoURI(Uri.parse(url));
    }

    @Override
    public int getDuration() {
        return super.getDuration();
    }

    public int getCurrentPosition() {
        return super.getCurrentPosition();
    }

    @Override
    public boolean getIsPlaying() {
        return super.isPlaying();
    }

    @Override
    public void play() {
    	if ( !this.isPlaying() ) {
            super.start();
            if ( mStartPos != 0 ) {
            	super.seekTo( mStartPos );
            	mStartPos = 0;
            }
            if ( mTimer == null ) {
                mTimer = new Timer();
            }
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                	try {
                		int position = getCurrentPosition();
                		if ( position != 0 && mPlayheadUpdateListener != null )
                			mPlayheadUpdateListener.onPlayheadUpdated(position);
                	} catch(IllegalStateException e){
            	        e.printStackTrace(); 
            	    }
                   
                }
            }, 0, PLAYHEAD_UPDATE_INTERVAL);

            mPlayerStateListener.onStateChanged(PlayerStates.PLAY);
    	}
    }

    @Override
    public void pause() {
    	if ( this.isPlaying() ) {
            super.pause();
            mPlayerStateListener.onStateChanged(PlayerStates.PAUSE);	
    	}
    }

    @Override
    public void stop() {
        super.stopPlayback();
        updateStopState();
    }
    
    private void updateStopState() {
        if ( mTimer != null ) {
            mTimer.cancel();
            mTimer = null;
        }
       
    }

    @Override
    public void seek(int msec) {
    	mPlayerStateListener.onStateChanged(PlayerStates.SEEKING);
        super.seekTo(msec);
    }

    @Override
    public void registerPlayerStateChange( OnPlayerStateChangeListener listener) {
        mPlayerStateListener = listener;
    }

    @Override
    public void registerReadyToPlay( MediaPlayer.OnPreparedListener listener) {
        mPreparedListener = listener;
    }

    @Override
    public void registerError( MediaPlayer.OnErrorListener listener) {
        super.setOnErrorListener(listener);

    }

    @Override
    public void registerPlayheadUpdate( OnPlayheadUpdateListener listener ) {
        mPlayheadUpdateListener = listener;
    }

    @Override
    public void removePlayheadUpdateListener() {
    	mPlayheadUpdateListener = null;
    }

    @Override
    public void registerProgressUpdate ( OnProgressListener listener ) {
        mProgressListener = listener;
    }
}



@@ -168,16 +168,43 @@
 et.Uri;%0A
+import android.os.Handler;%0A
 import a
@@ -943,34 +943,8 @@
 er;%0A
-    private Timer mTimer;%0A
     
@@ -969,16 +969,479 @@
 Pos = 0;
+%0A    %0A    private Handler mHandler;%0A    private Runnable runnable = new Runnable() %7B%0A %09   @Override%0A %09   public void run() %7B%0A %09%09  try %7B%0A      %09%09int position = getCurrentPosition();%0A      %09%09if ( position != 0 && mPlayheadUpdateListener != null )%0A      %09%09%09mPlayheadUpdateListener.onPlayheadUpdated(position);%0A%09      %09%7D catch(IllegalStateException e)%7B%0A%09  %09        e.printStackTrace(); %0A%09  %09    %7D%0A%0A %09%09 mHandler.postDelayed(this, PLAYHEAD_UPDATE_INTERVAL);%0A %09   %7D%0A %09%7D;
 %0A%0A    pu
@@ -3798,19 +3798,21 @@
   if ( m
-Tim
+Handl
 er == nu
@@ -3826,32 +3826,31 @@
             
-    mTim
+%09mHandl
 er = new Tim
@@ -3846,19 +3846,21 @@
 r = new 
-Tim
+Handl
 er();%0A  
@@ -3888,494 +3888,36 @@
    m
-Timer.schedule(new TimerTask() %7B%0A                @Override%0A                public void run() %7B%0A                %09try %7B%0A                %09%09int position = getCurrentPosition();%0A                %09%09if ( position != 0 && mPlayheadUpdateListener != null )%0A                %09%09%09mPlayheadUpdateListener.onPlayheadUpdated(position);%0A                %09%7D catch(IllegalStateException e)%7B%0A            %09        e.printStackTrace(); %0A            %09    %7D%0A                   %0A                %7D%0A            %7D, 0
+Handler.postDelayed(runnable
 , PL
@@ -3941,17 +3941,16 @@
 ERVAL);%0A
-%0A
         
@@ -4363,19 +4363,21 @@
   if ( m
-Tim
+Handl
 er != nu
@@ -4395,54 +4395,45 @@
     
-    mTimer.cancel();%0A            mTimer = null
+%09mHandler.removeCallbacks( runnable )
 ;%0A  
@@ -5403,12 +5403,21 @@
 ;%0A    %7D%0A
+    %0A   %0A
 %7D%0A%0A%0A

use handler instead of timer to improve performance