package cn.citycraft.GsonAgent.nms;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.citycraft.GsonAgent.api.JsonArray;
import cn.citycraft.GsonAgent.api.utils.Utils;

/**
 *
 * @since 2016年1月5日 下午1:49:21
 * @author 喵♂呜
 */
public class JsonArrayHandle extends JsonElementHandle implements JsonArray {

	private static Field elementsField;

	static {
		try {
			elementsField = net.minecraft.util.com.google.gson.JsonArray.class.getDeclaredField("elements");
			elementsField.setAccessible(true);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	private final net.minecraft.util.com.google.gson.JsonArray handle;

	private List<net.minecraft.util.com.google.gson.JsonElement> elements;

	public JsonArrayHandle() {
		this(new net.minecraft.util.com.google.gson.JsonArray());
	}

	@SuppressWarnings("unchecked")
	protected JsonArrayHandle(final net.minecraft.util.com.google.gson.JsonArray handle) {
		this.handle = handle;
		try {
			elements = (List<net.minecraft.util.com.google.gson.JsonElement>) elementsField.get(this.handle);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void add(final cn.citycraft.GsonAgent.api.JsonElement element) {
		handle.add(((JsonElementHandle) element).getHandle());
	}

	@Override
	public void addAll(final cn.citycraft.GsonAgent.api.JsonArray array) {
		handle.addAll(((JsonArrayHandle) array).handle);
	}

	@Override
	public cn.citycraft.GsonAgent.api.JsonArray deepCopy() {
		return new JsonArrayHandle(Utils.deepCopyObject(this.handle));
	}

	@Override
	public boolean equals(final Object o) {
		return handle.equals(o);
	}

	@Override
	public cn.citycraft.GsonAgent.api.JsonElement get(final int i) {
		return new JsonElementHandle(handle.get(i));
	}

	@Override
	public BigDecimal getAsBigDecimal() {
		return handle.getAsBigDecimal();
	}

	@Override
	public BigInteger getAsBigInteger() {
		return handle.getAsBigInteger();
	}

	@Override
	public boolean getAsBoolean() {
		return handle.getAsBoolean();
	}

	@Override
	public byte getAsByte() {
		return handle.getAsByte();
	}

	@Override
	public char getAsCharacter() {
		return handle.getAsCharacter();
	}

	@Override
	public double getAsDouble() {
		return handle.getAsDouble();
	}

	@Override
	public float getAsFloat() {
		return handle.getAsFloat();
	}

	@Override
	public int getAsInt() {
		return handle.getAsInt();
	}

	@Override
	public long getAsLong() {
		return handle.getAsLong();
	}

	@Override
	public Number getAsNumber() {
		return handle.getAsNumber();
	}

	@Override
	public short getAsShort() {
		return handle.getAsShort();
	}

	@Override
	public String getAsString() {
		return handle.getAsString();
	}

	@Override
	public int hashCode() {
		return handle.hashCode();
	}

	@Override
	public Iterator<cn.citycraft.GsonAgent.api.JsonElement> iterator() {
		try {
			final List<cn.citycraft.GsonAgent.api.JsonElement> result = new ArrayList<>();
			final Iterator<net.minecraft.util.com.google.gson.JsonElement> iterator = this.elements.iterator();
			while (iterator.hasNext()) {
				result.add(new JsonElementHandle(iterator.next()));
			}
			return result.iterator();
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int size() {
		return handle.size();
	}

}
