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

import net.minecraft.world.phys.Vec2;
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.Function;
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.nbt.CompoundTag;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.entity.player.Player;
import net.minecraft.network.chat.Component;

public class SignBlockEntity extends BlockEntity {
    public final Component[] messages;
    private boolean isEditable;
    private Player playerWhoMayEdit;
    private final String[] renderMessages;
    private DyeColor color;
    
    public SignBlockEntity() {
        super(BlockEntityType.SIGN);
        this.messages = new Component[] { new TextComponent(""), new TextComponent(""), new TextComponent(""), new TextComponent("") };
        this.isEditable = true;
        this.renderMessages = new String[4];
        this.color = DyeColor.BLACK;
    }
    
    @Override
    public CompoundTag save(final CompoundTag jt) {
        super.save(jt);
        for (int integer3 = 0; integer3 < 4; ++integer3) {
            final String string4 = Component.Serializer.toJson(this.messages[integer3]);
            jt.putString("Text" + (integer3 + 1), string4);
        }
        jt.putString("Color", this.color.getName());
        return jt;
    }
    
    @Override
    public void load(final CompoundTag jt) {
        this.isEditable = false;
        super.load(jt);
        this.color = DyeColor.byName(jt.getString("Color"), DyeColor.BLACK);
        for (int integer3 = 0; integer3 < 4; ++integer3) {
            final String string4 = jt.getString("Text" + (integer3 + 1));
            final Component lf5 = Component.Serializer.fromJson(string4.isEmpty() ? "\"\"" : string4);
            if (this.level instanceof ServerLevel) {
                try {
                    this.messages[integer3] = ComponentUtils.updateForEntity(this.createCommandSourceStack(null), lf5, null, 0);
                }
                catch (CommandSyntaxException commandSyntaxException6) {
                    this.messages[integer3] = lf5;
                }
            }
            else {
                this.messages[integer3] = lf5;
            }
            this.renderMessages[integer3] = null;
        }
    }
    
    public Component getMessage(final int integer) {
        return this.messages[integer];
    }
    
    public void setMessage(final int integer, final Component lf) {
        this.messages[integer] = lf;
        this.renderMessages[integer] = null;
    }
    
    @Nullable
    public String getRenderMessage(final int integer, final Function<Component, String> function) {
        if (this.renderMessages[integer] == null && this.messages[integer] != null) {
            this.renderMessages[integer] = function.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 ayj) {
        this.playerWhoMayEdit = ayj;
    }
    
    public Player getPlayerWhoMayEdit() {
        return this.playerWhoMayEdit;
    }
    
    public boolean executeClickCommands(final Player ayj) {
        for (final Component lf6 : this.messages) {
            final Style ln7 = (lf6 == null) ? null : lf6.getStyle();
            if (ln7 != null) {
                if (ln7.getClickEvent() != null) {
                    final ClickEvent le8 = ln7.getClickEvent();
                    if (le8.getAction() == ClickEvent.Action.RUN_COMMAND) {
                        ayj.getServer().getCommands().performCommand(this.createCommandSourceStack((ServerPlayer)ayj), le8.getValue());
                    }
                }
            }
        }
        return true;
    }
    
    public CommandSourceStack createCommandSourceStack(@Nullable final ServerPlayer xe) {
        final String string3 = (xe == null) ? "Sign" : xe.getName().getString();
        final Component lf4 = (xe == null) ? new TextComponent("Sign") : xe.getDisplayName();
        return new CommandSourceStack(CommandSource.NULL, new Vec3(this.worldPosition.getX() + 0.5, this.worldPosition.getY() + 0.5, this.worldPosition.getZ() + 0.5), Vec2.ZERO, (ServerLevel)this.level, 2, string3, lf4, this.level.getServer(), xe);
    }
    
    public DyeColor getColor() {
        return this.color;
    }
    
    public boolean setColor(final DyeColor bdj) {
        if (bdj != this.getColor()) {
            this.color = bdj;
            this.setChanged();
            this.level.sendBlockUpdated(this.getBlockPos(), this.getBlockState(), this.getBlockState(), 3);
            return true;
        }
        return false;
    }
}
