package net.minecraft.world.level.block.entity;

import net.minecraft.world.phys.Vec2;
import net.minecraft.core.Vec3i;
import net.minecraft.world.phys.Vec3;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import javax.annotation.Nullable;
import java.util.function.UnaryOperator;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.world.entity.Entity;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.item.DyeColor;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.world.entity.player.Player;
import net.minecraft.network.chat.Component;

public class SignBlockEntity extends BlockEntity {
    private final Component[] messages;
    private boolean isEditable;
    private Player playerWhoMayEdit;
    private final FormattedText[] renderMessages;
    private DyeColor color;
    
    public SignBlockEntity() {
        super(BlockEntityType.SIGN);
        this.messages = new Component[] { TextComponent.EMPTY, TextComponent.EMPTY, TextComponent.EMPTY, TextComponent.EMPTY };
        this.isEditable = true;
        this.renderMessages = new FormattedText[4];
        this.color = DyeColor.BLACK;
    }
    
    @Override
    public CompoundTag save(final CompoundTag le) {
        super.save(le);
        for (int integer3 = 0; integer3 < 4; ++integer3) {
            final String string4 = Component.Serializer.toJson(this.messages[integer3]);
            le.putString("Text" + (integer3 + 1), string4);
        }
        le.putString("Color", this.color.getName());
        return le;
    }
    
    @Override
    public void load(final BlockState cfj, final CompoundTag le) {
        this.isEditable = false;
        super.load(cfj, le);
        this.color = DyeColor.byName(le.getString("Color"), DyeColor.BLACK);
        for (int integer4 = 0; integer4 < 4; ++integer4) {
            final String string5 = le.getString("Text" + (integer4 + 1));
            final Component mr6 = Component.Serializer.fromJson(string5.isEmpty() ? "\"\"" : string5);
            if (this.level instanceof ServerLevel) {
                try {
                    this.messages[integer4] = ComponentUtils.updateForEntity(this.createCommandSourceStack(null), mr6, null, 0);
                }
                catch (CommandSyntaxException commandSyntaxException7) {
                    this.messages[integer4] = mr6;
                }
            }
            else {
                this.messages[integer4] = mr6;
            }
            this.renderMessages[integer4] = null;
        }
    }
    
    public void setMessage(final int integer, final Component mr) {
        this.messages[integer] = mr;
        this.renderMessages[integer] = null;
    }
    
    @Nullable
    public FormattedText getRenderMessage(final int integer, final UnaryOperator<FormattedText> unaryOperator) {
        if (this.renderMessages[integer] == null && this.messages[integer] != null) {
            this.renderMessages[integer] = unaryOperator.apply(this.messages[integer]);
        }
        return this.renderMessages[integer];
    }
    
    @Nullable
    @Override
    public ClientboundBlockEntityDataPacket getUpdatePacket() {
        return new ClientboundBlockEntityDataPacket(this.worldPosition, 9, this.getUpdateTag());
    }
    
    @Override
    public CompoundTag getUpdateTag() {
        return this.save(new CompoundTag());
    }
    
    @Override
    public boolean onlyOpCanSetNbt() {
        return true;
    }
    
    public boolean isEditable() {
        return this.isEditable;
    }
    
    public void setEditable(final boolean boolean1) {
        if (!(this.isEditable = boolean1)) {
            this.playerWhoMayEdit = null;
        }
    }
    
    public void setAllowedPlayerEditor(final Player bec) {
        this.playerWhoMayEdit = bec;
    }
    
    public Player getPlayerWhoMayEdit() {
        return this.playerWhoMayEdit;
    }
    
    public boolean executeClickCommands(final Player bec) {
        for (final Component mr6 : this.messages) {
            final Style nb7 = (mr6 == null) ? null : mr6.getStyle();
            if (nb7 != null) {
                if (nb7.getClickEvent() != null) {
                    final ClickEvent mp8 = nb7.getClickEvent();
                    if (mp8.getAction() == ClickEvent.Action.RUN_COMMAND) {
                        bec.getServer().getCommands().performCommand(this.createCommandSourceStack((ServerPlayer)bec), mp8.getValue());
                    }
                }
            }
        }
        return true;
    }
    
    public CommandSourceStack createCommandSourceStack(@Nullable final ServerPlayer ze) {
        final String string3 = (ze == null) ? "Sign" : ze.getName().getString();
        final Component mr4 = (ze == null) ? new TextComponent("Sign") : ze.getDisplayName();
        return new CommandSourceStack(CommandSource.NULL, Vec3.atCenterOf(this.worldPosition), Vec2.ZERO, (ServerLevel)this.level, 2, string3, mr4, this.level.getServer(), ze);
    }
    
    public DyeColor getColor() {
        return this.color;
    }
    
    public boolean setColor(final DyeColor bje) {
        if (bje != this.getColor()) {
            this.color = bje;
            this.setChanged();
            this.level.sendBlockUpdated(this.getBlockPos(), this.getBlockState(), this.getBlockState(), 3);
            return true;
        }
        return false;
    }
}
