package eu.mhsl.minenet.minigames.command.privileged;

import eu.mhsl.minenet.minigames.command.PrivilegedCommand;
import eu.mhsl.minenet.minigames.instance.room.Room;
import eu.mhsl.minenet.minigames.score.tournament.RewardConfiguration;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.command.builder.arguments.ArgumentStringArray;
import net.minestom.server.command.builder.arguments.ArgumentType;
import net.minestom.server.command.builder.arguments.ArgumentWord;
import net.minestom.server.command.builder.suggestion.SuggestionEntry;
import net.minestom.server.entity.Player;
import net.minestom.server.item.Material;

import java.util.Arrays;
import java.util.stream.Collectors;

public class SetRewardCommand extends PrivilegedCommand {
    public SetRewardCommand() {
        super("setRewards");

        ArgumentWord materialArgument = ArgumentType.Word("material");
        ArgumentStringArray amountsArgument = ArgumentType.StringArray("amount");

        materialArgument.setSuggestionCallback((sender, context, suggestion) -> {
            Material
                    .values()
                    .stream()
                    .map(material -> new SuggestionEntry(material.name(), Component.text(material.name())))
                    .forEach(suggestion::addEntry);
        });

        addSyntax((sender, context) -> {
            Room
                    .getRoom((Player) sender)
                    .orElseThrow()
                    .getTournament()
                    .setRewardConfiguration(
                            new RewardConfiguration(
                                    Material.fromNamespaceId(context.get(materialArgument)),
                                    Arrays.stream(context.get(amountsArgument)).map(Integer::valueOf).collect(Collectors.toList())
                            )
                    );

            sender.sendMessage(
                    Component.text()
                            .append(Component.text("Belohnung gesetzt:", NamedTextColor.GOLD))
                            .appendNewline()
                            .append(Component.text(context.get(materialArgument), NamedTextColor.AQUA))
                            .appendNewline()
                            .append(Component.text(String.join(", ", context.get(amountsArgument)), NamedTextColor.RED))
                            .build()
            );
        }, materialArgument, amountsArgument);
    }
}
