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

import lombok.Getter;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemDisplay;
import org.bukkit.inventory.ItemStack;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * !! This class cannot be used in Versions lower than or equal to 1.19.4 !!
 */
@Getter
public class RItemDisplay extends RDisplay {

    public static final ItemStack DEFAULT_ITEM_STACK = new ItemStack(Material.AIR);

    private ItemStack itemStack;
    private ItemDisplay.ItemDisplayTransform itemDisplayTransform;

    public RItemDisplay(REntityServer server, Location location) {
        super(server, EntityType.ITEM_DISPLAY, location);
        this.itemStack = DEFAULT_ITEM_STACK;
        this.itemDisplayTransform = ItemDisplay.ItemDisplayTransform.NONE;
        server.addEntity(this);
    }

    @Override
    protected void postSpawn(Consumer<Object> packetSink) {
        super.postSpawn(packetSink);
        sendPacket(packetSink, this::getItemStack, this::getItemDisplayTransform);
    }

    public void setItemStack(ItemStack itemStack) {
        this.itemStack = itemStack;
        sendPacket(updatePacketSink, this::getItemStack);
    }

    private static final EntityDataAccessor<net.minecraft.world.item.ItemStack> itemStackWatcher = new EntityDataAccessor<>(23, EntityDataSerializers.ITEM_STACK);

    private void getItemStack(boolean ignoreDefault, EntityDataPacketBuilder packetSink) {
        if (ignoreDefault || !itemStack.equals(DEFAULT_ITEM_STACK)) {
            packetSink.add(itemStackWatcher, CraftItemStack.asNMSCopy(itemStack));
        }
    }

    private static final EntityDataAccessor<Byte> itemDisplayTransformWatcher = new EntityDataAccessor<>(24, EntityDataSerializers.BYTE);

    public void setItemDisplayTransform(ItemDisplay.ItemDisplayTransform itemDisplayTransform) {
        this.itemDisplayTransform = itemDisplayTransform;
        sendPacket(updatePacketSink, this::getItemDisplayTransform);
    }

    private void getItemDisplayTransform(boolean ignoreDefault, EntityDataPacketBuilder packetSink) {
        if (ignoreDefault || itemDisplayTransform != ItemDisplay.ItemDisplayTransform.NONE) {
            packetSink.add(itemDisplayTransformWatcher, (byte) itemDisplayTransform.ordinal());
        }
    }
}
