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

import com.google.gson.JsonParser;
import de.steamwar.inventory.SWAnvilInv;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.network.NetworkSender;
import de.steamwar.network.packets.PacketHandler;
import de.steamwar.network.packets.client.AnvilAnswerPacket;
import de.steamwar.network.packets.client.InventoryCallbackPacket;
import de.steamwar.network.packets.server.AnvilInventoryPacket;
import de.steamwar.network.packets.server.InventoryPacket;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;

import java.util.HashMap;
import java.util.Map;

public class InventoryHandler extends PacketHandler {

    @Handler
    public static void handleInventoryPacket(InventoryPacket packet) {
        Player player = Bukkit.getPlayer(SteamwarUser.byId(packet.getPlayer()).getUUID());
        Map<Integer, SWItem> items = new HashMap<>();
        packet.getItems().forEach((i, item) -> {
            @SuppressWarnings("deprecation") SWItem it = SWItem.getItemFromJson(new JsonParser().parse(item).getAsJsonObject());
            it.setCallback(click -> NetworkSender.send(InventoryCallbackPacket.builder().owner(packet.getPlayer()).position(i).type(InventoryCallbackPacket.CallbackType.CLICK).clickType(InventoryCallbackPacket.ClickType.getByName(click.name())).build(), player));
            items.put(i, it);
        });

        SWInventory inventory = new SWInventory(player, packet.getSize(), packet.getTitle(), items);
        inventory.addCloseCallback(click -> {
            if (player.getOpenInventory().getType() != InventoryType.CHEST) {
                NetworkSender.send(InventoryCallbackPacket.builder().owner(packet.getPlayer()).type(InventoryCallbackPacket.CallbackType.CLOSE).build(), player);
            }
        });
        inventory.open();
    }

    @Handler
    public static void handleAnvilInventoryPacket(AnvilInventoryPacket packet) {
        Player player = Bukkit.getPlayer(SteamwarUser.byId(packet.getPlayerId()).getUUID());

        SWAnvilInv inv = new SWAnvilInv(player, packet.getTitle(), packet.getDefaultText());
        if (packet.getMaterial() != null && !packet.getMaterial().isEmpty()) {
            inv.setItem(SWItem.getMaterial(packet.getMaterial()));
        }

        inv.setCallback(s -> NetworkSender.send(AnvilAnswerPacket.builder().playerId(packet.getPlayerId()).action(AnvilAnswerPacket.Action.ANSWER).text(s).build()));
        inv.addLeftCallback(() -> NetworkSender.send(AnvilAnswerPacket.builder().playerId(packet.getPlayerId()).action(AnvilAnswerPacket.Action.LEFT_CLICK).build()));
        inv.addCloseCallback(() -> NetworkSender.send(AnvilAnswerPacket.builder().playerId(packet.getPlayerId()).action(AnvilAnswerPacket.Action.CLOSE).build()));

        inv.open();
    }
}
