package net.minecraft.server.packs.repository;

import java.util.function.Function;
import java.util.List;
import net.minecraft.SharedConstants;
import net.minecraft.network.chat.TranslatableComponent;
import org.apache.logging.log4j.LogManager;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.HoverEvent;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.TextComponent;
import javax.annotation.Nullable;
import java.io.IOException;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.PackResources;
import java.util.function.Supplier;
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
import org.apache.logging.log4j.Logger;

public class Pack implements AutoCloseable {
    private static final Logger LOGGER;
    private static final PackMetadataSection BROKEN_ASSETS_FALLBACK;
    private final String id;
    private final Supplier<PackResources> supplier;
    private final Component title;
    private final Component description;
    private final PackCompatibility compatibility;
    private final Position defaultPosition;
    private final boolean required;
    private final boolean fixedPosition;
    private final PackSource packSource;
    
    @Nullable
    public static <T extends Pack> T create(final String string, final boolean boolean2, final Supplier<PackResources> supplier, final PackConstructor<T> a, final Position b, final PackSource aas) {
        try (final PackResources aae7 = supplier.get()) {
            PackMetadataSection aaj9 = aae7.<PackMetadataSection>getMetadataSection((MetadataSectionSerializer<PackMetadataSection>)PackMetadataSection.SERIALIZER);
            if (boolean2 && aaj9 == null) {
                Pack.LOGGER.error("Broken/missing pack.mcmeta detected, fudging it into existance. Please check that your launcher has downloaded all assets for the game correctly!");
                aaj9 = Pack.BROKEN_ASSETS_FALLBACK;
            }
            if (aaj9 != null) {
                return a.create(string, boolean2, supplier, aae7, aaj9, b, aas);
            }
            Pack.LOGGER.warn("Couldn't find pack meta for pack {}", string);
        }
        catch (IOException iOException7) {
            Pack.LOGGER.warn("Couldn't get pack info for: {}", iOException7.toString());
        }
        return null;
    }
    
    public Pack(final String string, final boolean boolean2, final Supplier<PackResources> supplier, final Component mr4, final Component mr5, final PackCompatibility aaq, final Position b, final boolean boolean8, final PackSource aas) {
        this.id = string;
        this.supplier = supplier;
        this.title = mr4;
        this.description = mr5;
        this.compatibility = aaq;
        this.required = boolean2;
        this.defaultPosition = b;
        this.fixedPosition = boolean8;
        this.packSource = aas;
    }
    
    public Pack(final String string, final boolean boolean2, final Supplier<PackResources> supplier, final PackResources aae, final PackMetadataSection aaj, final Position b, final PackSource aas) {
        this(string, boolean2, supplier, new TextComponent(aae.getName()), aaj.getDescription(), PackCompatibility.forFormat(aaj.getPackFormat()), b, false, aas);
    }
    
    public Component getTitle() {
        return this.title;
    }
    
    public Component getDescription() {
        return this.description;
    }
    
    public Component getChatLink(final boolean boolean1) {
        final HoverEvent mv;
        final Style style;
        return ComponentUtils.wrapInSquareBrackets(this.packSource.decorate(new TextComponent(this.id))).withStyle(nb -> {
            nb.withColor(boolean1 ? ChatFormatting.GREEN : ChatFormatting.RED).withInsertion(StringArgumentType.escapeIfRequired(this.id));
            new HoverEvent((HoverEvent.Action<MutableComponent>)HoverEvent.Action.SHOW_TEXT, new TextComponent("").append(this.title).append("\n").append(this.description));
            return style.withHoverEvent(mv);
        });
    }
    
    public PackCompatibility getCompatibility() {
        return this.compatibility;
    }
    
    public PackResources open() {
        return this.supplier.get();
    }
    
    public String getId() {
        return this.id;
    }
    
    public boolean isRequired() {
        return this.required;
    }
    
    public boolean isFixedPosition() {
        return this.fixedPosition;
    }
    
    public Position getDefaultPosition() {
        return this.defaultPosition;
    }
    
    public PackSource getPackSource() {
        return this.packSource;
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (!(object instanceof Pack)) {
            return false;
        }
        final Pack aap3 = (Pack)object;
        return this.id.equals(aap3.id);
    }
    
    @Override
    public int hashCode() {
        return this.id.hashCode();
    }
    
    @Override
    public void close() {
    }
    
    static {
        LOGGER = LogManager.getLogger();
        BROKEN_ASSETS_FALLBACK = new PackMetadataSection(new TranslatableComponent("resourcePack.broken_assets").withStyle(ChatFormatting.RED, ChatFormatting.ITALIC), SharedConstants.getCurrentVersion().getPackVersion());
    }
    
    public enum Position {
        TOP, 
        BOTTOM;
        
        public <T, P extends Pack> int insert(final List<T> list, final T object, final Function<T, P> function, final boolean boolean4) {
            final Position b6 = boolean4 ? this.opposite() : this;
            if (b6 == Position.BOTTOM) {
                int integer7;
                for (integer7 = 0; integer7 < list.size(); ++integer7) {
                    final P aap8 = function.apply(list.get(integer7));
                    if (!aap8.isFixedPosition() || aap8.getDefaultPosition() != this) {
                        break;
                    }
                }
                list.add(integer7, object);
                return integer7;
            }
            int integer7;
            for (integer7 = list.size() - 1; integer7 >= 0; --integer7) {
                final P aap8 = function.apply(list.get(integer7));
                if (!aap8.isFixedPosition() || aap8.getDefaultPosition() != this) {
                    break;
                }
            }
            list.add(integer7 + 1, object);
            return integer7 + 1;
        }
        
        public Position opposite() {
            return (this == Position.TOP) ? Position.BOTTOM : Position.TOP;
        }
    }
    
    @FunctionalInterface
    public interface PackConstructor<T extends Pack> {
        @Nullable
        T create(final String string, final boolean boolean2, final Supplier<PackResources> supplier, final PackResources aae, final PackMetadataSection aaj, final Position b, final PackSource aas);
    }
}
