package org.kne.cloud.network.klalb;

import java.lang.reflect.Type;
import java.util.Objects;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

public class KLALBConfigItem {
	private String Type;

	public String getType() {
		return Type;
	}

	public void setType(String type) {
		Type = type;
	}

	@Override
	public String toString() {
		return "KLALBConfigItem [Type=" + Type + "]";
	}

	public KLALBConfigItem(String type) {
		super();
		Type = type;
	}

	@Override
	public int hashCode() {
		return Objects.hash(Type);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		KLALBConfigItem other = (KLALBConfigItem) obj;
		return Objects.equals(Type, other.Type);
	}
	public static JsonDeserializer<KLALBConfigItem>getDefaultJsonDeserializer(){
		return new JsonDeserializer<KLALBConfigItem>() {
			
			@Override
			public KLALBConfigItem deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
					throws JsonParseException {
				if(arg0.isJsonObject()) {
					JsonObject jobj=(JsonObject) arg0;
					String s=jobj.get("Type").getAsString();
					switch(s) {
					case "KLALBController":
						return arg2.deserialize(arg0, new TypeToken<KLALBControllerConfigItem>() {}.getType());
					case "SocketBridge":
						return arg2.deserialize(arg0, new TypeToken<SocketBridgeConfigItem>() {}.getType());
					default:
						return arg2.deserialize(arg0, new TypeToken<UnknownKLALBConfigItem>() {}.getType());
					}
				}
				throw new JsonParseException("not a object:"+arg0);
			}
		};
	}
	public static JsonSerializer<KLALBConfigItem>getDefaultJsonSerializer(){
		return new JsonSerializer<KLALBConfigItem>() {
			
			@Override
			public JsonElement serialize(KLALBConfigItem arg0, Type arg1, JsonSerializationContext arg2) {
				if(arg0 instanceof KLALBControllerConfigItem) {
					return arg2.serialize(arg0, new TypeToken<KLALBControllerConfigItem>() {}.getType());
				}
				if(arg0 instanceof SocketBridgeConfigItem) {
					return arg2.serialize(arg0, new TypeToken<SocketBridgeConfigItem>() {}.getType());
				}
				return arg2.serialize(arg0, new TypeToken<UnknownKLALBConfigItem>() {}.getType());
			}
		};
	}
	
	public static void registerToGsonBuilder(GsonBuilder gsonBuilder) {
		gsonBuilder.registerTypeAdapter(KLALBConfigItem.class, getDefaultJsonDeserializer());
		gsonBuilder.registerTypeAdapter(KLALBConfigItem.class, getDefaultJsonSerializer());
	}
}
