package dev.drawethree.xprison.gems.commands;

import com.google.common.collect.ImmutableList;
import dev.drawethree.xprison.gems.managers.CommandManager;
import dev.drawethree.xprison.utils.player.PlayerUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.Bukkit;
import java.util.List;

public class GemsSaveCommand extends GemsCommand {

    private static final String COMMAND_NAME = "savegems";
    private static final String[] COMMAND_ALIASES = {"save"};

    public GemsSaveCommand(CommandManager manager) {
        super(manager, COMMAND_NAME, COMMAND_ALIASES);
    }

    @Override
    public boolean execute(CommandSender sender, ImmutableList<String> args) {
        if (!sender.hasPermission("xprison.gems.save")) {
            PlayerUtils.sendMessage(sender, "&cYou do not have permission to execute this command.");
            return true;
        }

        try {
            // If no arguments provided, save all player data
            if (args.isEmpty()) {
                this.commandManager.getPlugin().getGemsManager().saveAllPlayerData(true); // Assuming async saving is desired
                PlayerUtils.sendMessage(sender, "&aGem data for all players has been successfully saved.");
            } else {
                // Save data for a specific player
                String playerName = args.get(0);
                Player targetPlayer = Bukkit.getPlayerExact(playerName);

                if (targetPlayer == null) {
                    PlayerUtils.sendMessage(sender, "&cThe player '" + playerName + "' could not be found.");
                    return true;
                }

                this.commandManager.getPlugin().getGemsManager().saveData(targetPlayer);
                PlayerUtils.sendMessage(sender, "&aGem data for " + playerName + " has been successfully saved.");
            }
        } catch (Exception e) {
            PlayerUtils.sendMessage(sender, "&cError saving gem data. Please check the console for details.");
            e.printStackTrace();
        }

        return true;
    }

    @Override
    public boolean canExecute(CommandSender sender) {
        // Implement logic to determine if the command can be executed by the sender, e.g., based on permissions
        return sender.hasPermission("xprison.gems.save");
    }

    @Override
    public String getUsage() {
        return "/gems savegems - Saves all player gem data.";
    }

    @Override
    public List<String> getTabComplete(List<String> args) {
        // You can provide tab completion options here if relevant, otherwise return an empty list
        return ImmutableList.of();
    }
}
