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 fu, final DyeColor bje, @Nullable final Component mr) {
        this.pos = fu;
        this.color = bje;
        this.name = mr;
    }
    
    public static MapBanner load(final CompoundTag le) {
        final BlockPos fu2 = NbtUtils.readBlockPos(le.getCompound("Pos"));
        final DyeColor bje3 = DyeColor.byName(le.getString("Color"), DyeColor.WHITE);
        final Component mr4 = le.contains("Name") ? Component.Serializer.fromJson(le.getString("Name")) : null;
        return new MapBanner(fu2, bje3, mr4);
    }
    
    @Nullable
    public static MapBanner fromWorld(final BlockGetter bpg, final BlockPos fu) {
        final BlockEntity cdl3 = bpg.getBlockEntity(fu);
        if (cdl3 instanceof BannerBlockEntity) {
            final BannerBlockEntity cdc4 = (BannerBlockEntity)cdl3;
            final DyeColor bje5 = cdc4.getBaseColor(() -> bpg.getBlockState(fu));
            final Component mr6 = cdc4.hasCustomName() ? cdc4.getCustomName() : null;
            return new MapBanner(fu, bje5, mr6);
        }
        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 czr3 = (MapBanner)object;
        return Objects.equals(this.pos, czr3.pos) && this.color == czr3.color && Objects.equals(this.name, czr3.name);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.pos, this.color, this.name);
    }
    
    public CompoundTag save() {
        final CompoundTag le2 = new CompoundTag();
        le2.put("Pos", NbtUtils.writeBlockPos(this.pos));
        le2.putString("Color", this.color.getName());
        if (this.name != null) {
            le2.putString("Name", Component.Serializer.toJson(this.name));
        }
        return le2;
    }
    
    public String getId() {
        return "banner-" + this.pos.getX() + "," + this.pos.getY() + "," + this.pos.getZ();
    }
}
