/*
 *   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.Reflection;
import de.steamwar.core.BountifulWrapper;
import de.steamwar.core.ChatWrapper;
import de.steamwar.core.Core;
import lombok.Getter;
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 Class<?> iChatBaseComponent = Reflection.getClass("net.minecraft.network.chat.Component");
    private static final Object textWatcher = BountifulWrapper.impl.getDataWatcherObject(Core.getVersion() >= 21 ? 23 : 22, iChatBaseComponent);
    private void getText(boolean ignoreDefault, BiConsumer<Object, Object> packetSink) {
        if (ignoreDefault || !text.isEmpty()) {
            packetSink.accept(textWatcher, ChatWrapper.impl.stringToChatComponent(text));
        }
    }

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

    private static final Object lineWidthWatcher = BountifulWrapper.impl.getDataWatcherObject(Core.getVersion() >= 21 ? 24 : 23, Integer.class);
    private void getLineWidth(boolean ignoreDefault, BiConsumer<Object, Object> packetSink) {
        if (ignoreDefault || lineWidth != 200) {
            packetSink.accept(lineWidthWatcher, lineWidth);
        }
    }

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

    private static final Object textOpacityWatcher = BountifulWrapper.impl.getDataWatcherObject(Core.getVersion() >= 21 ? 26 : 25, Byte.class);
    private void getTextOpacity(boolean ignoreDefault, BiConsumer<Object, Object> packetSink) {
        if (ignoreDefault || textOpacity != (byte) -1) {
            packetSink.accept(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 Object backgroundColorWatcher = BountifulWrapper.impl.getDataWatcherObject(Core.getVersion() >= 21 ? 25 : 24, Integer.class);
    private void getBackgroundColor(boolean ignoreDefault, BiConsumer<Object, Object> packetSink) {
        if (ignoreDefault || backgroundColor != null) {
            packetSink.accept(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 Object textStatusWatcher = BountifulWrapper.impl.getDataWatcherObject(Core.getVersion() >= 21 ? 27 : 26, Byte.class);
    private void getTextStatus(boolean ignoreDefault, BiConsumer<Object, Object> 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.accept(textStatusWatcher, status);
        }
    }
}
