package org.redalert1741.steamworks;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.JsonReader;

public class JsonAutonomous extends Autonomous
{
	private JsonElement auto;
	private List<AutoInstruction> instructions;
	
	private enum Unit { Seconds, Milliseconds, EncoderTicks, Rotations, Inches, Feet, Invalid };
	
	private static class AutoInstruction
	{
		public String type;
		public Unit unit;
		public double amount;
		public List<Double> args;
		public AutoInstruction(String type, Unit unit, double amount, List<Double> args)
		{
			this.type = type;
			this.unit = unit;
			this.amount = amount;
			this.args = args;
		}
	}
	
	public JsonAutonomous(String file)
	{
		instructions = new ArrayList<AutoInstruction>();
		try
		{
			auto = new JsonParser().parse(new JsonReader(new FileReader(new File(file))));
			JsonElement inner = auto.getAsJsonObject().get("auto");
			if(inner.isJsonArray())
			{
				for(JsonElement e : inner.getAsJsonArray())
				{
					JsonObject o = e.getAsJsonObject();
					List<Double> tmp = new ArrayList<Double>();
					for(JsonElement e2 : o.get("args").getAsJsonArray())
					{
						tmp.add(e2.getAsDouble());
					}
					instructions.add(new AutoInstruction(o.get("type").getAsString(), parseUnit(o.get("unit").getAsString()), o.get("amount").getAsDouble(), tmp));
				}
			}
		}
		catch (JsonIOException | JsonSyntaxException | FileNotFoundException e)
		{
			e.printStackTrace();
		}
	}
	
	public static Unit parseUnit(String in)
	{
		return Unit.valueOf(in);
	}

	@Override
	public void run()
	{
		for(AutoInstruction ai : instructions)
		{
			if(ai.type.equals("drive"))
			{
				System.out.println("Drive x: " + ai.args.get(0) + ", y: " + ai.args.get(1) + ", z: " + ai.args.get(2) + ", " + ai.amount + " " + ai.unit);
			}
			else if(ai.type.equals("gear"))
			{
				System.out.println("Gear " + (ai.args.get(0)==1?"open":"close"));
			}
			else if(ai.type.equals("wait"))
			{
				System.out.println("Wait " + ai.amount + " " + ai.unit);
			}
		}
	}

}

@@ -417,16 +417,55 @@
 der;%0D%0A%0D%0A
+import edu.wpi.first.wpilibj.Timer;%0D%0A%0D%0A
 public c
@@ -581,16 +581,59 @@
 tions;%0D%0A
+%09private int step;%0D%0A%09private Timer timer;%0D%0A
 %09%0D%0A%09priv
@@ -1100,16 +1100,54 @@
 e)%0D%0A%09%7B%0D%0A
+%09%09step = -1;%0D%0A%09%09timer = new Timer();%0D%0A
 %09%09instru
@@ -2039,20 +2039,82 @@
 %0D%0A%09%7B%0D%0A%09%09
-for(
+if(step == -1)%0D%0A%09%09%7B%0D%0A%09%09%09timer.reset();%0D%0A%09%09%09timer.start();%0D%0A%09%09%7D%0D%0A%09%09
 AutoInst
@@ -2124,17 +2124,17 @@
 tion ai 
-:
+=
  instruc
@@ -2138,25 +2138,29 @@
 ructions
-)%0D%0A%09%09%7B%0D%0A%09
+.get(step);%0D%0A
 %09%09if(ai.
@@ -2180,29 +2180,27 @@
 drive%22))%0D%0A%09%09
-%09
 %7B%0D%0A
-%09
 %09%09%09System.ou
@@ -2329,28 +2329,135 @@
 .unit);%0D%0A%09%09%09
+if(ai.unit.equals(Unit.Seconds))%0D%0A%09%09%09%7B%0D%0A%09%09%09%09if(timer.get() %3C ai.amount)%0D%0A%09%09%09%09%7B%0D%0A%09%09%09%09%09Robot.%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09
 %7D%0D%0A
-%09
 %09%09else if(ai
@@ -2477,29 +2477,27 @@
 %22gear%22))%0D%0A%09%09
-%09
 %7B%0D%0A
-%09
 %09%09%09System.ou
@@ -2556,21 +2556,19 @@
 %22));%0D%0A%09%09
-%09
 %7D%0D%0A
-%09
 %09%09else i
@@ -2596,21 +2596,19 @@
 t%22))%0D%0A%09%09
-%09
 %7B%0D%0A
-%09
 %09%09%09Syste
@@ -2660,22 +2660,16 @@
 unit);%0D%0A
-%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%7D%0D

Add auto for absolute swerve in seconds