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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
import lombok.experimental.UtilityClass;
import org.bukkit.Material;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@UtilityClass
public class WarpGui {

    public static void openGui(Player player) {
        List<SWListInv.SWListEntry<Warp>> entries = new ArrayList<>();
        Warp.getWarps().forEach(warp -> entries.add(new SWListInv.SWListEntry<>(new SWItem(
                warp.getMat(),
                "§e" + warp.getName(),
                Arrays.asList(
                        BauSystem.MESSAGE.parse("WARP_LOC_X", player, (int) warp.getLocation().getX()),
                        BauSystem.MESSAGE.parse("WARP_LOC_Y", player, (int) warp.getLocation().getY()),
                        BauSystem.MESSAGE.parse("WARP_LOC_Z", player, (int) warp.getLocation().getZ()),
                        BauSystem.MESSAGE.parse("WARP_GUI_DISTANCE", player, (int) warp.getLocation().distance(player.getLocation())),
                        "",
                        BauSystem.MESSAGE.parse("WARP_GUI_LCLICK", player),
                        BauSystem.MESSAGE.parse("WARP_GUI_RCLICK", player)
                ),
                false,
                clickType -> {
                }
        ), warp)));

        SWListInv<Warp> inv = new SWListInv<>(player, BauSystem.MESSAGE.parse("WARP_GUI_NAME", player), false, entries, (clickType, warp) -> {
            if (clickType.isRightClick() && Permission.BUILD.hasPermission(player)) {
                openWarpGui(player, warp);
            } else {
                warp.teleport(player);
            }
        });

        if (entries.isEmpty()) {
            inv.setItem(22, new SWItem(Material.BARRIER, BauSystem.MESSAGE.parse("WARP_GUI_NO", player), clickType -> {
            }));
        }

        inv.open();
    }

    public static void openWarpGui(Player player, Warp warp) {
        SWInventory inv = new SWInventory(player, 9, warp.getName());
        inv.setItem(0, new SWItem(Material.ENDER_PEARL, BauSystem.MESSAGE.parse("WARP_TELEPORT_HOVER", player, warp.getName()), clickType -> {
            player.closeInventory();
            warp.teleport(player);
        }));

        inv.setItem(2, new SWItem(warp.getMat(), "§e" + warp.getMat().name(), clickType -> changeMaterial(player, warp)));

        inv.setItem(8, new SWItem(Material.BARRIER, BauSystem.MESSAGE.parse("WARP_DELETE_HOVER", player, warp.getName()), clickType -> {
            player.closeInventory();
            warp.delete();
        }));

        inv.open();
    }

    public static void changeMaterial(Player player, Warp warp) {
        List<SWListInv.SWListEntry<Material>> materials = new ArrayList<>();
        for (Material value : Material.values()) {
            if (value.isLegacy() || value.isAir() || !value.isBlock() || !value.isItem()) continue;
            materials.add(new SWListInv.SWListEntry<>(new SWItem(value, "§e" + value.name()), value));
        }

        SWListInv<Material> inv = new SWListInv<>(player, BauSystem.MESSAGE.parse("WARP_MATERIAL_CHOOSE", player), materials, (clickType, material) -> {
            player.closeInventory();
            warp.setMat(material);
        });
        inv.open();
    }
}
