package net.minecraft.advancements;

import net.minecraft.core.Registry;
import com.google.gson.JsonElement;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.nbt.TagParser;
import net.minecraft.world.level.ItemLike;
import com.google.gson.JsonParseException;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonObject;
import javax.annotation.Nullable;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.network.chat.Component;

public class DisplayInfo {
    private final Component title;
    private final Component description;
    private final ItemStack icon;
    private final ResourceLocation background;
    private final FrameType frame;
    private final boolean showToast;
    private final boolean announceChat;
    private final boolean hidden;
    private float x;
    private float y;
    
    public DisplayInfo(final ItemStack bki, final Component mr2, final Component mr3, @Nullable final ResourceLocation uh, final FrameType ag, final boolean boolean6, final boolean boolean7, final boolean boolean8) {
        this.title = mr2;
        this.description = mr3;
        this.icon = bki;
        this.background = uh;
        this.frame = ag;
        this.showToast = boolean6;
        this.announceChat = boolean7;
        this.hidden = boolean8;
    }
    
    public void setLocation(final float float1, final float float2) {
        this.x = float1;
        this.y = float2;
    }
    
    public Component getTitle() {
        return this.title;
    }
    
    public Component getDescription() {
        return this.description;
    }
    
    public ItemStack getIcon() {
        return this.icon;
    }
    
    @Nullable
    public ResourceLocation getBackground() {
        return this.background;
    }
    
    public FrameType getFrame() {
        return this.frame;
    }
    
    public float getX() {
        return this.x;
    }
    
    public float getY() {
        return this.y;
    }
    
    public boolean shouldShowToast() {
        return this.showToast;
    }
    
    public boolean shouldAnnounceChat() {
        return this.announceChat;
    }
    
    public boolean isHidden() {
        return this.hidden;
    }
    
    public static DisplayInfo fromJson(final JsonObject jsonObject) {
        final Component mr2 = Component.Serializer.fromJson(jsonObject.get("title"));
        final Component mr3 = Component.Serializer.fromJson(jsonObject.get("description"));
        if (mr2 == null || mr3 == null) {
            throw new JsonSyntaxException("Both title and description must be set");
        }
        final ItemStack bki4 = getIcon(GsonHelper.getAsJsonObject(jsonObject, "icon"));
        final ResourceLocation uh5 = jsonObject.has("background") ? new ResourceLocation(GsonHelper.getAsString(jsonObject, "background")) : null;
        final FrameType ag6 = jsonObject.has("frame") ? FrameType.byName(GsonHelper.getAsString(jsonObject, "frame")) : FrameType.TASK;
        final boolean boolean7 = GsonHelper.getAsBoolean(jsonObject, "show_toast", true);
        final boolean boolean8 = GsonHelper.getAsBoolean(jsonObject, "announce_to_chat", true);
        final boolean boolean9 = GsonHelper.getAsBoolean(jsonObject, "hidden", false);
        return new DisplayInfo(bki4, mr2, mr3, uh5, ag6, boolean7, boolean8, boolean9);
    }
    
    private static ItemStack getIcon(final JsonObject jsonObject) {
        if (!jsonObject.has("item")) {
            throw new JsonSyntaxException("Unsupported icon type, currently only items are supported (add 'item' key)");
        }
        final Item bke2 = GsonHelper.getAsItem(jsonObject, "item");
        if (jsonObject.has("data")) {
            throw new JsonParseException("Disallowed data tag found");
        }
        final ItemStack bki3 = new ItemStack(bke2);
        if (jsonObject.has("nbt")) {
            try {
                final CompoundTag le4 = TagParser.parseTag(GsonHelper.convertToString(jsonObject.get("nbt"), "nbt"));
                bki3.setTag(le4);
            }
            catch (CommandSyntaxException commandSyntaxException4) {
                throw new JsonSyntaxException("Invalid nbt tag: " + commandSyntaxException4.getMessage());
            }
        }
        return bki3;
    }
    
    public void serializeToNetwork(final FriendlyByteBuf mg) {
        mg.writeComponent(this.title);
        mg.writeComponent(this.description);
        mg.writeItem(this.icon);
        mg.writeEnum(this.frame);
        int integer3 = 0;
        if (this.background != null) {
            integer3 |= 0x1;
        }
        if (this.showToast) {
            integer3 |= 0x2;
        }
        if (this.hidden) {
            integer3 |= 0x4;
        }
        mg.writeInt(integer3);
        if (this.background != null) {
            mg.writeResourceLocation(this.background);
        }
        mg.writeFloat(this.x);
        mg.writeFloat(this.y);
    }
    
    public static DisplayInfo fromNetwork(final FriendlyByteBuf mg) {
        final Component mr2 = mg.readComponent();
        final Component mr3 = mg.readComponent();
        final ItemStack bki4 = mg.readItem();
        final FrameType ag5 = mg.<FrameType>readEnum(FrameType.class);
        final int integer6 = mg.readInt();
        final ResourceLocation uh7 = ((integer6 & 0x1) != 0x0) ? mg.readResourceLocation() : null;
        final boolean boolean8 = (integer6 & 0x2) != 0x0;
        final boolean boolean9 = (integer6 & 0x4) != 0x0;
        final DisplayInfo af10 = new DisplayInfo(bki4, mr2, mr3, uh7, ag5, boolean8, false, boolean9);
        af10.setLocation(mg.readFloat(), mg.readFloat());
        return af10;
    }
    
    public JsonElement serializeToJson() {
        final JsonObject jsonObject2 = new JsonObject();
        jsonObject2.add("icon", this.serializeIcon());
        jsonObject2.add("title", Component.Serializer.toJsonTree(this.title));
        jsonObject2.add("description", Component.Serializer.toJsonTree(this.description));
        jsonObject2.addProperty("frame", this.frame.getName());
        jsonObject2.addProperty("show_toast", this.showToast);
        jsonObject2.addProperty("announce_to_chat", this.announceChat);
        jsonObject2.addProperty("hidden", this.hidden);
        if (this.background != null) {
            jsonObject2.addProperty("background", this.background.toString());
        }
        return jsonObject2;
    }
    
    private JsonObject serializeIcon() {
        final JsonObject jsonObject2 = new JsonObject();
        jsonObject2.addProperty("item", Registry.ITEM.getKey(this.icon.getItem()).toString());
        if (this.icon.hasTag()) {
            jsonObject2.addProperty("nbt", this.icon.getTag().toString());
        }
        return jsonObject2;
    }
}
