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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.linkage.BauGuiItem;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.core.SWPlayer;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;

import java.util.Arrays;
import java.util.Locale;

@Linked
public class SelectBauGuiItem extends BauGuiItem {

    public SelectBauGuiItem() {
        super(13);
    }

    private static void selectExtension(Player p, RegionType type) {
        p.closeInventory();
        SWInventory inv = new SWInventory(p, 9, BauSystem.MESSAGE.parse("SELECT_ITEM_CHOOSE_EXTENSION", p));
        inv.setItem(2, new SWItem(Material.END_STONE, BauSystem.MESSAGE.parse("SELECT_ITEM_NORMAL_EXTENSION", p), clickType -> selectFinish(p, type, RegionExtensionType.NORMAL)));
        inv.setItem(6, new SWItem(Material.PISTON, BauSystem.MESSAGE.parse("SELECT_ITEM_EXTENDED_EXTENSION", p), clickType -> selectFinish(p, type, RegionExtensionType.EXTENSION)));
        inv.open();
    }

    private static void selectFinish(Player p, RegionType type, RegionExtensionType extensionType) {
        p.closeInventory();
        SWPlayer.of(p).setComponent(new LastSelect(type, extensionType));
        p.performCommand("select " + type.name() + " " + extensionType.toString());
    }

    @Override
    public ItemStack getItem(Player player) {
        LastSelect last = SWPlayer.of(player).getComponentOrDefault(LastSelect.class, () -> new LastSelect(RegionType.BUILD, RegionExtensionType.NORMAL));
        return new SWItem(Material.SCAFFOLDING, BauSystem.MESSAGE.parse("SELECT_ITEM_SELECT", player), Arrays.asList(BauSystem.MESSAGE.parse("SELECT_ITEM_AUSWAHL", player, BauSystem.MESSAGE.parse(last.type.getChatValue(), player), last.extensionType.name()), BauSystem.MESSAGE.parse("SELECT_ITEM_RIGHT_CLICK", player)), false, clickType -> {
        }).getItemStack();
    }

    @Override
    public boolean click(ClickType click, Player p) {
        if (click == ClickType.RIGHT) {
            p.closeInventory();
            SWInventory inv = new SWInventory(p, 9, BauSystem.MESSAGE.parse("SELECT_ITEM_CHOOSE_SELECTION", p));
            inv.setItem(2, new SWItem(Material.REDSTONE_BLOCK, BauSystem.MESSAGE.parse("SELECT_ITEM_BAURAHMEN", p), clickType -> selectExtension(p, RegionType.BUILD)));
            inv.setItem(4, new SWItem(Material.LECTERN, BauSystem.MESSAGE.parse("SELECT_ITEM_BAUPLATTFORM", p), clickType -> selectFinish(p, RegionType.NORMAL, RegionExtensionType.NORMAL)));
            inv.setItem(6, new SWItem(Material.END_STONE, BauSystem.MESSAGE.parse("SELECT_ITEM_TESTBLOCK", p), clickType -> selectExtension(p, RegionType.TESTBLOCK)));
            inv.open();
        } else {
            p.closeInventory();
            LastSelect last = SWPlayer.of(p).getComponentOrDefault(LastSelect.class, () -> new LastSelect(RegionType.BUILD, RegionExtensionType.NORMAL));
            p.performCommand("select " + last.getType().name() + " " + last.getExtensionType().toString());
        }
        return false;
    }

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

    @AllArgsConstructor
    private static class LastSelect implements SWPlayer.Component {
        @Getter
        private final RegionType type;
        @Getter
        private final RegionExtensionType extensionType;

        @Override
        public String toString() {
            return type.getChatValue() + " " + extensionType.name().toLowerCase(Locale.ROOT);
        }
    }
}
