package eu.mhsl.minecraft.pixelblocks.commands;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class PixelBlocksCommand implements CommandExecutor, TabCompleter {
    private final List<SubCommand> subCommands = List.of(
        new CreateSubCommand(),
        new GiveSubCommand(),
        new ExitSubCommand(),
        new DestroyAllSubCommand()
    );

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if(!(sender instanceof Player player)) {
            sender.sendMessage(Component.translatable("pixelblocks.command.players-only").color(NamedTextColor.RED));
            return true;
        }

        Optional<SubCommand> subCommand = args.length == 0
            ? Optional.empty()
            : this.subCommands.stream().filter(sub -> sub.name().equalsIgnoreCase(args[0])).findFirst();

        if(subCommand.isEmpty()) {
            this.sendUsage(player);
            return true;
        }

        if(!player.hasPermission(subCommand.get().permission())) {
            player.sendMessage(Component.translatable("pixelblocks.command.no-permission").color(NamedTextColor.RED));
            return true;
        }

        subCommand.get().execute(player, Arrays.copyOfRange(args, 1, args.length));
        return true;
    }

    @Override
    public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if(!(sender instanceof Player player)) return List.of();

        if(args.length == 1) {
            return this.subCommands.stream()
                .filter(sub -> player.hasPermission(sub.permission()))
                .map(SubCommand::name)
                .filter(name -> name.startsWith(args[0].toLowerCase()))
                .toList();
        }

        return this.subCommands.stream()
            .filter(sub -> sub.name().equalsIgnoreCase(args[0]))
            .filter(sub -> player.hasPermission(sub.permission()))
            .findFirst()
            .map(sub -> sub.tabComplete(player, Arrays.copyOfRange(args, 1, args.length)))
            .orElse(List.of());
    }

    private void sendUsage(@NotNull Player player) {
        player.sendMessage(Component.translatable("pixelblocks.command.usage.header").color(NamedTextColor.GOLD));
        this.subCommands.stream()
            .filter(sub -> player.hasPermission(sub.permission()))
            .forEach(sub -> player.sendMessage(Component.text()
                .append(Component.text("/pixelblocks " + sub.name(), NamedTextColor.YELLOW))
                .append(Component.text(" - ", NamedTextColor.GRAY))
                .append(Component.translatable(sub.descriptionKey()).color(NamedTextColor.GRAY))
                .build()));
    }
}
