/*
 *   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.region.items;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.BauGuiItem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.flags.ColorMode;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import de.steamwar.linkage.Linked;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Linked
public class ColorBauGuiItem extends BauGuiItem {

    public ColorBauGuiItem() {
        super(2);
    }

    private static Material mapColor(ColorMode color) {
        switch (color) {
            case RED:
                return Material.RED_CONCRETE;
            case CYAN:
                return Material.CYAN_CONCRETE;
            case BLACK:
                return Material.BLACK_CONCRETE;
            case BLUE:
                return Material.BLUE_CONCRETE;
            case BROWN:
                return Material.BROWN_CONCRETE;
            case GRAY:
                return Material.GRAY_CONCRETE;
            case GREEN:
                return Material.GREEN_CONCRETE;
            case LIGHT_BLUE:
                return Material.LIGHT_BLUE_CONCRETE;
            case LIGHT_GRAY:
                return Material.LIGHT_GRAY_CONCRETE;
            case LIME:
                return Material.LIME_CONCRETE;
            case MAGENTA:
                return Material.MAGENTA_CONCRETE;
            case ORANGE:
                return Material.ORANGE_CONCRETE;
            case PINK:
                return Material.PINK_CONCRETE;
            case PURPLE:
                return Material.PURPLE_CONCRETE;
            case WHITE:
                return Material.WHITE_CONCRETE;
            default:
                return Material.YELLOW_CONCRETE;
        }
    }

    @Override
    public ItemStack getItem(Player player) {
        Region region = Region.getRegion(player.getLocation());
        ColorMode mode = region.getRegionData().get(Flag.COLOR).orElse(ColorMode.PINK);
        return new SWItem(mapColor(mode), BauSystem.MESSAGE.parse("REGION_ITEM_COLOR", player, BauSystem.MESSAGE.parse(mode.getChatValue(), player))).getItemStack();
    }

    @Override
    public boolean click(ClickType click, Player p) {
        p.closeInventory();
        ColorMode current = Region.getRegion(p.getLocation()).getRegionData().get(Flag.COLOR).orElse(ColorMode.PINK);
        List<SWListInv.SWListEntry<ColorMode>> items = new ArrayList<>();
        for (ColorMode value : ColorMode.values()) {
            items.add(new SWListInv.SWListEntry<>(new SWItem(mapColor(value), (byte) 0, "§f" + BauSystem.MESSAGE.parse(value.getChatValue(), p), Collections.emptyList(), value == current, clickType -> {
            }), value));
        }
        SWListInv<ColorMode> inv = new SWListInv<>(p, BauSystem.MESSAGE.parse("REGION_ITEM_COLOR_CHOOSE", p), items, (clickType, colorMode) -> {
            p.closeInventory();
            p.performCommand("color " + colorMode.getChatValue());
            update();
        });
        inv.open();
        return false;
    }

    @Override
    public Permission permission() {
        return Permission.OWNER;
    }
}
