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

import java.util.function.Supplier;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.BannerBlock;
import net.minecraft.world.level.block.state.BlockState;
import com.google.common.collect.Lists;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.level.block.AbstractBannerBlock;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import com.mojang.datafixers.util.Pair;
import java.util.List;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.item.DyeColor;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
import net.minecraft.world.Nameable;

public class BannerBlockEntity extends BlockEntity implements Nameable {
    @Nullable
    private Component name;
    @Nullable
    private DyeColor baseColor;
    @Nullable
    private ListTag itemPatterns;
    private boolean receivedData;
    @Nullable
    private List<Pair<BannerPattern, DyeColor>> patterns;
    
    public BannerBlockEntity() {
        super(BlockEntityType.BANNER);
        this.baseColor = DyeColor.WHITE;
    }
    
    public BannerBlockEntity(final DyeColor bdj) {
        this();
        this.baseColor = bdj;
    }
    
    @Nullable
    public static ListTag getItemPatterns(final ItemStack ben) {
        ListTag jz2 = null;
        final CompoundTag jt3 = ben.getTagElement("BlockEntityTag");
        if (jt3 != null && jt3.contains("Patterns", 9)) {
            jz2 = jt3.getList("Patterns", 10).copy();
        }
        return jz2;
    }
    
    public void fromItem(final ItemStack ben, final DyeColor bdj) {
        this.itemPatterns = getItemPatterns(ben);
        this.baseColor = bdj;
        this.patterns = null;
        this.receivedData = true;
        this.name = (ben.hasCustomHoverName() ? ben.getHoverName() : null);
    }
    
    @Override
    public Component getName() {
        if (this.name != null) {
            return this.name;
        }
        return new TranslatableComponent("block.minecraft.banner", new Object[0]);
    }
    
    @Nullable
    @Override
    public Component getCustomName() {
        return this.name;
    }
    
    public void setCustomName(final Component lf) {
        this.name = lf;
    }
    
    @Override
    public CompoundTag save(final CompoundTag jt) {
        super.save(jt);
        if (this.itemPatterns != null) {
            jt.put("Patterns", this.itemPatterns);
        }
        if (this.name != null) {
            jt.putString("CustomName", Component.Serializer.toJson(this.name));
        }
        return jt;
    }
    
    @Override
    public void load(final CompoundTag jt) {
        super.load(jt);
        if (jt.contains("CustomName", 8)) {
            this.name = Component.Serializer.fromJson(jt.getString("CustomName"));
        }
        if (this.hasLevel()) {
            this.baseColor = ((AbstractBannerBlock)this.getBlockState().getBlock()).getColor();
        }
        else {
            this.baseColor = null;
        }
        this.itemPatterns = jt.getList("Patterns", 10);
        this.patterns = null;
        this.receivedData = true;
    }
    
    @Nullable
    @Override
    public ClientboundBlockEntityDataPacket getUpdatePacket() {
        return new ClientboundBlockEntityDataPacket(this.worldPosition, 6, this.getUpdateTag());
    }
    
    @Override
    public CompoundTag getUpdateTag() {
        return this.save(new CompoundTag());
    }
    
    public static int getPatternCount(final ItemStack ben) {
        final CompoundTag jt2 = ben.getTagElement("BlockEntityTag");
        if (jt2 != null && jt2.contains("Patterns")) {
            return jt2.getList("Patterns", 10).size();
        }
        return 0;
    }
    
    public List<Pair<BannerPattern, DyeColor>> getPatterns() {
        if (this.patterns == null && this.receivedData) {
            this.patterns = createPatterns(this.getBaseColor(this::getBlockState), this.itemPatterns);
        }
        return this.patterns;
    }
    
    public static List<Pair<BannerPattern, DyeColor>> createPatterns(final DyeColor bdj, @Nullable final ListTag jz) {
        final List<Pair<BannerPattern, DyeColor>> list3 = Lists.newArrayList();
        list3.add((Pair<BannerPattern, DyeColor>)Pair.of(BannerPattern.BASE, bdj));
        if (jz != null) {
            for (int integer4 = 0; integer4 < jz.size(); ++integer4) {
                final CompoundTag jt5 = jz.getCompound(integer4);
                final BannerPattern bwd6 = BannerPattern.byHash(jt5.getString("Pattern"));
                if (bwd6 != null) {
                    final int integer5 = jt5.getInt("Color");
                    list3.add((Pair<BannerPattern, DyeColor>)Pair.of(bwd6, DyeColor.byId(integer5)));
                }
            }
        }
        return list3;
    }
    
    public static void removeLastPattern(final ItemStack ben) {
        final CompoundTag jt2 = ben.getTagElement("BlockEntityTag");
        if (jt2 == null || !jt2.contains("Patterns", 9)) {
            return;
        }
        final ListTag jz3 = jt2.getList("Patterns", 10);
        if (jz3.isEmpty()) {
            return;
        }
        jz3.remove(jz3.size() - 1);
        if (jz3.isEmpty()) {
            ben.removeTagKey("BlockEntityTag");
        }
    }
    
    public ItemStack getItem(final BlockState byj) {
        final ItemStack ben3 = new ItemStack(BannerBlock.byColor(this.getBaseColor(() -> byj)));
        if (this.itemPatterns != null && !this.itemPatterns.isEmpty()) {
            ben3.getOrCreateTagElement("BlockEntityTag").put("Patterns", this.itemPatterns.copy());
        }
        if (this.name != null) {
            ben3.setHoverName(this.name);
        }
        return ben3;
    }
    
    public DyeColor getBaseColor(final Supplier<BlockState> supplier) {
        if (this.baseColor == null) {
            this.baseColor = ((AbstractBannerBlock)supplier.get().getBlock()).getColor();
        }
        return this.baseColor;
    }
}
