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 com.google.gson.JsonSyntaxException;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
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 ben, final Component lf2, final Component lf3, @Nullable final ResourceLocation sm, final FrameType ae, final boolean boolean6, final boolean boolean7, final boolean boolean8) {
        this.title = lf2;
        this.description = lf3;
        this.icon = ben;
        this.background = sm;
        this.frame = ae;
        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 JsonDeserializationContext jsonDeserializationContext) {
        final Component lf3 = GsonHelper.<Component>getAsObject(jsonObject, "title", jsonDeserializationContext, Component.class);
        final Component lf4 = GsonHelper.<Component>getAsObject(jsonObject, "description", jsonDeserializationContext, Component.class);
        if (lf3 == null || lf4 == null) {
            throw new JsonSyntaxException("Both title and description must be set");
        }
        final ItemStack ben5 = getIcon(GsonHelper.getAsJsonObject(jsonObject, "icon"));
        final ResourceLocation sm6 = jsonObject.has("background") ? new ResourceLocation(GsonHelper.getAsString(jsonObject, "background")) : null;
        final FrameType ae7 = jsonObject.has("frame") ? FrameType.byName(GsonHelper.getAsString(jsonObject, "frame")) : FrameType.TASK;
        final boolean boolean8 = GsonHelper.getAsBoolean(jsonObject, "show_toast", true);
        final boolean boolean9 = GsonHelper.getAsBoolean(jsonObject, "announce_to_chat", true);
        final boolean boolean10 = GsonHelper.getAsBoolean(jsonObject, "hidden", false);
        return new DisplayInfo(ben5, lf3, lf4, sm6, ae7, boolean8, boolean9, boolean10);
    }
    
    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 bei2 = GsonHelper.getAsItem(jsonObject, "item");
        if (jsonObject.has("data")) {
            throw new JsonParseException("Disallowed data tag found");
        }
        final ItemStack ben3 = new ItemStack(bei2);
        if (jsonObject.has("nbt")) {
            try {
                final CompoundTag jt4 = TagParser.parseTag(GsonHelper.convertToString(jsonObject.get("nbt"), "nbt"));
                ben3.setTag(jt4);
            }
            catch (CommandSyntaxException commandSyntaxException4) {
                throw new JsonSyntaxException("Invalid nbt tag: " + commandSyntaxException4.getMessage());
            }
        }
        return ben3;
    }
    
    public void serializeToNetwork(final FriendlyByteBuf kv) {
        kv.writeComponent(this.title);
        kv.writeComponent(this.description);
        kv.writeItem(this.icon);
        kv.writeEnum(this.frame);
        int integer3 = 0;
        if (this.background != null) {
            integer3 |= 0x1;
        }
        if (this.showToast) {
            integer3 |= 0x2;
        }
        if (this.hidden) {
            integer3 |= 0x4;
        }
        kv.writeInt(integer3);
        if (this.background != null) {
            kv.writeResourceLocation(this.background);
        }
        kv.writeFloat(this.x);
        kv.writeFloat(this.y);
    }
    
    public static DisplayInfo fromNetwork(final FriendlyByteBuf kv) {
        final Component lf2 = kv.readComponent();
        final Component lf3 = kv.readComponent();
        final ItemStack ben4 = kv.readItem();
        final FrameType ae5 = kv.<FrameType>readEnum(FrameType.class);
        final int integer6 = kv.readInt();
        final ResourceLocation sm7 = ((integer6 & 0x1) != 0x0) ? kv.readResourceLocation() : null;
        final boolean boolean8 = (integer6 & 0x2) != 0x0;
        final boolean boolean9 = (integer6 & 0x4) != 0x0;
        final DisplayInfo ad10 = new DisplayInfo(ben4, lf2, lf3, sm7, ae5, boolean8, false, boolean9);
        ad10.setLocation(kv.readFloat(), kv.readFloat());
        return ad10;
    }
    
    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;
    }
}
