/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Zoe Lee (Kihira)
 *
 * See LICENSE for full License
 */

package uk.kihira.tails.common.network;

import com.google.gson.JsonSyntaxException;
import com.mojang.util.UUIDTypeAdapter;
import io.netty.buffer.ByteBuf;
import uk.kihira.tails.common.PartsData;
import uk.kihira.tails.common.Tails;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import java.util.UUID;

public class PlayerDataMessage implements IMessage {

    private UUID uuid;
    private PartsData partsData;
    private boolean shouldRemove;

    public PlayerDataMessage() {}
    public PlayerDataMessage(UUID uuid, PartsData partsData, boolean shouldRemove) {
        this.uuid = uuid;
        this.partsData = partsData;
        this.shouldRemove = shouldRemove;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        uuid = UUIDTypeAdapter.fromString(ByteBufUtils.readUTF8String(buf));
        String tailInfoJson = ByteBufUtils.readUTF8String(buf);
        try {
            partsData = Tails.gson.fromJson(tailInfoJson, PartsData.class);
        } catch (JsonSyntaxException e) {
            Tails.logger.warn(e);
        }
    }

    @Override
    public void toBytes(ByteBuf buf) {
        ByteBufUtils.writeUTF8String(buf, UUIDTypeAdapter.fromUUID(uuid));
        String tailInfoJson = Tails.gson.toJson(this.partsData);
        ByteBufUtils.writeUTF8String(buf, tailInfoJson);
    }

    public static class Handler implements IMessageHandler<PlayerDataMessage, IMessage> {

        @Override
        public IMessage onMessage(PlayerDataMessage message, MessageContext ctx) {
            if (message.partsData != null) {
                if (message.shouldRemove) Tails.proxy.removePartsData(message.uuid);
                else {
                    Tails.proxy.addPartsData(message.uuid, message.partsData);
                    //Tell other clients about the change
                    if (ctx.side.isServer()) {
                        Tails.networkWrapper.sendToAll(new PlayerDataMessage(message.uuid, message.partsData, false));
                    }
                }
            }
            return null;
        }
    }
}

@@ -144,16 +144,55 @@
 twork;%0A%0A
+import com.google.common.base.Strings;%0A
 import c
@@ -303,86 +303,8 @@
 uf;%0A
-import uk.kihira.tails.common.PartsData;%0Aimport uk.kihira.tails.common.Tails;%0A
 impo
@@ -568,16 +568,94 @@
 Context;
+%0Aimport uk.kihira.tails.common.PartsData;%0Aimport uk.kihira.tails.common.Tails;
 %0A%0Aimport
@@ -1243,16 +1243,72 @@
 g(buf);%0A
+        if (!Strings.isNullOrEmpty(tailInfoJson)) %7B%0A    
         
@@ -1325,16 +1325,20 @@
         
+ 
+   
 partsDat
@@ -1393,16 +1393,20 @@
 class);%0A
+    
         
@@ -1447,24 +1447,28 @@
             
+    
 Tails.logger
@@ -1481,25 +1481,70 @@
 e);%0A        
+   
-%7D
+ %7D%0A        %7D%0A        else partsData = null;
 %0A    %7D%0A%0A    
@@ -1696,16 +1696,41 @@
 foJson =
+ partsData == null ? %22%22 :
  Tails.g
@@ -2017,57 +2017,8 @@
 ) %7B%0A
-            if (message.partsData != null) %7B%0A    
     
@@ -2110,23 +2110,46 @@
     
-    
 else 
-%7B%0A    
+if (message.partsData != null) %7B%0A
     
@@ -2235,20 +2235,16 @@
         
-    
 //Tell o
@@ -2285,28 +2285,24 @@
             
- 
-   
 if (ctx.side
@@ -2312,28 +2312,24 @@
 Server()) %7B%0A
-    
             
@@ -2435,30 +2435,8 @@
 ));%0A
-                    %7D%0A
     

Fix logic in PlayerDataMesssage to allow proper deleting with null data