package shadersmod.installer;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Json {

	public static class JsValue {
		void print(Writer wr, int indent) throws IOException {
		}
		void println(Writer wr, int indent) throws IOException {
			wr.append("\n");
			for(int i=0; i<indent; ++i)
				wr.append("  ");
		}
	}
	
	public static class JsString extends JsValue {
		String str;
		public JsString() {}
		public JsString(String str) {
			this.str = str;
		}
		void print(Writer wr, int indent) throws IOException {
			wr.append("\"").append(str).append("\"");
		}
	}
	public static class JsNumber extends JsValue {
		String str;
		void print(Writer wr, int indent) throws IOException {
			wr.append(str);
		}
	}
	public static class JsBoolean extends JsValue {
		String str;
		public JsBoolean() {}
		public JsBoolean(String str) {
			this.str = str;
		}
		void print(Writer wr, int indent) throws IOException {
			wr.append(str);
		}
	}
	public static class JsArray extends JsValue {
		ArrayList<JsValue> elements = new ArrayList<JsValue>();
		void print(Writer wr, int indent) throws IOException {
			wr.append("[");
			int indent1 = indent+1;
			for (int n=elements.size(), i=0; i<n; ++i) {
				JsValue e = elements.get(i);
				println(wr, indent1);
				e.print(wr, indent1);
				if (i+1<n)
					wr.append(",");
			}
			println(wr, indent);
			wr.append("]");
		}
	}
	public static class JsField extends JsValue {
		String name;
		JsValue value;
		void print(Writer wr, int indent) throws IOException {
			wr.append("\"").append(name).append("\": ");
			value.print(wr, indent);
		}
	}
	public static class JsObject extends JsValue {
		ArrayList<JsField> members = new ArrayList<JsField>();
		Map<String,JsField> membersMap = new HashMap<String,JsField>();
		void print(Writer wr, int indent) throws IOException {
			wr.append("{");
			int indent1 = indent+1;
			for (int n=members.size(), i=0; i<n; ++i) {
				JsField m = members.get(i);
				println(wr, indent1);
				m.print(wr, indent1);
				if (i+1<n)
					wr.append(",");
			}
			println(wr, indent);
			wr.append("}");
		}
		void put(String name, JsValue value) {
			JsField jsf = membersMap.get(name);
			if (jsf==null) {
				jsf = new JsField();
				jsf.name = name;
				jsf.value = value;
				members.add(jsf);
				membersMap.put(name, jsf);
			} else {
				jsf.value = value;
			}
		}
		JsValue get(String name) {
			JsField jsf = membersMap.get(name);
			if (jsf==null) {
				return null;
			} else {
				return jsf.value;
			}
		}
	}
	
}
