package org.twipnetwork.colorfulwords;

import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class ColorCommand implements CommandExecutor {
    private final ColorfulWords plugin;
    private final MiniMessage mm;

    public ColorCommand(ColorfulWords plugin) {
        this.mm = MiniMessage.miniMessage();
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("This command can only be used by players.");
            return true;
        }

        if (args.length != 1) {
            player.sendMessage(mm.deserialize("<red>Usage: /color <hex>|reset</red>"));
            return true;
        }

        String colorHex = args[0];
        if (colorHex.equalsIgnoreCase("reset")) {
            plugin.resetPlayerColor(player.getUniqueId());
            player.sendMessage(mm.deserialize("<green>Your chat color has been reset.</green>"));
        } else if (colorHex.matches("^#[a-fA-F0-9]{6}$")) {
            plugin.setPlayerColor(player.getUniqueId(), colorHex);
            player.sendMessage(mm.deserialize("<green>Your chat color has been set to </green><" + colorHex + ">" + colorHex + "<green>."));
        } else {
            player.sendMessage(mm.deserialize("<red>Invalid hex color. Please use a valid hex code.</red>"));
        }

        return true;
    }
}
