package moe.oko.opennaw.command;

import moe.oko.opennaw.OpenNAW;
import moe.oko.opennaw.util.CommandHelper;
import org.bukkit.ChatColor;
import org.bukkit.Location;
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 CityCommand 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 cityList = OpenNAW.getInstance().getCityHandler().getCityList();

        switch (args[0]) {
            case "add": {
                var location = ((Player) sender).getLocation();
                OpenNAW.getInstance().getCityHandler().addCity(args[1], args[2], new Location(location.getWorld() ,location.getBlockX(), location.getBlockY(), location.getBlockZ()));
                sender.sendMessage("Created city " + args[1] + " at " + location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ());
                return true;
            }
            case "remove": {
                OpenNAW.getInstance().getCityHandler().removeCity(args[1]);
                sender.sendMessage("Removed city " + args[1]);
                return true;
            }
            case "list": {
                final var msg = cityList.size() == 0
                        ? "There are no cities"
                        : "There are " + cityList.size() + " cities: " + cityList;
                sender.sendMessage(msg);
                return true;
            }
            case "debug": {
                var city = OpenNAW.getInstance().getCityHandler().fetchCityViaName(args[1]);
                OpenNAW.getInstance().getCityHandler().setCityOwner(city, OpenNAW.getInstance().getNationHandler().fetchNationViaName(args[2]));
                sender.sendMessage(ChatColor.GREEN + "Done!" + "Owner set to " + city.getOwner().getName() );
            }
        }
        return true;
    }

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