package cn.citycraft.GsonAgent.normal;

import java.io.Reader;
import java.lang.reflect.Type;

import com.google.gson.JsonSyntaxException;

import cn.citycraft.GsonAgent.api.Gson;
import cn.citycraft.GsonAgent.api.exception.JsonIOException;
import cn.citycraft.GsonAgent.normal.bind.TypeAdapterFactoryHandle;
import cn.citycraft.GsonAgent.normal.bind.TypeAdapterHandle;
import cn.citycraft.GsonAgent.normal.stream.JsonReaderHandle;
import cn.citycraft.GsonAgent.normal.stream.JsonWriterHandle;

/**
 * 新版本Gson代理类
 *
 * @since 2016年1月5日 下午12:57:20
 * @author 喵♂呜
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class GsonHandle implements Gson {
	final com.google.gson.Gson handle = new com.google.gson.Gson();

	@Override
	public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Class<T> classOfT) throws JsonSyntaxException {
		return getHandle().fromJson(((JsonElementHandle) json).getHandle(), classOfT);
	}

	@Override
	public <T> T fromJson(final cn.citycraft.GsonAgent.api.JsonElement json, final Type typeOfT) throws JsonSyntaxException {
		return getHandle().fromJson(((JsonElementHandle) json).getHandle(), typeOfT);
	}

	@Override
	public <T> T fromJson(final cn.citycraft.GsonAgent.api.stream.JsonReader reader, final Type typeOfT) throws JsonIOException, JsonSyntaxException {
		return getHandle().fromJson(((JsonReaderHandle) reader).getHandle(), typeOfT);
	}

	@Override
	public <T> T fromJson(final Reader json, final Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
		return getHandle().fromJson(json, classOfT);
	}

	@Override
	public <T> T fromJson(final Reader json, final Type typeOfT) throws JsonIOException, JsonSyntaxException {
		return getHandle().fromJson(json, typeOfT);
	}

	@Override
	public <T> T fromJson(final String json, final Class<T> classOfT) throws JsonSyntaxException {
		return getHandle().fromJson(json, classOfT);
	}

	@Override
	public <T> T fromJson(final String json, final Type typeOfT) throws JsonSyntaxException {
		return getHandle().fromJson(json, typeOfT);
	}

	@Override
	public <T> TypeAdapterHandle<T> getAdapter(final Class<T> type) {
		return new TypeAdapterHandle(getHandle().getAdapter(type));
	}

	@Override
	public <T> TypeAdapterHandle<T> getAdapter(final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> type) {
		return new TypeAdapterHandle(getHandle().getAdapter((com.google.gson.reflect.TypeToken<T>) type.getHandle()));
	}

	@Override
	public <T> TypeAdapterHandle<T> getDelegateAdapter(final cn.citycraft.GsonAgent.api.TypeAdapterFactory skipPast, final cn.citycraft.GsonAgent.api.reflect.TypeToken<T> type) {
		return new TypeAdapterHandle(getHandle().getDelegateAdapter(((TypeAdapterFactoryHandle) skipPast).getHandle(), (com.google.gson.reflect.TypeToken<T>) type.getHandle()));
	}

	public com.google.gson.Gson getHandle() {
		return handle;
	}

	@Override
	public String toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement) {
		return getHandle().toJson(((JsonElementHandle) jsonElement).getHandle());
	}

	@Override
	public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final Appendable writer) throws JsonIOException {
		getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), writer);
	}

	@Override
	public void toJson(final cn.citycraft.GsonAgent.api.JsonElement jsonElement, final cn.citycraft.GsonAgent.api.stream.JsonWriter writer) throws JsonIOException {
		getHandle().toJson(((JsonElementHandle) jsonElement).getHandle(), ((JsonWriterHandle) writer).getHandle());
	}

	@Override
	public String toJson(final Object src) {
		return getHandle().toJson(src);
	}

	@Override
	public void toJson(final Object src, final Appendable writer) throws JsonIOException {
		getHandle().toJson(src, writer);
	}

	@Override
	public String toJson(final Object src, final Type typeOfSrc) {
		return getHandle().toJson(src, typeOfSrc);
	}

	@Override
	public void toJson(final Object src, final Type typeOfSrc, final Appendable writer) throws JsonIOException {
		getHandle().toJson(src, typeOfSrc, writer);
	}

	@Override
	public void toJson(final Object src, final Type typeOfSrc, final cn.citycraft.GsonAgent.api.stream.JsonWriter writer) throws JsonIOException {
		getHandle().toJson(src, typeOfSrc, ((JsonWriterHandle) writer).getHandle());
	}

	@Override
	public JsonElementHandle toJsonTree(final Object src) {
		return new JsonElementHandle(getHandle().toJsonTree(src));
	}

	@Override
	public JsonElementHandle toJsonTree(final Object src, final Type typeOfSrc) {
		return new JsonElementHandle(getHandle().toJsonTree(src, typeOfSrc));
	}
}
