package org.twipnetwork.colorfulwords;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class ColorCommand implements CommandExecutor {
    private final ColorfulWords plugin;

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

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

        Player player = (Player) sender;
        if (args.length != 1) {
            player.sendMessage(ChatColor.RED + "Usage: /color <hex>|reset");
            return true;
        }

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

        return true;
    }
}
