package com.github.games647.changeskin.sponge;

import com.github.games647.changeskin.core.SkinData;
import com.github.games647.changeskin.sponge.ChangeSkinSponge;
import com.github.games647.changeskin.sponge.tasks.SkinUpdater;

import org.spongepowered.api.Platform.Type;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.network.ChannelBuf;
import org.spongepowered.api.network.RawDataListener;
import org.spongepowered.api.network.RemoteConnection;

public class BungeeCordListener implements RawDataListener {

    private final ChangeSkinSponge plugin;

    public BungeeCordListener(ChangeSkinSponge plugin) {
        this.plugin = plugin;
    }

    @Override
    public void handlePayload(ChannelBuf data, RemoteConnection connection, Type side) {
        Player player = (Player) connection;
        
        String subchannel = data.readUTF();

        if ("UpdateSkin".equalsIgnoreCase(subchannel)) {
            plugin.getLogger().info("Received instant update request from BungeeCord. "
                    + "This request should only be send if the command /setskin was invoked");
            updateSkin(data, player);
        } else if ("PermissionsCheck".equalsIgnoreCase(subchannel)) {
            checkPermissions(player, data);
        }
    }

    private boolean updateSkin(ChannelBuf data, Player player) throws IllegalArgumentException {
        String encodedData = data.readUTF();
        if (encodedData.equalsIgnoreCase("null")) {
            plugin.getGame().getScheduler().createTaskBuilder()
                    .execute(new SkinUpdater(plugin, null, player, null)).submit(plugin);
            return true;
        }

        String signature = data.readUTF();
        Player receiver = player;
        try {
            String playerName = data.readUTF();
            receiver = plugin.getGame().getServer().getPlayer(playerName).orElseGet(null);
            plugin.getLogger().info("Instant update for {}", playerName);
        } catch (Exception ex) {
            plugin.getLogger().warn("You are using an outdated ChangeSkin spigot version");
        }

        SkinData skinData = new SkinData(encodedData, signature);
        plugin.getGame().getScheduler().createTaskBuilder()
                    .execute(new SkinUpdater(plugin, null, receiver, skinData)).submit(plugin);
        return false;
    }

    private void checkPermissions(Player player, ChannelBuf dataInput) {
//        int skinId = dataInput.readInteger();
//        String encodedData = dataInput.readUTF();
//        String encodedSignature = dataInput.readUTF();
//
//        //continue on success only
//        String receiverUUID = dataInput.readUTF();
//
//        SkinData targetSkin = new SkinData(encodedData, encodedSignature);
//        if (checkBungeePerms(player, UUID.fromString(receiverUUID), targetSkin.getUuid())) {
//            ChannelBuf out = Chann
//            out.writeUTF("PermissionsSuccess");
//            out.writeInteger(skinId);
//            out.writeUTF(encodedData);
//            out.writeUTF(encodedSignature);
//            out.writeUTF(receiverUUID);
//
//            plugin.getPluginChannel().sendTo(player, out);
//        } else {
//            ByteArrayDataOutput out = ByteStreams.newDataOutput();
//            out.writeUTF("PermissionsFailure");
//            player.sendPluginMessage(plugin, plugin.getPluginContainer().getId(), out.toByteArray());
//        }
    }

//    private boolean checkBungeePerms(Player player, UUID receiver, UUID targetSkinUUID) {
//        if (player.getUniqueId().equals(receiver)) {
//            return player.hasPermission(plugin.getPluginContainer().getId() + ".command.setskin")
//                && plugin.checkPermission(player, targetSkinUUID, false);
//        } else {
//            return player.hasPermission(plugin.getPluginContainer().getId() + ".command.setskin.other")
//                    && plugin.checkPermission(player, targetSkinUUID, false);
//        }
//    }
}

@@ -222,16 +222,40 @@
 dater;%0A%0A
+import java.util.UUID;%0A%0A
 import o
@@ -2462,26 +2462,24 @@
 ataInput) %7B%0A
-//
         int 
@@ -2508,26 +2508,24 @@
 dInteger();%0A
-//
         Stri
@@ -2558,26 +2558,24 @@
 .readUTF();%0A
-//
         Stri
@@ -2609,37 +2609,33 @@
 nput.readUTF();%0A
-//%0A//
+%0A
         //contin
@@ -2653,18 +2653,16 @@
 ss only%0A
-//
         
@@ -2704,21 +2704,17 @@
 dUTF();%0A
-//%0A//
+%0A
         
@@ -2772,34 +2772,32 @@
 odedSignature);%0A
-//
         if (chec
@@ -2869,26 +2869,24 @@
 tUuid())) %7B%0A
-//
             
@@ -2889,33 +2889,64 @@
     
-ChannelBuf out = Chann%0A//
+plugin.getPluginChannel().sendTo(player, (out) -%3E %7B%0A    
     
@@ -2985,26 +2985,28 @@
 sSuccess%22);%0A
-//
+    
             
@@ -3031,18 +3031,20 @@
 kinId);%0A
-//
+    
         
@@ -3074,18 +3074,20 @@
 dData);%0A
-//
+    
         
@@ -3118,26 +3118,28 @@
 Signature);%0A
-//
+    
             
@@ -3170,13 +3170,41 @@
 D);%0A
-//%0A//
+            %7D);%0A        %7D else %7B%0A
     
@@ -3256,104 +3256,23 @@
 er, 
+(
 out)
-;%0A//        %7D else
+ -%3E
  %7B%0A
-//
     
-        ByteArrayDataOutput out = ByteStreams.newDataOutput();%0A//
     
@@ -3315,18 +3315,16 @@
 lure%22);%0A
-//
         
@@ -3331,100 +3331,12 @@
     
-player.sendPluginMessage(plugin, plugin.getPluginContainer().getId(), out.toByteArray()
+%7D
 );%0A
-//
     
@@ -3348,18 +3348,16 @@
 %0A    %7D%0A%0A
-//
     priv
@@ -3434,26 +3434,24 @@
 SkinUUID) %7B%0A
-//
         if (
@@ -3491,18 +3491,16 @@
 ver)) %7B%0A
-//
         
@@ -3589,18 +3589,16 @@
 tskin%22)%0A
-//
         
@@ -3655,34 +3655,32 @@
 inUUID, false);%0A
-//
         %7D else %7B
@@ -3680,18 +3680,16 @@
  else %7B%0A
-//
         
@@ -3784,18 +3784,16 @@
 other%22)%0A
-//
         
@@ -3862,18 +3862,16 @@
 false);%0A
-//
         
@@ -3872,18 +3872,16 @@
       %7D%0A
-//
     %7D%0A%7D%0A

Add missing bungee permission check for sponge