package org.studentrobotics.ide.checkout;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

public class ZipWriteRunner implements Runnable {
	private ArrayBlockingQueue<FileWriteJob> mWriteQueue = new ArrayBlockingQueue<FileWriteJob>(100);

	private ConcurrentHashMap<Integer, FutureValue<Boolean>> mJobSuccessMap = new ConcurrentHashMap<Integer, FutureValue<Boolean>>();

	/**
     * Sets a new zip to be written to the file system
     */
    public FutureValue<Boolean> setZip(byte[] zip) throws InterruptedException {
		FileWriteJob newJob = new FileWriteJob(zip);
		FutureValue<Boolean> result = new FutureValue<Boolean>();
		mJobSuccessMap.put(newJob.getCode(), result);
		mWriteQueue.put(newJob);
		return result;
	}

	@Override
	public void run() {
		while (true) {
			String path = DriveFinder.findSroboDrive();

			if (path == null) {
				System.err.println("failed to find a drive");

				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				// notify all waiting jobs that they're not going to happen
				while (!mWriteQueue.isEmpty()) {
					FileWriteJob job = mWriteQueue.poll();
					mJobSuccessMap.get(job.getCode()).set(false);
				}

				continue;
			}

			File f = new File(path + "robot.zip");
			System.err.println("path: " + path + "robot.zip");
			System.err.println("tag1");
			System.err.println("file exists? " + f.exists());
			System.err.println("tag2");

			FileWriteJob job;
			System.err.println("tag3");
			try {
				writeFile(f);
				System.err.println("tag4");
			} catch (InterruptedException e1) {
				e1.printStackTrace();
				job = mWriteQueue.poll();
				if (job != null) {
					mJobSuccessMap.get(job.getCode()).set(false);
				}

			}

		}

	}

	private void writeFile(File f) throws InterruptedException {
		
		FileWriteJob job;
		System.err.println("tag5");
		job = mWriteQueue.take();
		System.err.println("tag6");
		try {
		    FileOutputStream fos = new FileOutputStream(f);
			System.err.println("tag7");
			fos.write(job.getContents());
			System.err.println("tag8");
			fos.flush();
			System.err.println("tag9");
			fos.close();
			fos.getFD().sync();
			System.err.println("tag10");
			System.err.println("setting job with code " + job.getCode() + "to true");
			mJobSuccessMap.get(job.getCode()).set(true);
			System.err.println("tag11");
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("failed to do io");
			mJobSuccessMap.get(job.getCode()).set(false);
		}
	}

}

@@ -176,16 +176,52 @@
 eption;%0A
+import java.io.SyncFailedException;%0A
 import j
@@ -595,20 +595,17 @@
 ;%0A%0A%09/**%0A
-    
+%09
  * Sets 
@@ -651,20 +651,14 @@
 tem%0A
-     */%0A    
+%09 */%0A%09
 publ
@@ -2014,11 +2014,9 @@
 n %7B%0A
-%09%09
 %0A
+
 %09%09Fi
@@ -2133,12 +2133,9 @@
 %7B%0A%09%09
-    
+%09
 File
@@ -2335,24 +2335,38 @@
 close();%0A%09%09%09
+%0A%09%09%09try %7B%0A%09%09%09%09
 fos.getFD().
@@ -2369,24 +2369,130 @@
 D().sync();%0A
+%09%09%09%7D catch (SyncFailedException sfe) %7B%0A%09%09%09%09System.err.println(%22sync failed, continuing anyway%22);%0A%09%09%09%7D%0A%09%09%09%0A
 %09%09%09System.er

update java applet to correctly handle os sync fail