package net.minecraft.network.chat;

import com.google.gson.JsonSerializationContext;
import net.minecraft.ResourceLocationException;
import com.google.gson.JsonSyntaxException;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonParseException;
import com.google.gson.JsonObject;
import com.google.gson.JsonDeserializationContext;
import java.lang.reflect.Type;
import com.google.gson.JsonElement;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonDeserializer;
import java.util.Objects;
import net.minecraft.ChatFormatting;
import javax.annotation.Nullable;
import net.minecraft.resources.ResourceLocation;

public class Style {
    public static final ResourceLocation DEFAULT_FONT;
    public static final Style EMPTY;
    @Nullable
    private final TextColor color;
    @Nullable
    private final Boolean bold;
    @Nullable
    private final Boolean italic;
    @Nullable
    private final Boolean underlined;
    @Nullable
    private final Boolean strikethrough;
    @Nullable
    private final Boolean obfuscated;
    @Nullable
    private final ClickEvent clickEvent;
    @Nullable
    private final HoverEvent hoverEvent;
    @Nullable
    private final String insertion;
    @Nullable
    private final ResourceLocation font;
    
    private Style(@Nullable final TextColor nc, @Nullable final Boolean boolean2, @Nullable final Boolean boolean3, @Nullable final Boolean boolean4, @Nullable final Boolean boolean5, @Nullable final Boolean boolean6, @Nullable final ClickEvent mp, @Nullable final HoverEvent mv, @Nullable final String string, @Nullable final ResourceLocation uh) {
        this.color = nc;
        this.bold = boolean2;
        this.italic = boolean3;
        this.underlined = boolean4;
        this.strikethrough = boolean5;
        this.obfuscated = boolean6;
        this.clickEvent = mp;
        this.hoverEvent = mv;
        this.insertion = string;
        this.font = uh;
    }
    
    @Nullable
    public TextColor getColor() {
        return this.color;
    }
    
    public boolean isBold() {
        return this.bold == Boolean.TRUE;
    }
    
    public boolean isItalic() {
        return this.italic == Boolean.TRUE;
    }
    
    public boolean isStrikethrough() {
        return this.strikethrough == Boolean.TRUE;
    }
    
    public boolean isUnderlined() {
        return this.underlined == Boolean.TRUE;
    }
    
    public boolean isObfuscated() {
        return this.obfuscated == Boolean.TRUE;
    }
    
    public boolean isEmpty() {
        return this == Style.EMPTY;
    }
    
    @Nullable
    public ClickEvent getClickEvent() {
        return this.clickEvent;
    }
    
    @Nullable
    public HoverEvent getHoverEvent() {
        return this.hoverEvent;
    }
    
    @Nullable
    public String getInsertion() {
        return this.insertion;
    }
    
    public ResourceLocation getFont() {
        return (this.font != null) ? this.font : Style.DEFAULT_FONT;
    }
    
    public Style withColor(@Nullable final TextColor nc) {
        return new Style(nc, this.bold, this.italic, this.underlined, this.strikethrough, this.obfuscated, this.clickEvent, this.hoverEvent, this.insertion, this.font);
    }
    
    public Style withColor(@Nullable final ChatFormatting i) {
        return this.withColor((i != null) ? TextColor.fromLegacyFormat(i) : null);
    }
    
    public Style withBold(@Nullable final Boolean boolean1) {
        return new Style(this.color, boolean1, this.italic, this.underlined, this.strikethrough, this.obfuscated, this.clickEvent, this.hoverEvent, this.insertion, this.font);
    }
    
    public Style withItalic(@Nullable final Boolean boolean1) {
        return new Style(this.color, this.bold, boolean1, this.underlined, this.strikethrough, this.obfuscated, this.clickEvent, this.hoverEvent, this.insertion, this.font);
    }
    
    public Style withClickEvent(@Nullable final ClickEvent mp) {
        return new Style(this.color, this.bold, this.italic, this.underlined, this.strikethrough, this.obfuscated, mp, this.hoverEvent, this.insertion, this.font);
    }
    
    public Style withHoverEvent(@Nullable final HoverEvent mv) {
        return new Style(this.color, this.bold, this.italic, this.underlined, this.strikethrough, this.obfuscated, this.clickEvent, mv, this.insertion, this.font);
    }
    
    public Style withInsertion(@Nullable final String string) {
        return new Style(this.color, this.bold, this.italic, this.underlined, this.strikethrough, this.obfuscated, this.clickEvent, this.hoverEvent, string, this.font);
    }
    
    public Style withFont(@Nullable final ResourceLocation uh) {
        return new Style(this.color, this.bold, this.italic, this.underlined, this.strikethrough, this.obfuscated, this.clickEvent, this.hoverEvent, this.insertion, uh);
    }
    
    public Style applyFormat(final ChatFormatting i) {
        TextColor nc3 = this.color;
        Boolean boolean4 = this.bold;
        Boolean boolean5 = this.italic;
        Boolean boolean6 = this.strikethrough;
        Boolean boolean7 = this.underlined;
        Boolean boolean8 = this.obfuscated;
        switch (i) {
            case OBFUSCATED: {
                boolean8 = true;
                break;
            }
            case BOLD: {
                boolean4 = true;
                break;
            }
            case STRIKETHROUGH: {
                boolean6 = true;
                break;
            }
            case UNDERLINE: {
                boolean7 = true;
                break;
            }
            case ITALIC: {
                boolean5 = true;
                break;
            }
            case RESET: {
                return Style.EMPTY;
            }
            default: {
                nc3 = TextColor.fromLegacyFormat(i);
                break;
            }
        }
        return new Style(nc3, boolean4, boolean5, boolean7, boolean6, boolean8, this.clickEvent, this.hoverEvent, this.insertion, this.font);
    }
    
    public Style applyLegacyFormat(final ChatFormatting i) {
        TextColor nc3 = this.color;
        Boolean boolean4 = this.bold;
        Boolean boolean5 = this.italic;
        Boolean boolean6 = this.strikethrough;
        Boolean boolean7 = this.underlined;
        Boolean boolean8 = this.obfuscated;
        switch (i) {
            case OBFUSCATED: {
                boolean8 = true;
                break;
            }
            case BOLD: {
                boolean4 = true;
                break;
            }
            case STRIKETHROUGH: {
                boolean6 = true;
                break;
            }
            case UNDERLINE: {
                boolean7 = true;
                break;
            }
            case ITALIC: {
                boolean5 = true;
                break;
            }
            case RESET: {
                return Style.EMPTY;
            }
            default: {
                boolean8 = false;
                boolean4 = false;
                boolean6 = false;
                boolean7 = false;
                boolean5 = false;
                nc3 = TextColor.fromLegacyFormat(i);
                break;
            }
        }
        return new Style(nc3, boolean4, boolean5, boolean7, boolean6, boolean8, this.clickEvent, this.hoverEvent, this.insertion, this.font);
    }
    
    public Style applyFormats(final ChatFormatting... arr) {
        TextColor nc3 = this.color;
        Boolean boolean4 = this.bold;
        Boolean boolean5 = this.italic;
        Boolean boolean6 = this.strikethrough;
        Boolean boolean7 = this.underlined;
        Boolean boolean8 = this.obfuscated;
        for (final ChatFormatting i12 : arr) {
            switch (i12) {
                case OBFUSCATED: {
                    boolean8 = true;
                    break;
                }
                case BOLD: {
                    boolean4 = true;
                    break;
                }
                case STRIKETHROUGH: {
                    boolean6 = true;
                    break;
                }
                case UNDERLINE: {
                    boolean7 = true;
                    break;
                }
                case ITALIC: {
                    boolean5 = true;
                    break;
                }
                case RESET: {
                    return Style.EMPTY;
                }
                default: {
                    nc3 = TextColor.fromLegacyFormat(i12);
                    break;
                }
            }
        }
        return new Style(nc3, boolean4, boolean5, boolean7, boolean6, boolean8, this.clickEvent, this.hoverEvent, this.insertion, this.font);
    }
    
    public Style applyTo(final Style nb) {
        if (this == Style.EMPTY) {
            return nb;
        }
        if (nb == Style.EMPTY) {
            return this;
        }
        return new Style((this.color != null) ? this.color : nb.color, (this.bold != null) ? this.bold : nb.bold, (this.italic != null) ? this.italic : nb.italic, (this.underlined != null) ? this.underlined : nb.underlined, (this.strikethrough != null) ? this.strikethrough : nb.strikethrough, (this.obfuscated != null) ? this.obfuscated : nb.obfuscated, (this.clickEvent != null) ? this.clickEvent : nb.clickEvent, (this.hoverEvent != null) ? this.hoverEvent : nb.hoverEvent, (this.insertion != null) ? this.insertion : nb.insertion, (this.font != null) ? this.font : nb.font);
    }
    
    @Override
    public String toString() {
        return "Style{ color=" + this.color + ", bold=" + this.bold + ", italic=" + this.italic + ", underlined=" + this.underlined + ", strikethrough=" + this.strikethrough + ", obfuscated=" + this.obfuscated + ", clickEvent=" + this.getClickEvent() + ", hoverEvent=" + this.getHoverEvent() + ", insertion=" + this.getInsertion() + ", font=" + this.getFont() + '}';
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object instanceof Style) {
            final Style nb3 = (Style)object;
            return this.isBold() == nb3.isBold() && Objects.equals(this.getColor(), nb3.getColor()) && this.isItalic() == nb3.isItalic() && this.isObfuscated() == nb3.isObfuscated() && this.isStrikethrough() == nb3.isStrikethrough() && this.isUnderlined() == nb3.isUnderlined() && Objects.equals(this.getClickEvent(), nb3.getClickEvent()) && Objects.equals(this.getHoverEvent(), nb3.getHoverEvent()) && Objects.equals(this.getInsertion(), nb3.getInsertion()) && Objects.equals(this.getFont(), nb3.getFont());
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.color, this.bold, this.italic, this.underlined, this.strikethrough, this.obfuscated, this.clickEvent, this.hoverEvent, this.insertion);
    }
    
    static {
        DEFAULT_FONT = new ResourceLocation("minecraft", "default");
        EMPTY = new Style(null, null, null, null, null, null, null, null, null, null);
    }
    
    public static class Serializer implements JsonDeserializer<Style>, JsonSerializer<Style> {
        @Nullable
        @Override
        public Style deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            if (!jsonElement.isJsonObject()) {
                return null;
            }
            final JsonObject jsonObject5 = jsonElement.getAsJsonObject();
            if (jsonObject5 == null) {
                return null;
            }
            final Boolean boolean6 = getOptionalFlag(jsonObject5, "bold");
            final Boolean boolean7 = getOptionalFlag(jsonObject5, "italic");
            final Boolean boolean8 = getOptionalFlag(jsonObject5, "underlined");
            final Boolean boolean9 = getOptionalFlag(jsonObject5, "strikethrough");
            final Boolean boolean10 = getOptionalFlag(jsonObject5, "obfuscated");
            final TextColor nc11 = getTextColor(jsonObject5);
            final String string12 = getInsertion(jsonObject5);
            final ClickEvent mp13 = getClickEvent(jsonObject5);
            final HoverEvent mv14 = getHoverEvent(jsonObject5);
            final ResourceLocation uh15 = getFont(jsonObject5);
            return new Style(nc11, boolean6, boolean7, boolean8, boolean9, boolean10, mp13, mv14, string12, uh15, null);
        }
        
        @Nullable
        private static ResourceLocation getFont(final JsonObject jsonObject) {
            if (jsonObject.has("font")) {
                final String string2 = GsonHelper.getAsString(jsonObject, "font");
                try {
                    return new ResourceLocation(string2);
                }
                catch (ResourceLocationException t3) {
                    throw new JsonSyntaxException("Invalid font name: " + string2);
                }
            }
            return null;
        }
        
        @Nullable
        private static HoverEvent getHoverEvent(final JsonObject jsonObject) {
            if (jsonObject.has("hoverEvent")) {
                final JsonObject jsonObject2 = GsonHelper.getAsJsonObject(jsonObject, "hoverEvent");
                final HoverEvent mv3 = HoverEvent.deserialize(jsonObject2);
                if (mv3 != null && mv3.getAction().isAllowedFromServer()) {
                    return mv3;
                }
            }
            return null;
        }
        
        @Nullable
        private static ClickEvent getClickEvent(final JsonObject jsonObject) {
            if (jsonObject.has("clickEvent")) {
                final JsonObject jsonObject2 = GsonHelper.getAsJsonObject(jsonObject, "clickEvent");
                final String string3 = GsonHelper.getAsString(jsonObject2, "action", null);
                final ClickEvent.Action a4 = (string3 == null) ? null : ClickEvent.Action.getByName(string3);
                final String string4 = GsonHelper.getAsString(jsonObject2, "value", null);
                if (a4 != null && string4 != null && a4.isAllowedFromServer()) {
                    return new ClickEvent(a4, string4);
                }
            }
            return null;
        }
        
        @Nullable
        private static String getInsertion(final JsonObject jsonObject) {
            return GsonHelper.getAsString(jsonObject, "insertion", null);
        }
        
        @Nullable
        private static TextColor getTextColor(final JsonObject jsonObject) {
            if (jsonObject.has("color")) {
                final String string2 = GsonHelper.getAsString(jsonObject, "color");
                return TextColor.parseColor(string2);
            }
            return null;
        }
        
        @Nullable
        private static Boolean getOptionalFlag(final JsonObject jsonObject, final String string) {
            if (jsonObject.has(string)) {
                return jsonObject.get(string).getAsBoolean();
            }
            return null;
        }
        
        @Nullable
        @Override
        public JsonElement serialize(final Style nb, final Type type, final JsonSerializationContext jsonSerializationContext) {
            if (nb.isEmpty()) {
                return null;
            }
            final JsonObject jsonObject5 = new JsonObject();
            if (nb.bold != null) {
                jsonObject5.addProperty("bold", nb.bold);
            }
            if (nb.italic != null) {
                jsonObject5.addProperty("italic", nb.italic);
            }
            if (nb.underlined != null) {
                jsonObject5.addProperty("underlined", nb.underlined);
            }
            if (nb.strikethrough != null) {
                jsonObject5.addProperty("strikethrough", nb.strikethrough);
            }
            if (nb.obfuscated != null) {
                jsonObject5.addProperty("obfuscated", nb.obfuscated);
            }
            if (nb.color != null) {
                jsonObject5.addProperty("color", nb.color.serialize());
            }
            if (nb.insertion != null) {
                jsonObject5.add("insertion", jsonSerializationContext.serialize(nb.insertion));
            }
            if (nb.clickEvent != null) {
                final JsonObject jsonObject6 = new JsonObject();
                jsonObject6.addProperty("action", nb.clickEvent.getAction().getName());
                jsonObject6.addProperty("value", nb.clickEvent.getValue());
                jsonObject5.add("clickEvent", jsonObject6);
            }
            if (nb.hoverEvent != null) {
                jsonObject5.add("hoverEvent", nb.hoverEvent.serialize());
            }
            if (nb.font != null) {
                jsonObject5.addProperty("font", nb.font.toString());
            }
            return jsonObject5;
        }
    }
}
