package net.minecraft.world.level.saveddata.maps;

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.nbt.Tag;
import java.util.Objects;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BannerBlockEntity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.CompoundTag;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.DyeColor;
import net.minecraft.core.BlockPos;

public class MapBanner {
    private final BlockPos pos;
    private final DyeColor color;
    @Nullable
    private final Component name;
    
    public MapBanner(final BlockPos fk, final DyeColor bdj, @Nullable final Component lf) {
        this.pos = fk;
        this.color = bdj;
        this.name = lf;
    }
    
    public static MapBanner load(final CompoundTag jt) {
        final BlockPos fk2 = NbtUtils.readBlockPos(jt.getCompound("Pos"));
        final DyeColor bdj3 = DyeColor.byName(jt.getString("Color"), DyeColor.WHITE);
        final Component lf4 = jt.contains("Name") ? Component.Serializer.fromJson(jt.getString("Name")) : null;
        return new MapBanner(fk2, bdj3, lf4);
    }
    
    @Nullable
    public static MapBanner fromWorld(final BlockGetter bjg, final BlockPos fk) {
        final BlockEntity bwl3 = bjg.getBlockEntity(fk);
        if (bwl3 instanceof BannerBlockEntity) {
            final BannerBlockEntity bwc4 = (BannerBlockEntity)bwl3;
            final DyeColor bdj5 = bwc4.getBaseColor(() -> bjg.getBlockState(fk));
            final Component lf6 = bwc4.hasCustomName() ? bwc4.getCustomName() : null;
            return new MapBanner(fk, bdj5, lf6);
        }
        return null;
    }
    
    public BlockPos getPos() {
        return this.pos;
    }
    
    public MapDecoration.Type getDecoration() {
        switch (this.color) {
            case WHITE: {
                return MapDecoration.Type.BANNER_WHITE;
            }
            case ORANGE: {
                return MapDecoration.Type.BANNER_ORANGE;
            }
            case MAGENTA: {
                return MapDecoration.Type.BANNER_MAGENTA;
            }
            case LIGHT_BLUE: {
                return MapDecoration.Type.BANNER_LIGHT_BLUE;
            }
            case YELLOW: {
                return MapDecoration.Type.BANNER_YELLOW;
            }
            case LIME: {
                return MapDecoration.Type.BANNER_LIME;
            }
            case PINK: {
                return MapDecoration.Type.BANNER_PINK;
            }
            case GRAY: {
                return MapDecoration.Type.BANNER_GRAY;
            }
            case LIGHT_GRAY: {
                return MapDecoration.Type.BANNER_LIGHT_GRAY;
            }
            case CYAN: {
                return MapDecoration.Type.BANNER_CYAN;
            }
            case PURPLE: {
                return MapDecoration.Type.BANNER_PURPLE;
            }
            case BLUE: {
                return MapDecoration.Type.BANNER_BLUE;
            }
            case BROWN: {
                return MapDecoration.Type.BANNER_BROWN;
            }
            case GREEN: {
                return MapDecoration.Type.BANNER_GREEN;
            }
            case RED: {
                return MapDecoration.Type.BANNER_RED;
            }
            default: {
                return MapDecoration.Type.BANNER_BLACK;
            }
        }
    }
    
    @Nullable
    public Component getName() {
        return this.name;
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object == null || this.getClass() != object.getClass()) {
            return false;
        }
        final MapBanner crc3 = (MapBanner)object;
        return Objects.equals(this.pos, crc3.pos) && this.color == crc3.color && Objects.equals(this.name, crc3.name);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.pos, this.color, this.name);
    }
    
    public CompoundTag save() {
        final CompoundTag jt2 = new CompoundTag();
        jt2.put("Pos", NbtUtils.writeBlockPos(this.pos));
        jt2.putString("Color", this.color.getName());
        if (this.name != null) {
            jt2.putString("Name", Component.Serializer.toJson(this.name));
        }
        return jt2;
    }
    
    public String getId() {
        return "banner-" + this.pos.getX() + "," + this.pos.getY() + "," + this.pos.getZ();
    }
}
