/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2026 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 lombok.Setter;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;

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 RInteraction extends REntity implements RInteractableEntity<RInteraction> {

    protected final Consumer<Object> updatePacketSink = o -> server.updateEntity(this, o);

    @Setter
    private REntityActionListener<RInteraction> callback = null;

    private float interactionWidth = 1.0f;
    private float interactionHeight = 1.0f;
    private boolean responsive = false;

    public RInteraction(REntityServer server, Location location) {
        super(server, EntityType.INTERACTION, location);
        server.addEntity(this);
    }

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

    @SafeVarargs
    protected final void sendPacket(Consumer<Object> packetSink, BiConsumer<Boolean, EntityDataPacketBuilder>... dataSinkSinks) {
        EntityDataPacketBuilder builder = entityDataPacket();
        boolean ignoreDefault = packetSink == updatePacketSink;
        for (BiConsumer<Boolean, EntityDataPacketBuilder> dataSinkSink : dataSinkSinks) {
            dataSinkSink.accept(ignoreDefault, builder);
        }
        if (!builder.isEmpty()) builder.send(packetSink);
    }

    public void setInteractionWidth(float interactionWidth) {
        this.interactionWidth = interactionWidth;
        sendPacket(updatePacketSink, this::getInteractionWidthData);
    }

    private static final EntityDataAccessor<Float> interactionWidthWatcher = new EntityDataAccessor<>(8, EntityDataSerializers.FLOAT);

    private void getInteractionWidthData(boolean ignoreDefault, EntityDataPacketBuilder dataSink) {
        if (ignoreDefault || interactionWidth != 1.0) {
            dataSink.add(interactionWidthWatcher, interactionWidth);
        }
    }

    public void setInteractionHeight(float interactionHeight) {
        this.interactionHeight = interactionHeight;
        sendPacket(updatePacketSink, this::getInteractionHeightData);
    }

    private static final EntityDataAccessor<Float> interactionHeightWatcher = new EntityDataAccessor<>(9, EntityDataSerializers.FLOAT);

    private void getInteractionHeightData(boolean ignoreDefault, EntityDataPacketBuilder dataSink) {
        if (ignoreDefault || interactionHeight != 1.0) {
            dataSink.add(interactionHeightWatcher, interactionHeight);
        }
    }

    public void setResponsive(boolean responsive) {
        this.responsive = responsive;
        sendPacket(updatePacketSink, this::getResponsiveData);
    }

    private static final EntityDataAccessor<Boolean> responsiveWatcher = new EntityDataAccessor<>(10, EntityDataSerializers.BOOLEAN);

    private void getResponsiveData(boolean ignoreDefault, EntityDataPacketBuilder dataSink) {
        if (ignoreDefault || !responsive) {
            dataSink.add(responsiveWatcher, responsive);
        }
    }
}