package mytown.protection.json;

import com.google.common.reflect.TypeToken;
import com.google.gson.*;

import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModContainer;
import myessentials.json.SerializerTemplate;
import mytown.protection.segment.*;
import mytown.util.exceptions.ProtectionParseException;

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

/**
 * This object is used only for loading the protection.
 */
public class Protection {

    public final String modid;
    public final String version;

    public final Segment.Container<Segment> segments = new Segment.Container<Segment>();

    public Protection(String modid) {
        this(modid, "");
    }

    public Protection(String modid, String version) {
        this.modid = modid;
        this.version = version;
    }

    public static class Serializer extends SerializerTemplate<Protection> {

        @Override
        public void register(GsonBuilder builder) {

        }

        @Override
        public JsonElement serialize(Protection protection, Type typeOfSrc, JsonSerializationContext context) {
            JsonObject json = new JsonObject();

            json.addProperty("modid", protection.modid);
            if(!protection.version.equals("")) {
                json.addProperty("version", protection.version);
            }

            json.add("segments", context.serialize(protection.segments, new TypeToken<List<Segment>>() {}.getType()));

            return json;
        }

        @Override
        public Protection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            JsonObject jsonObject = json.getAsJsonObject();
            if(!jsonObject.has("modid")) {
                throw new ProtectionParseException("Missing modid identifier");
            }

            String modid = jsonObject.get("modid").getAsString();
            String version = "";
            if(jsonObject.has("version")) {
                version = jsonObject.get("version").getAsString();
            }
            
            if (!isModLoaded(modid, version)) {
            	return null;
            }

            Protection protection = new Protection(modid, version);

            if(jsonObject.has("segments")) {
                protection.segments.addAll((List<Segment>) context.deserialize(jsonObject.get("segments"), new TypeToken<List<Segment>>() {
                }.getType()));
            }

            return protection;
        }
        
        private static boolean isModLoaded(String modid, String version) {
        	if ("Minecraft".equals(modid)) {
        		return true;
        	}
            for(ModContainer mod : Loader.instance().getModList()) {
                if(mod.getModId().equals(modid) && mod.getVersion().startsWith(version)) {
                    return true;
                }
            }
            return false;
        }
    }
}
@@ -484,24 +484,54 @@
 ring modid;%0A
+    public final String name;%0A
     public f
@@ -699,16 +699,20 @@
 s(modid,
+ %22%22,
  %22%22);%0A  
@@ -743,32 +743,45 @@
 on(String modid,
+ String name,
  String version)
@@ -807,24 +807,50 @@
 id = modid;%0A
+        this.name = name;%0A
         this
@@ -1271,16 +1271,135 @@
 modid);%0A
+            if(!protection.name.equals(%22%22)) %7B%0A                json.addProperty(%22name%22, protection.name);%0A            %7D%0A
         
@@ -2083,32 +2083,62 @@
 .getAsString();%0A
+            String name = %22%22;%0A
             Stri
@@ -2149,24 +2149,141 @@
 ersion = %22%22;
+%0A%0A            if(jsonObject.has(%22name%22)) %7B%0A                name = jsonObject.get(%22name%22).getAsString();%0A            %7D
 %0A           
@@ -2440,24 +2440,30 @@
 oaded(modid,
+ name,
  version)) %7B
@@ -2561,16 +2561,22 @@
 n(modid,
+ name,
  version
@@ -2908,32 +2908,45 @@
 ed(String modid,
+ String name,
  String version)
@@ -3143,16 +3143,50 @@
 odid) &&
+ mod.getName().startsWith(name) &&
  mod.get

Allow use of name also for protect json files.