/*
 *   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 de.steamwar.core.BountifulWrapper;
import de.steamwar.core.Core;
import de.steamwar.core.ProtocolWrapper;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.Material;
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 Object itemStackWatcher = BountifulWrapper.impl.getDataWatcherObject(Core.getVersion() >= 21 ? 23 : 22, ProtocolWrapper.itemStack);
    private void getItemStack(boolean ignoreDefault, BiConsumer<Object, Object> packetSink) {
        if (ignoreDefault || !itemStack.equals(DEFAULT_ITEM_STACK)) {
            packetSink.accept(itemStackWatcher, asNMSCopy.invoke(null, itemStack));
        }
    }

    private static final Object itemDisplayTransformWatcher = BountifulWrapper.impl.getDataWatcherObject(Core.getVersion() >= 21 ? 24 : 23, Byte.class);
    public void setItemDisplayTransform(ItemDisplay.ItemDisplayTransform itemDisplayTransform) {
        this.itemDisplayTransform = itemDisplayTransform;
        sendPacket(updatePacketSink, this::getItemDisplayTransform);
    }

    private void getItemDisplayTransform(boolean ignoreDefault, BiConsumer<Object, Object> packetSink) {
        if (ignoreDefault || itemDisplayTransform != ItemDisplay.ItemDisplayTransform.NONE) {
            packetSink.accept(itemDisplayTransformWatcher, (byte) itemDisplayTransform.ordinal());
        }
    }
}
