package me.proxylink.plugin.paper;

import me.proxylink.plugin.PluginRuntime;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

final class DirtSimplePaperCommand implements CommandExecutor, TabCompleter {
    private static final List<String> SUBCOMMANDS = List.of("status", "doctor", "webwizard");

    private final PluginRuntime runtime;

    DirtSimplePaperCommand(PluginRuntime runtime) {
        this.runtime = runtime;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        String subcommand = args.length == 0 ? "status" : args[0].toLowerCase(Locale.ROOT);
        switch (subcommand) {
            case "status" -> sendBlock(sender, "DirtSimpleP2P Status", runtime.statusLines());
            case "doctor" -> sendBlock(sender, "DirtSimpleP2P Doctor", runtime.doctorLines());
            case "webwizard" -> sendBlock(sender, "DirtSimpleP2P Web Wizard", runtime.toggleWebWizardLines());
            default -> sender.sendMessage(ChatColor.RED + "Usage: /dsp2p status, /dsp2p doctor, or /dsp2p webwizard");
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        if (args.length != 1) {
            return List.of();
        }
        String prefix = args[0].toLowerCase(Locale.ROOT);
        List<String> matches = new ArrayList<>();
        for (String subcommand : SUBCOMMANDS) {
            if (subcommand.startsWith(prefix)) {
                matches.add(subcommand);
            }
        }
        return matches;
    }

    private static void sendBlock(CommandSender sender, String title, List<String> lines) {
        sender.sendMessage(ChatColor.GOLD + title);
        for (String line : lines) {
            sender.sendMessage(ChatColor.GRAY + "- " + ChatColor.WHITE + line);
        }
    }
}
