package net.minecraft.client.gui.screens.resourcepacks;

import net.minecraft.server.packs.repository.PackRepository;
import java.util.Iterator;
import net.minecraft.client.resources.UnopenedResourcePack;
import java.util.Collection;
import java.util.List;
import java.util.Collections;
import net.minecraft.client.gui.screens.resourcepacks.lists.ResourcePackList;
import com.google.common.collect.Lists;
import net.minecraft.client.gui.components.Button;
import net.minecraft.Util;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.Options;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.resourcepacks.lists.SelectedResourcePackList;
import net.minecraft.client.gui.screens.resourcepacks.lists.AvailableResourcePackList;
import net.minecraft.client.gui.screens.OptionsSubScreen;

public class ResourcePackSelectScreen extends OptionsSubScreen {
    private AvailableResourcePackList availableResourcePackList;
    private SelectedResourcePackList selectedResourcePackList;
    private boolean changed;
    
    public ResourcePackSelectScreen(final Screen dgb, final Options dbr) {
        super(dgb, dbr, new TranslatableComponent("resourcePack.title", new Object[0]));
    }
    
    @Override
    protected void init() {
        this.<Button>addButton(new Button(this.width / 2 - 154, this.height - 48, 150, 20, I18n.get("resourcePack.openFolder"), dcv -> Util.getPlatform().openFile(this.minecraft.getResourcePackDirectory())));
        List<UnopenedResourcePack> list3;
        final Iterator<ResourcePackList.ResourcePackEntry> iterator;
        ResourcePackList.ResourcePackEntry a2;
        final Iterator<UnopenedResourcePack> iterator2;
        UnopenedResourcePack ebc5;
        this.<Button>addButton(new Button(this.width / 2 + 4, this.height - 48, 150, 20, I18n.get("gui.done"), dcv -> {
            if (this.changed) {
                list3 = Lists.newArrayList();
                this.selectedResourcePackList.children().iterator();
                while (iterator.hasNext()) {
                    a2 = iterator.next();
                    list3.add(a2.getResourcePack());
                }
                Collections.reverse(list3);
                this.minecraft.getResourcePackRepository().setSelected(list3);
                this.options.resourcePacks.clear();
                this.options.incompatibleResourcePacks.clear();
                list3.iterator();
                while (iterator2.hasNext()) {
                    ebc5 = iterator2.next();
                    if (!ebc5.isFixedPosition()) {
                        this.options.resourcePacks.add(ebc5.getId());
                        if (!ebc5.getCompatibility().isCompatible()) {
                            this.options.incompatibleResourcePacks.add(ebc5.getId());
                        }
                        else {
                            continue;
                        }
                    }
                }
                this.options.save();
                this.minecraft.setScreen(this.lastScreen);
                this.minecraft.reloadResourcePacks();
            }
            else {
                this.minecraft.setScreen(this.lastScreen);
            }
            return;
        }));
        final AvailableResourcePackList djb2 = this.availableResourcePackList;
        final SelectedResourcePackList djd3 = this.selectedResourcePackList;
        (this.availableResourcePackList = new AvailableResourcePackList(this.minecraft, 200, this.height)).setLeftPos(this.width / 2 - 4 - 200);
        if (djb2 != null) {
            this.availableResourcePackList.children().addAll(djb2.children());
        }
        this.children.add(this.availableResourcePackList);
        (this.selectedResourcePackList = new SelectedResourcePackList(this.minecraft, 200, this.height)).setLeftPos(this.width / 2 + 4);
        if (djd3 != null) {
            djd3.children().forEach(a -> {
                this.selectedResourcePackList.children().add(a);
                a.updateParentList(this.selectedResourcePackList);
                return;
            });
        }
        this.children.add(this.selectedResourcePackList);
        if (!this.changed) {
            this.availableResourcePackList.children().clear();
            this.selectedResourcePackList.children().clear();
            final PackRepository<UnopenedResourcePack> yq4 = this.minecraft.getResourcePackRepository();
            yq4.reload();
            final List<UnopenedResourcePack> list4 = Lists.newArrayList(yq4.getAvailable());
            list4.removeAll(yq4.getSelected());
            for (final UnopenedResourcePack ebc6 : list4) {
                this.availableResourcePackList.addResourcePackEntry(new ResourcePackList.ResourcePackEntry(this.availableResourcePackList, this, ebc6));
            }
            for (final UnopenedResourcePack ebc6 : Lists.<UnopenedResourcePack>reverse(Lists.newArrayList(yq4.getSelected()))) {
                this.selectedResourcePackList.addResourcePackEntry(new ResourcePackList.ResourcePackEntry(this.selectedResourcePackList, this, ebc6));
            }
        }
    }
    
    public void select(final ResourcePackList.ResourcePackEntry a) {
        this.availableResourcePackList.children().remove(a);
        a.addToList(this.selectedResourcePackList);
        this.setChanged();
    }
    
    public void deselect(final ResourcePackList.ResourcePackEntry a) {
        this.selectedResourcePackList.children().remove(a);
        this.availableResourcePackList.addResourcePackEntry(a);
        this.setChanged();
    }
    
    public boolean isSelected(final ResourcePackList.ResourcePackEntry a) {
        return this.selectedResourcePackList.children().contains(a);
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        this.renderDirtBackground(0);
        this.availableResourcePackList.render(integer1, integer2, float3);
        this.selectedResourcePackList.render(integer1, integer2, float3);
        this.drawCenteredString(this.font, this.title.getColoredString(), this.width / 2, 16, 16777215);
        this.drawCenteredString(this.font, I18n.get("resourcePack.folderInfo"), this.width / 2 - 77, this.height - 26, 8421504);
        super.render(integer1, integer2, float3);
    }
    
    public void setChanged() {
        this.changed = true;
    }
}
