package com.xtremelabs.robolectric.shadows;

import java.util.concurrent.*;

import android.os.AsyncTask;
import android.os.ShadowAsyncTaskBridge;

import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;

@Implements(AsyncTask.class)
public class ShadowAsyncTask<Params, Progress, Result> {

    @RealObject private AsyncTask<Params, Progress, Result> realAsyncTask;
    
    public static final int EXECUTE_USING_EXECUTOR = 1;
    private final FutureTask<Result> future;
    private final BackgroundWorker worker;
    private AsyncTask.Status status = AsyncTask.Status.PENDING;
    private int executor = 0;
    
	public ShadowAsyncTask() {
		worker = new BackgroundWorker();
		future = new FutureTask<Result>(worker) {
        	@Override
        	protected void done() {
                status = AsyncTask.Status.FINISHED;
				try {
					final Result result = get();
					Robolectric.getUiThreadScheduler().post(new Runnable() {
						@Override public void run() {
							getBridge().onPostExecute(result);
						}
					});
				} catch (CancellationException e) {
					Robolectric.getUiThreadScheduler().post(new Runnable() {
						@Override public void run() {
							getBridge().onCancelled();
						}
					});
				} catch (InterruptedException e) {
					// Ignore.
				} catch (Throwable t) {
					throw new RuntimeException("An error occured while executing doInBackground()",
							t.getCause());
				}
        	}
        };
	}

	@Implementation
    public boolean isCancelled() {
        return future.isCancelled();
    }

    @Implementation
    public boolean cancel(boolean mayInterruptIfRunning) {
        return future.cancel(mayInterruptIfRunning);
    }

    @Implementation
    public Result get() throws InterruptedException, ExecutionException {
        return future.get();
    }

    @Implementation
    public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return future.get(timeout, unit);
    }

    @Implementation
    public AsyncTask<Params, Progress, Result> execute(final Params... params) {
        status = AsyncTask.Status.RUNNING;
        getBridge().onPreExecute();

        worker.params = params;

        Robolectric.getBackgroundScheduler().post(new Runnable() {
            @Override public void run() {
            	future.run();
            }
        });

        return realAsyncTask;
    }

    @Implementation
    public AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, final Params... params) {
    	executor = EXECUTE_USING_EXECUTOR;
    	return this.execute( params );
    }

    @Implementation
    public AsyncTask.Status getStatus() {
        return status;
    }

    /**
     * Enqueue a call to {@link AsyncTask#onProgressUpdate(Object[])} on UI looper (or run it immediately
     * if the looper it is not paused).
     *
     * @param values The progress values to update the UI with.
     * @see AsyncTask#publishProgress(Object[])
     */
    @Implementation
    public void publishProgress(final Progress... values) {
        Robolectric.getUiThreadScheduler().post(new Runnable() {
            @Override public void run() {
                getBridge().onProgressUpdate(values);
            }
        });
    }

    private ShadowAsyncTaskBridge<Params, Progress, Result> getBridge() {
        return new ShadowAsyncTaskBridge<Params, Progress, Result>(realAsyncTask);
    }

    private final class BackgroundWorker implements Callable<Result> {
    	Params[] params;
		@Override
		public Result call() throws Exception {
			return getBridge().doInBackground(params);
		}
	}
    
    public final int getExecuteType(){
    	return executor;
    }
}

@@ -42,40 +42,8 @@
 s;%0A%0A
-import java.util.concurrent.*;%0A%0A
 impo
@@ -67,16 +67,16 @@
 ncTask;%0A
+
 import a
@@ -108,17 +108,16 @@
 Bridge;%0A
-%0A
 import c
@@ -325,16 +325,278 @@
 bject;%0A%0A
+import java.util.concurrent.Callable;%0Aimport java.util.concurrent.CancellationException;%0Aimport java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.FutureTask;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.concurrent.TimeoutException;%0A%0A
 @Impleme
@@ -753,69 +753,9 @@
 sk;%0A
-    %0A    public static final int EXECUTE_USING_EXECUTOR = 1;
 %0A
+
     
@@ -906,43 +906,9 @@
 NG;%0A
-    private int executor = 0;%0A    
 %0A
+
 %09pub
@@ -2697,216 +2697,8 @@
  %7D%0A%0A
-    @Implementation%0A    public AsyncTask%3CParams, Progress, Result%3E executeOnExecutor(Executor exec, final Params... params) %7B%0A    %09executor = EXECUTE_USING_EXECUTOR;%0A    %09return this.execute( params );%0A    %7D%0A
 %0A   
@@ -3709,78 +3709,5 @@
 %0A%09%7D%0A
-    %0A    public final int getExecuteType()%7B%0A    %09return executor;%0A    %7D%0A%7D%0A
+%7D

Remove implmentation of executeOnExecutor due to lower sdk version