/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.bausystem.features.util;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;

import java.util.List;
import java.util.stream.Collectors;

@Linked
public class SkullCommand extends SWCommand {

    public SkullCommand() {
        super("skull", "head");
    }

    @Register
    public void giveCommand(@Validator Player p) {
        if (p.getName().startsWith(".")) {
            BauSystem.MESSAGE.send("SKULL_INVALID", p);
            return;
        }
        giveCommand(p, p.getName());
    }

    @Register(description = "SKULL_HELP")
    public void giveCommand(@Validator Player p, @Mapper("player") @ErrorMessage("SKULL_INVALID") String skull) {
        ItemStack is = SWItem.getPlayerSkull(skull).getItemStack();
        SkullMeta sm = (SkullMeta) is.getItemMeta();
        assert sm != null;
        sm.setDisplayName(BauSystem.MESSAGE.parse("SKULL_ITEM", p, skull));
        is.setItemMeta(sm);
        SWUtils.giveItemToPlayer(p, is);
    }

    @Mapper(value = "player", local = true)
    public TypeMapper<String> typeMapper() {
        return new TypeMapper<String>() {
            @Override
            public String map(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                if (s.startsWith(".")) return null;
                return s;
            }

            @Override
            public List<String> tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) {
                return Bukkit.getOnlinePlayers().stream().map(Player::getName).filter(s1 -> !s1.startsWith(".")).collect(Collectors.toList());
            }
        };
    }
}