/*
 *   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.SWCommand;
import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Arrays;

@Linked
public class SlotCommand extends SWCommand {

    public SlotCommand() {
        super("slot", "s");
        addDefaultHelpMessage("OTHER_NOCLIP_SLOT_INFO");
    }

    @Register
    public void slotCommand(@Validator Player player, Integer slot) {
        if (slot < 1 || slot > 9) {
            BauSystem.MESSAGE.send("OTHER_SLOT_INVALID_SLOT", player);
            return;
        }
        player.getInventory().setHeldItemSlot(slot - 1);
    }

    @Register(value = "pick", description = "OTHER_NOCLIP_SLOT_HELP_PICK")
    public void eyedropBlock(@Validator Player player) {
        Block block = player.getTargetBlockExact(6);
        ItemStack itemStack;
        if (block == null) {
            return;
        }
        itemStack = new ItemStack(block.getType());
        SWUtils.giveItemToPlayer(player, itemStack);
    }

    @Register(value = "drop", description = "OTHER_NOCLIP_SLOT_HELP_DROP")
    public void dropBlock(@Validator Player player) {
        player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
    }

    @ClassMapper(value = Integer.class, local = true)
    private TypeMapper<Integer> integerTypeMapper() {
        return SWCommandUtils.createMapper(s -> {
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException e) {
                return null;
            }
        }, (commandSender, s) -> Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9"));
    }
}
