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

import de.steamwar.Reflection;
import com.mojang.authlib.GameProfile;
import com.mojang.datafixers.util.Pair;
import org.bukkit.GameMode;

import java.util.Collections;
import java.util.EnumMap;
import java.util.List;

public class ProtocolWrapper18 implements ProtocolWrapper {

    private static final Reflection.Field<List> equipmentStack = Reflection.getField(equipmentPacket, List.class, 0);
    @Override
    public void setEquipmentPacketStack(Object packet, Object slot, Object stack) {
        equipmentStack.set(packet, Collections.singletonList(new Pair<>(slot, stack)));
    }

    private static final Class<?> playerInfoPacket = Reflection.getClass("net.minecraft.network.protocol.game.PacketPlayOutPlayerInfo");
    private static final Class<?> playerInfoActionClass = Reflection.getClass("net.minecraft.network.protocol.game.PacketPlayOutPlayerInfo$EnumPlayerInfoAction");
    private static final Reflection.Field<?> playerInfoAction = Reflection.getField(playerInfoPacket, playerInfoActionClass, 0);
    private static final Reflection.Field<List> playerInfoData = Reflection.getField(playerInfoPacket, List.class, 0);
    private static final EnumMap<PlayerInfoAction, Object> actions = new EnumMap<>(PlayerInfoAction.class);
    static {
        Object[] nativeActions = playerInfoActionClass.getEnumConstants();
        actions.put(PlayerInfoAction.ADD, nativeActions[0]);
        actions.put(PlayerInfoAction.GAMEMODE, nativeActions[1]);
        actions.put(PlayerInfoAction.REMOVE, nativeActions[4]);
    }
    private static final Class<?> iChatBaseComponent = Reflection.getClass("net.minecraft.network.chat.Component");
    private static final Reflection.Constructor playerInfoDataConstructor = Reflection.getConstructor("net.minecraft.network.protocol.game.PacketPlayOutPlayerInfo$PlayerInfoData", GameProfile.class, int.class, enumGamemode, iChatBaseComponent);

    @Override
    @SuppressWarnings("deprecation")
    public Object playerInfoPacketConstructor(PlayerInfoAction action, GameProfile profile, GameMode mode) {
        Object packet = Reflection.newInstance(playerInfoPacket);
        playerInfoAction.set(packet, actions.get(action));
        playerInfoData.set(packet, Collections.singletonList(playerInfoDataConstructor.invoke(profile, 0, ProtocolWrapper.getGameModeById.invoke(null, mode.getValue()), null)));
        return packet;
    }
}
