package com.freshplanet.ane.AirAACPlayer.functions;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;
import com.freshplanet.ane.AirAACPlayer.Extension;
import com.freshplanet.ane.AirAACPlayer.ExtensionContext;

public class LoadUrlFunction implements FREFunction 
{
    private ExtensionContext extensionContext;

    @Override
    public FREObject call(FREContext context, FREObject[] args) 
    {   
        extensionContext = (ExtensionContext) context;

        try
        {
            String url = args[0].getAsString();
            extensionContext.dispatchStatusEventAsync("LOGGING", "[Info] Loading url " + url + " - existing url: " + extensionContext.getMediaUrl() + " - isLoaded: " + extensionContext.isLoaded());

            if( !extensionContext.isLoaded() ) 
            {
                if ( url.equals(extensionContext.getMediaUrl()) )
                {

                    // This is a tough one - don't want to reload unless loading error

                    extensionContext.dispatchStatusEventAsync("LOGGING", "[Info] LOADING AGAIN!!");
                    extensionContext.getPlayer().release();
                    extensionContext.setMediaUrl(null);
                    extensionContext.setPlayer(null);
                }

            	extensionContext.setMediaUrl(url);
            	extensionContext.getPlayer().reset();
            	extensionContext.getPlayer().setDataSource(url);
            	extensionContext.getPlayer().prepareAsync();

            	OnPreparedListener listener = new OnPreparedListener() 
                {
                    @Override
                    public void onPrepared(MediaPlayer mp) 
                    {
                        extensionContext.setIsLoaded(true);
                        extensionContext.dispatchStatusEventAsync("LOGGING", "[Info] Player prepared");
                        extensionContext.dispatchStatusEventAsync("AAC_PLAYER_PREPARED", "OK");
                    }
                };
                extensionContext.getPlayer().setOnPreparedListener(listener);

                MediaPlayer.OnErrorListener onErrorListener = new MediaPlayer.OnErrorListener()   
                {  
                    @Override  
                    public boolean onError(MediaPlayer mp, int what, int extra)   
                    {  
                        extensionContext.dispatchStatusEventAsync("LOGGING", "[Info] Player error: " + what + " : " + extra);
                        extensionContext.dispatchStatusEventAsync("AAC_PLAYER_ERROR", "ERROR");
                        return true;  
                    }  
                }; 

                extensionContext.getPlayer().setOnErrorListener(onErrorListener);

            } 
            else 
            {
            	extensionContext.dispatchStatusEventAsync("LOGGING", "[Info] Player already prepared");
                extensionContext.dispatchStatusEventAsync("AAC_PLAYER_PREPARED", "OK");
            }

        }
        catch (Exception e)
        {
            extensionContext.dispatchStatusEventAsync("LOGGING", "[Error] Error on load");
            Log.e(Extension.TAG, "Error loading url: " + e.getMessage());
        }
        
        return null;
    }
}

@@ -788,76 +788,292 @@
 %22 - 
-existing url: %22 + extensionContext.getMediaUrl() + %22 - isLoaded: %22 +
+STATE: %22 + extensionContext.getState());%0A%0A            if ( extensionContext.getState() == ExtensionContext.LOADING )%0A            %7B%0A                return null;%0A            %7D%0A            else if( extensionContext.getState() == ExtensionContext.PREPARED ) %0A            %7B%0A               
  ext
@@ -1090,22 +1090,79 @@
 ext.
+d
 is
-Loaded()
+patchStatusEventAsync(%22LOGGING%22, %22%5BInfo%5D Player already prepared%22
 );%0A
-%0A
     
@@ -1173,13 +1173,12 @@
     
-if( !
+    
 exte
@@ -1194,21 +1194,93 @@
 ext.
+d
 is
-Loaded() ) 
+patchStatusEventAsync(%22AAC_PLAYER_PREPARED%22, %22OK%22);%0A            %7D%0A            else
 %0A   
@@ -1315,19 +1315,8 @@
 f ( 
-url.equals(
 exte
@@ -1335,19 +1335,41 @@
 .get
-MediaUrl())
+State() == ExtensionContext.ERROR
  )%0A 
@@ -1385,17 +1385,16 @@
       %7B%0A
-%0A
         
@@ -1417,67 +1417,30 @@
 his 
-is a tough one - don't want to reload unless loading error%0A
+should never be called
 %0A   
@@ -1600,64 +1600,8 @@
 ();%0A
-                    extensionContext.setMediaUrl(null);%0A
     
@@ -1673,56 +1673,8 @@
  %7D%0A%0A
-            %09extensionContext.setMediaUrl(url);%0A
     
@@ -1839,16 +1839,85 @@
 Async();
+%0A                extensionContext.setState(ExtensionContext.LOADING);
 %0A%0A      
@@ -2157,21 +2157,39 @@
 .set
-IsLoaded(true
+State(ExtensionContext.PREPARED
 );%0A 
@@ -2757,32 +2757,107 @@
             %7B  %0A
+                        extensionContext.setState(ExtensionContext.ERROR);%0A
                 
@@ -3145,17 +3145,16 @@
     %7D; %0A
-%0A
         
@@ -3230,245 +3230,8 @@
 er);
-%0A%0A            %7D %0A            else %0A            %7B%0A            %09extensionContext.dispatchStatusEventAsync(%22LOGGING%22, %22%5BInfo%5D Player already prepared%22);%0A                extensionContext.dispatchStatusEventAsync(%22AAC_PLAYER_PREPARED%22, %22OK%22);
 %0A   
@@ -3232,32 +3232,33 @@
 );%0A            %7D
+ 
 %0A%0A        %7D%0A    

Change Load call… - add on error listener - Do action based on state