/*
 *   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.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.contents.PlainTextContents;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.TextDisplay;

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 RTextDisplay extends RDisplay {

    private String text;

    private int lineWidth;

    private byte textOpacity;

    private boolean shadowed;

    private boolean seeThrough;

    private Integer backgroundColor;

    private TextDisplay.TextAlignment alignment;

    public RTextDisplay(REntityServer server, Location location) {
        super(server, EntityType.TEXT_DISPLAY, location);
        this.text = "";
        this.lineWidth = 200;
        this.textOpacity = (byte) -1;
        this.shadowed = false;
        this.seeThrough = false;
        this.backgroundColor = null;
        this.alignment = TextDisplay.TextAlignment.CENTER;
        server.addEntity(this);
    }

    @Override
    protected void postSpawn(Consumer<Object> packetSink) {
        super.postSpawn(packetSink);
        sendPacket(packetSink, this::getText, this::getLineWidth, this::getTextOpacity, this::getTextStatus);
    }

    public void setText(String text) {
        this.text = text;
        sendPacket(updatePacketSink, this::getText);
    }

    private static final EntityDataAccessor<Component> textWatcher = new EntityDataAccessor<>(23, EntityDataSerializers.COMPONENT);

    private void getText(boolean ignoreDefault, EntityDataPacketBuilder packetSink) {
        if (ignoreDefault || !text.isEmpty()) {
            packetSink.add(textWatcher, MutableComponent.create(PlainTextContents.create(text)));
        }
    }

    public void setLineWidth(int lineWidth) {
        this.lineWidth = lineWidth;
        sendPacket(updatePacketSink, this::getLineWidth);
    }

    private static final EntityDataAccessor<Integer> lineWidthWatcher = new EntityDataAccessor<>(24, EntityDataSerializers.INT);

    private void getLineWidth(boolean ignoreDefault, EntityDataPacketBuilder packetSink) {
        if (ignoreDefault || lineWidth != 200) {
            packetSink.add(lineWidthWatcher, lineWidth);
        }
    }

    public void setTextOpacity(byte textOpacity) {
        this.textOpacity = textOpacity;
        sendPacket(updatePacketSink, this::getTextOpacity);
    }

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

    private void getTextOpacity(boolean ignoreDefault, EntityDataPacketBuilder packetSink) {
        if (ignoreDefault || textOpacity != (byte) -1) {
            packetSink.add(textOpacityWatcher, textOpacity);
        }
    }

    public void setShadowed(boolean shadowed) {
        this.shadowed = shadowed;
        sendPacket(updatePacketSink, this::getTextStatus);
    }

    public void setSeeThrough(boolean seeThrough) {
        this.seeThrough = seeThrough;
        sendPacket(updatePacketSink, this::getTextStatus);
    }

    public void setBackgroundColor(int color) {
        this.backgroundColor = color;
        sendPacket(updatePacketSink, this::getTextStatus, this::getBackgroundColor);
    }

    private static final EntityDataAccessor<Integer> backgroundColorWatcher = new EntityDataAccessor<>(25, EntityDataSerializers.INT);

    private void getBackgroundColor(boolean ignoreDefault, EntityDataPacketBuilder packetSink) {
        if (ignoreDefault || backgroundColor != null) {
            packetSink.add(backgroundColorWatcher, backgroundColor);
        }
    }

    public void setDefaultBackground(boolean defaultBackground) {
        if (defaultBackground) {
            this.backgroundColor = null;
        }
        sendPacket(updatePacketSink, this::getTextStatus);
    }

    public void setAlignment(TextDisplay.TextAlignment alignment) {
        this.alignment = alignment;
        sendPacket(updatePacketSink, this::getTextStatus);
    }

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

    private void getTextStatus(boolean ignoreDefault, EntityDataPacketBuilder packetSink) {
        byte status = 0;

        if (shadowed) {
            status |= 0x01;
        }
        if (seeThrough) {
            status |= 0x02;
        }
        if (backgroundColor == null) {
            status |= 0x04;
        }
        if (alignment == TextDisplay.TextAlignment.CENTER) {
            // Do nothing
        } else if (alignment == TextDisplay.TextAlignment.LEFT) {
            status |= 0x08;
        } else if (alignment == TextDisplay.TextAlignment.RIGHT) {
            status |= 0x0F;
        }

        if (ignoreDefault || status != 0) {
            packetSink.add(textStatusWatcher, status);
        }
    }
}
