package io.github.satoshinm.WebSandboxMC.bukkit;

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

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.id());
                String entityInfo = "";

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

                sender.sendMessage(i + ". " + name + ", " + ip + entityInfo);
                ++i;
            }
        } else if (subcommand.equals("tp")) {
            if (split.length < 2) {
                sender.sendMessage("Usage: /websandbox tp <user>");
                return true;
            }
            String name = split[1];

            Channel channel = webSocketServerThread.webPlayerBridge.name2channel.get(name);
            if (channel == null) {
                sender.sendMessage("No such web user: " + name);
                return true;
            }

            Entity entity = webSocketServerThread.webPlayerBridge.channelId2Entity.get(channel.id());
            if (entity == null) {
                sender.sendMessage("Web user "+name+" is connected, but has no spawned entity.");
                // TODO: allow tracking and teleporting independently of the Bukkit entity?
                return true;
            }

            Location location = entity.getLocation();
            if (!(sender instanceof Player)) {
                sender.sendMessage("Web user "+name+"'s entity is located at: "+location);
                sender.sendMessage("Web user "+name+"'s entity is "+entity);
                return true;
            }
            Player player = (Player) sender;
            player.sendMessage("Teleporting you to "+name+" at "+location+" for "+entity);
            player.teleport(entity);

            webSocketServerThread.sendLine(channel, "T,"+player.getDisplayName()+" teleported to you");
        }

        return true;
    }
}


@@ -792,25 +792,24 @@
 plit) %7B%0A
-%0A
         
 if (spli
@@ -804,410 +804,56 @@
     
-if (split.length == 0) %7B%0A            sender.sendMessage(%22/websandbox list -- list all web users connected%22);%0A            sender.sendMessage(%22/websandbox tp %3Cuser%3E -- teleport to given web username%22);%0A            sender.sendMessage(%22/websandbox kick %3Cuser%3E -- disconnect given web username%22);%0A            // TODO: reload, reconfig commands%0A            return true;%0A        %7D%0A%0A        String subcommand =
+String subcommand = split.length == 0 ? %22help%22 :
  spl
@@ -3380,28 +3380,1095 @@
    %7D
-%0A%0A        return tru
+ else if (subcommand.equals(%22kick%22)) %7B%0A            if (split.length %3C 2) %7B%0A                sender.sendMessage(%22Usage: /websandbox kick %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            sender.sendMessage(%22Kicking web client %22+name);%0A            webSocketServerThread.sendLine(channel,%22T,You were kicked by %22+sender.getName());%0A            webSocketServerThread.webPlayerBridge.clientDisconnected(channel);%0A            return true;%0A        %7D else %7B // help%0A            sender.sendMessage(%22/websandbox list -- list all web users connected%22);%0A            sender.sendMessage(%22/websandbox tp %3Cuser%3E -- teleport to given web username%22);%0A            sender.sendMessage(%22/websandbox kick %3Cuser%3E -- disconnect given web username%22);%0A            // TODO: reload, reconfig commands%0A        %7D%0A        return fals
 e;%0A 

Add /websandbox kick, to disconnect a web user