package org.studentrobotics.ide.checkout;

import java.applet.Applet;
import java.awt.Graphics;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import biz.source_code.base64Coder.Base64Coder;

public class CheckoutApplet extends Applet {

	/**
	 * constant used for serialization
	 */
	private static final long serialVersionUID = 17871929219L;

	private ZipWriteRunner mZwr;

	public static boolean hasDied = false;

	public CheckoutApplet() {
		mZwr = new ZipWriteRunner();
		ExecutorService dispatcher = Executors.newFixedThreadPool(1);
		dispatcher.submit(mZwr);

	}

	public void init() {
	}

	public void stop() {
	}

	public void paint(Graphics g) {
	}

	/**
	 * will attempt to write the passed zip to any found file system with a
	 * .srobo file in its root
	 *
	 * @param zipUrl
	 *            the url of the zip to download
	 * @return 0 on success, 1 on recoverable failure, -1 on irrecoverable failure
	 */
	public int writeZip(String zipUrl) {
		// we can only write to files with a char[] not a byte[]
		try {
			if (hasDied) {
				return -1;
			}

			try {
				URL url = new URL(zipUrl);
				URLConnection conn = url.openConnection();
				byte[] zipBytes = new byte[conn.getContentLength()];
				conn.getInputStream().read(zipBytes);
				

				FutureValue<Boolean> result = mZwr.setZip(zipBytes);
				System.err.println("dispatched");
				System.err.println("result future is: " + result);
				Boolean b = result.get(10L);
				System.err.println("result is " + b);
				System.err.println("got result");
				if (hasDied || b == null) {
					return -1;
				}
				if (b)
					return 0;
				else
					return 1;
			} catch (InterruptedException e) {
				e.printStackTrace();
				if (hasDied) {
					return -1;
				}
				return 1;
			}
		}
		catch (MalformedURLException mue) {
		    mue.printStackTrace();
		    return 1;
		}
		catch (IOException ioe) {
		    ioe.printStackTrace();
		    return 1;
		}
		catch (Throwable t) {
			t.printStackTrace();
			return -1;
		}
	}
}

@@ -564,24 +564,197 @@
  = false;%0A%0A%09
+/**%0A     * constructor on the applet, has to take no arguments b/c applet lifecycle%0A     * sets up the zipwriterunner and fires it off in a different threadpool%0A     */%0A    
 public Check
@@ -767,16 +767,16 @@
 let() %7B%0A
-
 %09%09mZwr =
@@ -890,24 +890,82 @@
 mZwr);%0A%0A%09%7D%0A%0A
+    //these are necessary because of the way applets work%0A
 %09public void
@@ -1634,13 +1634,9 @@
 s);%0A
-%09%09%09%09
 %0A
+
 %0A%09%09%09
@@ -2122,24 +2122,78 @@
 1;%0A%09%09%09%7D%0A%09%09%7D%0A
+        //caught if a passed url to the applet is bad%0A
 %09%09catch (Mal
@@ -2259,32 +2259,92 @@
   return 1;%0A%09%09%7D%0A
+        //caught if the applet throws any kind of exception%0A
 %09%09catch (IOExcep
@@ -2400,20 +2400,145 @@
 turn 1;%0A
-
 %09%09%7D%0A
+        //generic catch is because the security model can deny us access and we won't know%0A        //so we catch a throwable%0A
 %09%09catch 

add some documentation to the applet class