package net.minecraft.network.chat;

import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonParseException;
import com.google.gson.JsonObject;
import net.minecraft.util.GsonHelper;
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 javax.annotation.Nullable;
import net.minecraft.ChatFormatting;

public class Style {
    private Style parent;
    private ChatFormatting color;
    private Boolean bold;
    private Boolean italic;
    private Boolean underlined;
    private Boolean strikethrough;
    private Boolean obfuscated;
    private ClickEvent clickEvent;
    private HoverEvent hoverEvent;
    private String insertion;
    private static final Style ROOT;
    
    @Nullable
    public ChatFormatting getColor() {
        return (this.color == null) ? this.getParent().getColor() : this.color;
    }
    
    public boolean isBold() {
        return (this.bold == null) ? this.getParent().isBold() : this.bold;
    }
    
    public boolean isItalic() {
        return (this.italic == null) ? this.getParent().isItalic() : this.italic;
    }
    
    public boolean isStrikethrough() {
        return (this.strikethrough == null) ? this.getParent().isStrikethrough() : this.strikethrough;
    }
    
    public boolean isUnderlined() {
        return (this.underlined == null) ? this.getParent().isUnderlined() : this.underlined;
    }
    
    public boolean isObfuscated() {
        return (this.obfuscated == null) ? this.getParent().isObfuscated() : this.obfuscated;
    }
    
    public boolean isEmpty() {
        return this.bold == null && this.italic == null && this.strikethrough == null && this.underlined == null && this.obfuscated == null && this.color == null && this.clickEvent == null && this.hoverEvent == null && this.insertion == null;
    }
    
    @Nullable
    public ClickEvent getClickEvent() {
        return (this.clickEvent == null) ? this.getParent().getClickEvent() : this.clickEvent;
    }
    
    @Nullable
    public HoverEvent getHoverEvent() {
        return (this.hoverEvent == null) ? this.getParent().getHoverEvent() : this.hoverEvent;
    }
    
    @Nullable
    public String getInsertion() {
        return (this.insertion == null) ? this.getParent().getInsertion() : this.insertion;
    }
    
    public Style setColor(final ChatFormatting g) {
        this.color = g;
        return this;
    }
    
    public Style setBold(final Boolean boolean1) {
        this.bold = boolean1;
        return this;
    }
    
    public Style setItalic(final Boolean boolean1) {
        this.italic = boolean1;
        return this;
    }
    
    public Style setStrikethrough(final Boolean boolean1) {
        this.strikethrough = boolean1;
        return this;
    }
    
    public Style setUnderlined(final Boolean boolean1) {
        this.underlined = boolean1;
        return this;
    }
    
    public Style setObfuscated(final Boolean boolean1) {
        this.obfuscated = boolean1;
        return this;
    }
    
    public Style setClickEvent(final ClickEvent le) {
        this.clickEvent = le;
        return this;
    }
    
    public Style setHoverEvent(final HoverEvent li) {
        this.hoverEvent = li;
        return this;
    }
    
    public Style setInsertion(final String string) {
        this.insertion = string;
        return this;
    }
    
    public Style inheritFrom(final Style ln) {
        this.parent = ln;
        return this;
    }
    
    public String getLegacyFormatCodes() {
        if (!this.isEmpty()) {
            final StringBuilder stringBuilder2 = new StringBuilder();
            if (this.getColor() != null) {
                stringBuilder2.append(this.getColor());
            }
            if (this.isBold()) {
                stringBuilder2.append(ChatFormatting.BOLD);
            }
            if (this.isItalic()) {
                stringBuilder2.append(ChatFormatting.ITALIC);
            }
            if (this.isUnderlined()) {
                stringBuilder2.append(ChatFormatting.UNDERLINE);
            }
            if (this.isObfuscated()) {
                stringBuilder2.append(ChatFormatting.OBFUSCATED);
            }
            if (this.isStrikethrough()) {
                stringBuilder2.append(ChatFormatting.STRIKETHROUGH);
            }
            return stringBuilder2.toString();
        }
        if (this.parent != null) {
            return this.parent.getLegacyFormatCodes();
        }
        return "";
    }
    
    private Style getParent() {
        return (this.parent == null) ? Style.ROOT : this.parent;
    }
    
    @Override
    public String toString() {
        return "Style{hasParent=" + (this.parent != null) + ", color=" + this.color + ", bold=" + this.bold + ", italic=" + this.italic + ", underlined=" + this.underlined + ", obfuscated=" + this.obfuscated + ", clickEvent=" + this.getClickEvent() + ", hoverEvent=" + this.getHoverEvent() + ", insertion=" + this.getInsertion() + '}';
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object instanceof Style) {
            final Style ln3 = (Style)object;
            if (this.isBold() == ln3.isBold() && this.getColor() == ln3.getColor() && this.isItalic() == ln3.isItalic() && this.isObfuscated() == ln3.isObfuscated() && this.isStrikethrough() == ln3.isStrikethrough() && this.isUnderlined() == ln3.isUnderlined()) {
                if (this.getClickEvent() != null) {
                    if (!this.getClickEvent().equals(ln3.getClickEvent())) {
                        return false;
                    }
                }
                else if (ln3.getClickEvent() != null) {
                    return false;
                }
                if (this.getHoverEvent() != null) {
                    if (!this.getHoverEvent().equals(ln3.getHoverEvent())) {
                        return false;
                    }
                }
                else if (ln3.getHoverEvent() != null) {
                    return false;
                }
                if ((this.getInsertion() == null) ? (ln3.getInsertion() == null) : this.getInsertion().equals(ln3.getInsertion())) {
                    return true;
                }
            }
            return false;
        }
        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);
    }
    
    public Style copy() {
        final Style ln2 = new Style();
        ln2.bold = this.bold;
        ln2.italic = this.italic;
        ln2.strikethrough = this.strikethrough;
        ln2.underlined = this.underlined;
        ln2.obfuscated = this.obfuscated;
        ln2.color = this.color;
        ln2.clickEvent = this.clickEvent;
        ln2.hoverEvent = this.hoverEvent;
        ln2.parent = this.parent;
        ln2.insertion = this.insertion;
        return ln2;
    }
    
    public Style flatCopy() {
        final Style ln2 = new Style();
        ln2.setBold(this.isBold());
        ln2.setItalic(this.isItalic());
        ln2.setStrikethrough(this.isStrikethrough());
        ln2.setUnderlined(this.isUnderlined());
        ln2.setObfuscated(this.isObfuscated());
        ln2.setColor(this.getColor());
        ln2.setClickEvent(this.getClickEvent());
        ln2.setHoverEvent(this.getHoverEvent());
        ln2.setInsertion(this.getInsertion());
        return ln2;
    }
    
    static {
        ROOT = new Style() {
            @Nullable
            @Override
            public ChatFormatting getColor() {
                return null;
            }
            
            @Override
            public boolean isBold() {
                return false;
            }
            
            @Override
            public boolean isItalic() {
                return false;
            }
            
            @Override
            public boolean isStrikethrough() {
                return false;
            }
            
            @Override
            public boolean isUnderlined() {
                return false;
            }
            
            @Override
            public boolean isObfuscated() {
                return false;
            }
            
            @Nullable
            @Override
            public ClickEvent getClickEvent() {
                return null;
            }
            
            @Nullable
            @Override
            public HoverEvent getHoverEvent() {
                return null;
            }
            
            @Nullable
            @Override
            public String getInsertion() {
                return null;
            }
            
            @Override
            public Style setColor(final ChatFormatting g) {
                throw new UnsupportedOperationException();
            }
            
            @Override
            public Style setBold(final Boolean boolean1) {
                throw new UnsupportedOperationException();
            }
            
            @Override
            public Style setItalic(final Boolean boolean1) {
                throw new UnsupportedOperationException();
            }
            
            @Override
            public Style setStrikethrough(final Boolean boolean1) {
                throw new UnsupportedOperationException();
            }
            
            @Override
            public Style setUnderlined(final Boolean boolean1) {
                throw new UnsupportedOperationException();
            }
            
            @Override
            public Style setObfuscated(final Boolean boolean1) {
                throw new UnsupportedOperationException();
            }
            
            @Override
            public Style setClickEvent(final ClickEvent le) {
                throw new UnsupportedOperationException();
            }
            
            @Override
            public Style setHoverEvent(final HoverEvent li) {
                throw new UnsupportedOperationException();
            }
            
            @Override
            public Style inheritFrom(final Style ln) {
                throw new UnsupportedOperationException();
            }
            
            @Override
            public String toString() {
                return "Style.ROOT";
            }
            
            @Override
            public Style copy() {
                return this;
            }
            
            @Override
            public Style flatCopy() {
                return this;
            }
            
            @Override
            public String getLegacyFormatCodes() {
                return "";
            }
        };
    }
    
    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 Style ln5 = new Style();
            final JsonObject jsonObject6 = jsonElement.getAsJsonObject();
            if (jsonObject6 == null) {
                return null;
            }
            if (jsonObject6.has("bold")) {
                ln5.bold = jsonObject6.get("bold").getAsBoolean();
            }
            if (jsonObject6.has("italic")) {
                ln5.italic = jsonObject6.get("italic").getAsBoolean();
            }
            if (jsonObject6.has("underlined")) {
                ln5.underlined = jsonObject6.get("underlined").getAsBoolean();
            }
            if (jsonObject6.has("strikethrough")) {
                ln5.strikethrough = jsonObject6.get("strikethrough").getAsBoolean();
            }
            if (jsonObject6.has("obfuscated")) {
                ln5.obfuscated = jsonObject6.get("obfuscated").getAsBoolean();
            }
            if (jsonObject6.has("color")) {
                ln5.color = jsonDeserializationContext.<ChatFormatting>deserialize(jsonObject6.get("color"), ChatFormatting.class);
            }
            if (jsonObject6.has("insertion")) {
                ln5.insertion = jsonObject6.get("insertion").getAsString();
            }
            if (jsonObject6.has("clickEvent")) {
                final JsonObject jsonObject7 = GsonHelper.getAsJsonObject(jsonObject6, "clickEvent");
                final String string8 = GsonHelper.getAsString(jsonObject7, "action", null);
                final ClickEvent.Action a9 = (string8 == null) ? null : ClickEvent.Action.getByName(string8);
                final String string9 = GsonHelper.getAsString(jsonObject7, "value", null);
                if (a9 != null && string9 != null && a9.isAllowedFromServer()) {
                    ln5.clickEvent = new ClickEvent(a9, string9);
                }
            }
            if (jsonObject6.has("hoverEvent")) {
                final JsonObject jsonObject7 = GsonHelper.getAsJsonObject(jsonObject6, "hoverEvent");
                final String string8 = GsonHelper.getAsString(jsonObject7, "action", null);
                final HoverEvent.Action a10 = (string8 == null) ? null : HoverEvent.Action.getByName(string8);
                final Component lf10 = jsonDeserializationContext.<Component>deserialize(jsonObject7.get("value"), Component.class);
                if (a10 != null && lf10 != null && a10.isAllowedFromServer()) {
                    ln5.hoverEvent = new HoverEvent(a10, lf10);
                }
            }
            return ln5;
        }
        
        @Nullable
        @Override
        public JsonElement serialize(final Style ln, final Type type, final JsonSerializationContext jsonSerializationContext) {
            if (ln.isEmpty()) {
                return null;
            }
            final JsonObject jsonObject5 = new JsonObject();
            if (ln.bold != null) {
                jsonObject5.addProperty("bold", ln.bold);
            }
            if (ln.italic != null) {
                jsonObject5.addProperty("italic", ln.italic);
            }
            if (ln.underlined != null) {
                jsonObject5.addProperty("underlined", ln.underlined);
            }
            if (ln.strikethrough != null) {
                jsonObject5.addProperty("strikethrough", ln.strikethrough);
            }
            if (ln.obfuscated != null) {
                jsonObject5.addProperty("obfuscated", ln.obfuscated);
            }
            if (ln.color != null) {
                jsonObject5.add("color", jsonSerializationContext.serialize(ln.color));
            }
            if (ln.insertion != null) {
                jsonObject5.add("insertion", jsonSerializationContext.serialize(ln.insertion));
            }
            if (ln.clickEvent != null) {
                final JsonObject jsonObject6 = new JsonObject();
                jsonObject6.addProperty("action", ln.clickEvent.getAction().getName());
                jsonObject6.addProperty("value", ln.clickEvent.getValue());
                jsonObject5.add("clickEvent", jsonObject6);
            }
            if (ln.hoverEvent != null) {
                final JsonObject jsonObject6 = new JsonObject();
                jsonObject6.addProperty("action", ln.hoverEvent.getAction().getName());
                jsonObject6.add("value", jsonSerializationContext.serialize(ln.hoverEvent.getValue()));
                jsonObject5.add("hoverEvent", jsonObject6);
            }
            return jsonObject5;
        }
    }
}
