/*
 *   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 de.steamwar.network.packets.NetworkPacket;
import de.steamwar.network.packets.client.ExecuteCommandPacket;
import de.steamwar.network.packets.client.ImALobbyPacket;
import de.steamwar.network.packets.client.InventoryCallbackPacket;
import de.steamwar.network.packets.client.PrepareSchemPacket;
import de.steamwar.network.packets.common.FightEndsPacket;
import de.steamwar.network.packets.common.FightInfoPacket;
import de.steamwar.network.packets.server.*;
import lombok.SneakyThrows;

import java.lang.reflect.Constructor;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

import static de.steamwar.RandomGenerator.generateRandom;

public class AllPackets {

    public static final Class<? extends NetworkPacket>[] packets = new Class[]{
            ExecuteCommandPacket.class,
            ImALobbyPacket.class,
            InventoryCallbackPacket.class,
            PrepareSchemPacket.class,
            FightEndsPacket.class,
            FightInfoPacket.class,
            BaumemberUpdatePacket.class,
            CloseInventoryPacket.class,
            InventoryPacket.class,
            PingPacket.class,
            StartingServerPacket.class
    };

    @SneakyThrows
    public static List<NetworkPacket> getAllPackets() {
        List<NetworkPacket> packets = new ArrayList<>();

        for (Class<? extends NetworkPacket> packet : AllPackets.packets) {
            Constructor<? extends NetworkPacket> longConstructor = (Constructor<? extends NetworkPacket>) packet.getConstructors()[0];
            for (Constructor<?> constructor : packet.getConstructors()) {
                if (constructor.getParameterCount() > longConstructor.getParameterCount()) {
                    longConstructor = (Constructor<? extends NetworkPacket>) constructor;
                }
            }

            Object[] args = new Object[longConstructor.getParameterCount()];
            Parameter[] parameters = longConstructor.getParameters();
            for (int i = 0; i < parameters.length; i++) {
                Parameter parameter = parameters[i];
                parameter.getName();
                args[i] = generateRandom(parameter.getType());
            }

            NetworkPacket packetInstance = longConstructor.newInstance(args);
            packets.add(packetInstance);
        }
        return packets;
    }
}
