/*
    This file is a part of the SteamWar software.

    Copyright (C) 2021  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 org.bukkit.GameMode;

import java.util.function.LongSupplier;

public interface ProtocolWrapper {

    Class<?> itemStack = Reflection.getClass("net.minecraft.world.item.ItemStack");
    Class<?> spawnPacket = Reflection.getClass("net.minecraft.network.protocol.game.ClientboundAddEntityPacket");
    Class<?> spawnLivingPacket = Core.getVersion() > 18 ? ProtocolWrapper.spawnPacket : Reflection.getClass("net.minecraft.network.protocol.game.PacketPlayOutSpawnEntityLiving");
    Class<?> equipmentPacket = Reflection.getClass("net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket");

    Class<?> enumGamemode = Reflection.getClass(Core.getVersion() > 9 ? "net.minecraft.world.level.GameType" : "net.minecraft.WorldSettings$EnumGamemode");
    Reflection.Method getGameModeById = Reflection.getTypedMethod(enumGamemode, null, enumGamemode, int.class);

    // 0: hand, 1: offhand, 2: feet, 3: legs, 4: chest, 5: head
    Object[] itemSlots = Core.getVersion() > 8 ? Reflection.getClass("net.minecraft.world.entity.EnumItemSlot").getEnumConstants() : new Integer[]{0, 0, 1, 2, 3, 4};

    ProtocolWrapper impl = VersionDependent.getVersionImpl(Core.getInstance());

    void setEquipmentPacketStack(Object packet, Object slot, Object stack);

    Object playerInfoPacketConstructor(PlayerInfoAction action, GameProfile profile, GameMode mode);

    default void initTPSWarp(LongSupplier longSupplier) {
        Class<?> systemUtils = Reflection.getClass("net.minecraft.Util");
        Reflection.getField(systemUtils, LongSupplier.class, 0).set(systemUtils, (LongSupplier) () -> System.nanoTime() + longSupplier.getAsLong());
    }

    enum PlayerInfoAction {
        ADD,
        GAMEMODE,
        REMOVE
    }
}
