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

import com.mojang.authlib.properties.Property;
import de.steamwar.core.BountifulWrapper;
import de.steamwar.network.handlers.InventoryHandler;
import de.steamwar.network.packets.PacketHandler;
import de.steamwar.network.packets.common.PlayerSkinResponsePacket;
import de.steamwar.network.packets.server.*;
import de.steamwar.sql.BauweltMember;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

public class CoreNetworkHandler extends PacketHandler {

    public CoreNetworkHandler() {
        super();
        new InventoryHandler().register();
    }

    @Handler
    public void handleBaumemberUpdatePacket(BaumemberUpdatePacket packet) {
        BauweltMember.clear();
    }

    @Handler
    public void handleCloseInventoryPacket(CloseInventoryPacket packet) {
        Player player = Bukkit.getPlayer(SteamwarUser.byId(packet.getPlayerId()).getUUID());
        if (player != null) {
            player.closeInventory();
        }
    }

    @Handler
    public void handleInventoryPacket(InventoryPacket packet) {
        InventoryHandler.handleInventoryPacket(packet);
    }

    @Handler
    public void handleAnvilPacket(AnvilInventoryPacket packet) {
        InventoryHandler.handleAnvilInventoryPacket(packet);
    }

    @Handler
    public void handlePingPacket(PingPacket packet) {
        UUID uuid = SteamwarUser.byId(packet.getId()).getUUID();
        if (Bukkit.getPlayer(uuid) != null) {
            Player player = Bukkit.getPlayer(uuid);
            BountifulWrapper.impl.playPling(player);
        }
    }

    @Handler
    public void handleLocaleChange(LocaleInvalidationPacket packet) {
        SteamwarUser.invalidate(packet.getPlayerId());
    }

    public static final Map<UUID, Property> SKIN_DATA_PROMISES = new LinkedHashMap<UUID, Property>() {
        @Override
        protected boolean removeEldestEntry(Map.Entry<UUID, Property> eldest) {
            return size() > 100;
        }
    };

    @Handler
    public void handlePlayerSkinResponse(PlayerSkinResponsePacket packet) {
        if (!SKIN_DATA_PROMISES.containsKey(packet.getUuid())) return;
        SKIN_DATA_PROMISES.put(packet.getUuid(), new Property("textures", packet.getSkin(), packet.getSignature()));
    }
}
