package moe.oko.opennaw.command;

import moe.oko.opennaw.OpenNAW;
import moe.oko.opennaw.util.CommandHelper;
import moe.oko.opennaw.util.GuiHelper;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import java.util.List;

public class NationCommand implements TabExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(CommandHelper.NOT_PLAYER);
            return true;
        }

        if (args.length < 1) { return false; }

        var nationList = OpenNAW.getInstance().getNationHandler().getNationList();

        switch (args[0]) {
            case "add" -> {
                if (args.length != 3) {
                    sender.sendMessage("Specify a player and group");
                    return true;
                }

                var group = OpenNAW.getInstance().getGroupHandler().getGroupFromString(args[2]);

                if (group == null) {
                    sender.sendMessage("Group " + args[2] + " does not exist");
                    return true;
                }

                OpenNAW.getInstance().getNationHandler().addNation(args[1], group);
                sender.sendMessage("Created nation " + args[1]);
                return true;
            }
            case "list" -> {
                final var msg = nationList.size() == 0
                        ? "There are no nations"
                        : "There are " + nationList.size() + " nations: " + nationList;
                sender.sendMessage(msg);
                return true;
            }
            case "remove" -> {
                OpenNAW.getInstance().getNationHandler().removeNation(args[1]);
                sender.sendMessage("Removed nation " + args[1]);
                return true;
            }
            case "join" -> {
                if(!(sender instanceof Player)) {
                    sender.sendMessage("This is an in-game command only.");
                    return true;
                }
                var player = (Player) sender;
                player.openInventory(GuiHelper.getJoinMenu().getInventory());
                return true;
            }
            case "leave" -> {
                var player = args.length < 3
                        ? ((Player) sender).getPlayer()
                        : OpenNAW.getInstance().getServer().getOfflinePlayer(args[2]);
                OpenNAW.getInstance().getGroupHandler().removePlayerFromGroup(player);

                sender.sendMessage("Removed " + player.getName() + " from their nation");
                return true;
            }
            case "modify" -> {
                if(!(sender instanceof Player)) {
                    sender.sendMessage("This is an in-game command only.");
                    return true;
                }
                if(args.length == 1) {
                    sender.sendMessage(ChatColor.RED + "You need to provide a nation in your argument.");
                }
                var player = (Player) sender;
                player.openInventory(GuiHelper.getNationModMenu(OpenNAW.getInstance().getNationHandler().fetchNationViaName(args[1])).getInventory());
                return true;
            }
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        return switch (args.length) {
            case 1 -> List.of("add", "join", "leave", "list", "modify", "remove");
            case 2 -> OpenNAW.getInstance().getNationHandler().getNationStringList();
            default -> null;
        };
    }
}
