package io.github.satoshinm.WebSandboxMC.bukkit;

import io.github.satoshinm.WebSandboxMC.ws.WebSocketServerThread;
import io.netty.channel.Channel;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;

import java.net.InetSocketAddress;

public class WsCommand implements CommandExecutor {
    private WebSocketServerThread webSocketServerThread;

    public WsCommand(WebSocketServerThread webSocketServerThread) {
        this.webSocketServerThread = webSocketServerThread;
    }

    public boolean onCommand(CommandSender sender, Command command, String label, String[] split) {

        if (split.length == 0) {
            sender.sendMessage("/websandbox list -- list all web users connected");
            sender.sendMessage("/websandbox tp <user> -- teleport to given web username");
            sender.sendMessage("/websandbox kick <user> -- disconnect given web username");
            // TODO: reload, reconfig commands
            return true;
        }

        String subcommand = split[0];

        if (subcommand.equals("list")) {
            int size = webSocketServerThread.webPlayerBridge.name2channel.size();
            sender.sendMessage(size + " web player(s) connected:");

            int i = 1;
            for (String name: webSocketServerThread.webPlayerBridge.name2channel.keySet()) { // TODO: sort?
                Channel channel = webSocketServerThread.webPlayerBridge.name2channel.get(name);

                String ip = ((InetSocketAddress) channel.remoteAddress()).getHostString() +
                        ":" + ((InetSocketAddress) channel.remoteAddress()).getPort();

                Entity entity = webSocketServerThread.webPlayerBridge.channelId2Entity.get(channel);
                String entityInfo = "";

                if (entity != null) {
                    entityInfo += " entity "+entity.getEntityId()+" "+entity.getClass().getName();
                    entityInfo += " at " + entity.getLocation();
                }

                sender.sendMessage(i + ". " + name + ", " + ip + entityInfo);
                ++i;
            }
        }

        return true;
    }
}


@@ -142,16 +142,44 @@
 hannel;%0A
+import org.bukkit.Location;%0A
 import o
@@ -205,16 +205,16 @@
 ommand;%0A
-
 import o
@@ -321,16 +321,104 @@
 .Entity;
+%0Aimport org.bukkit.entity.Player;%0Aimport org.bukkit.event.player.PlayerShearEntityEvent;
 %0A%0Aimport
@@ -1913,16 +1913,21 @@
 (channel
+.id()
 );%0A     
@@ -2282,16 +2282,16 @@
    ++i;%0A
-
         
@@ -2297,32 +2297,1458 @@
      %7D%0A        %7D
+ else if (subcommand.equals(%22tp%22)) %7B%0A            if (split.length %3C 2) %7B%0A                sender.sendMessage(%22Usage: /websandbox tp %3Cuser%3E%22);%0A                return true;%0A            %7D%0A            String name = split%5B1%5D;%0A%0A            Channel channel = webSocketServerThread.webPlayerBridge.name2channel.get(name);%0A            if (channel == null) %7B%0A                sender.sendMessage(%22No such web user: %22 + name);%0A                return true;%0A            %7D%0A%0A            Entity entity = webSocketServerThread.webPlayerBridge.channelId2Entity.get(channel.id());%0A            if (entity == null) %7B%0A                sender.sendMessage(%22Web user %22+name+%22 is connected, but has no spawned entity.%22);%0A                // TODO: allow tracking and teleporting independently of the Bukkit entity?%0A                return true;%0A            %7D%0A%0A            Location location = entity.getLocation();%0A            if (!(sender instanceof Player)) %7B%0A                sender.sendMessage(%22Web user %22+name+%22's entity is located at: %22+location);%0A                sender.sendMessage(%22Web user %22+name+%22's entity is %22+entity);%0A                return true;%0A            %7D%0A            Player player = (Player) sender;%0A            player.sendMessage(%22Teleporting you to %22+name+%22 at %22+location+%22 for %22+entity);%0A            player.teleport(entity);%0A%0A            webSocketServerThread.sendLine(channel, %22T,%22+player.getDisplayName()+%22 teleported to you%22);%0A        %7D
 %0A%0A        return

Add /websandbox tp, to teleport to a web user's entity location