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;

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

        super.setOnCompletionListener( new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                stop();
            }
        });

        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) {
                        if ( mediaPlayer.isPlaying() ) {
                            mPlayerStateListener.onStateChanged(PlayerStates.PLAY);
                        } else {
                            mPlayerStateListener.onStateChanged(PlayerStates.PAUSE);
                        }
                    }
                });

                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 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() {
        super.start();
        if ( mTimer == null ) {
            mTimer = new Timer();
        }
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mPlayheadUpdateListener.onPlayheadUpdated(getCurrentPosition());
            }
        }, 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();
        if ( mTimer != null ) {
            mTimer.cancel();
            mTimer = null;
        }
        mPlayerStateListener.onStateChanged(PlayerStates.END);
    }

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

    @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 registerProgressUpdate ( OnProgressListener listener ) {
        mProgressListener = listener;
    }
}



@@ -3083,24 +3083,60 @@
 id play() %7B%0A
+    %09if ( !this.isPlaying() ) %7B%0A    
         supe
@@ -3142,24 +3142,28 @@
 er.start();%0A
+    
         if (
@@ -3186,32 +3186,36 @@
 ) %7B%0A            
+  
+  
 mTimer = new Tim
@@ -3216,24 +3216,28 @@
 ew Timer();%0A
+    
         %7D%0A  
@@ -3238,24 +3238,28 @@
   %7D%0A        
+    
 mTimer.sched
@@ -3284,32 +3284,36 @@
 ) %7B%0A            
+ 
+   
 @Override%0A      
@@ -3298,32 +3298,36 @@
       @Override%0A
+    
             publ
@@ -3350,32 +3350,36 @@
                 
+   
+ 
 mPlayheadUpdateL
@@ -3431,34 +3431,42 @@
 ));%0A            
-%7D%0A
+    %7D%0A    
         %7D, 0, PL
@@ -3491,32 +3491,36 @@
 RVAL);%0A%0A        
+ 
+   
 mPlayerStateList
@@ -3555,24 +3555,31 @@
 ates.PLAY);%0A
+    %09%7D%0A
     %7D%0A%0A    @

fix loop sometimes during play