package net.minecraft.client.gui.screens;

import net.minecraft.network.chat.FormattedText;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.network.protocol.game.ServerboundLockDifficultyPacket;
import java.util.Iterator;
import java.util.List;
import java.util.Collection;
import com.google.common.collect.ImmutableList;
import net.minecraft.client.resources.ResourcePack;
import net.minecraft.server.packs.repository.PackRepository;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.client.gui.screens.controls.ControlsScreen;
import net.minecraft.client.gui.components.OptionButton;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerboundChangeDifficultyPacket;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.Difficulty;
import net.minecraft.client.gui.components.LockIconButton;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.Options;
import net.minecraft.client.Option;

public class OptionsScreen extends Screen {
    private static final Option[] OPTION_SCREEN_OPTIONS;
    private final Screen lastScreen;
    private final Options options;
    private Button difficultyButton;
    private LockIconButton lockButton;
    private Difficulty currentDifficulty;
    
    public OptionsScreen(final Screen dqs, final Options dmb) {
        super(new TranslatableComponent("options.title"));
        this.lastScreen = dqs;
        this.options = dmb;
    }
    
    @Override
    protected void init() {
        int integer2 = 0;
        for (final Option dma6 : OptionsScreen.OPTION_SCREEN_OPTIONS) {
            final int integer3 = this.width / 2 - 155 + integer2 % 2 * 160;
            final int integer4 = this.height / 6 - 12 + 24 * (integer2 >> 1);
            this.<AbstractWidget>addButton(dma6.createButton(this.minecraft.options, integer3, integer4, 150));
            ++integer2;
        }
        if (this.minecraft.level != null) {
            this.currentDifficulty = this.minecraft.level.getDifficulty();
            this.difficultyButton = this.<Button>addButton(new Button(this.width / 2 - 155 + integer2 % 2 * 160, this.height / 6 - 12 + 24 * (integer2 >> 1), 150, 20, this.getDifficultyText(this.currentDifficulty), dni -> {
                this.currentDifficulty = Difficulty.byId(this.currentDifficulty.getId() + 1);
                this.minecraft.getConnection().send(new ServerboundChangeDifficultyPacket(this.currentDifficulty));
                this.difficultyButton.setMessage(this.getDifficultyText(this.currentDifficulty));
                return;
            }));
            if (this.minecraft.hasSingleplayerServer() && !this.minecraft.level.getLevelData().isHardcore()) {
                this.difficultyButton.setWidth(this.difficultyButton.getWidth() - 20);
                final Minecraft minecraft;
                final TranslatableComponent mr2;
                final Object[] arr;
                final TranslatableComponent translatableComponent;
                final Object o;
                final Component mr3;
                final String string;
                final Screen screen;
                final BooleanConsumer booleanConsumer;
                (this.lockButton = this.<LockIconButton>addButton(new LockIconButton(this.difficultyButton.x + this.difficultyButton.getWidth(), this.difficultyButton.y, dni -> {
                    minecraft = this.minecraft;
                    // new(net.minecraft.client.gui.screens.ConfirmScreen.class)
                    this::lockCallback;
                    mr2 = new TranslatableComponent("difficulty.lock.title");
                    // new(net.minecraft.network.chat.TranslatableComponent.class)
                    arr = new Object[] { null };
                    new TranslatableComponent("options.difficulty." + this.minecraft.level.getLevelData().getDifficulty().getKey());
                    arr[o] = translatableComponent;
                    new TranslatableComponent(string, arr);
                    new ConfirmScreen(booleanConsumer, mr2, mr3);
                    minecraft.setScreen(screen);
                    return;
                }))).setLocked(this.minecraft.level.getLevelData().isDifficultyLocked());
                this.lockButton.active = !this.lockButton.isLocked();
                this.difficultyButton.active = !this.lockButton.isLocked();
            }
            else {
                this.difficultyButton.active = false;
            }
        }
        else {
            this.<OptionButton>addButton(new OptionButton(this.width / 2 - 155 + integer2 % 2 * 160, this.height / 6 - 12 + 24 * (integer2 >> 1), 150, 20, Option.REALMS_NOTIFICATIONS, Option.REALMS_NOTIFICATIONS.getMessage(this.options), dni -> {
                Option.REALMS_NOTIFICATIONS.toggle(this.options);
                this.options.save();
                dni.setMessage(Option.REALMS_NOTIFICATIONS.getMessage(this.options));
                return;
            }));
        }
        this.<Button>addButton(new Button(this.width / 2 - 155, this.height / 6 + 48 - 6, 150, 20, new TranslatableComponent("options.skinCustomisation"), dni -> this.minecraft.setScreen(new SkinCustomizationScreen(this, this.options))));
        this.<Button>addButton(new Button(this.width / 2 + 5, this.height / 6 + 48 - 6, 150, 20, new TranslatableComponent("options.sounds"), dni -> this.minecraft.setScreen(new SoundOptionsScreen(this, this.options))));
        this.<Button>addButton(new Button(this.width / 2 - 155, this.height / 6 + 72 - 6, 150, 20, new TranslatableComponent("options.video"), dni -> this.minecraft.setScreen(new VideoSettingsScreen(this, this.options))));
        this.<Button>addButton(new Button(this.width / 2 + 5, this.height / 6 + 72 - 6, 150, 20, new TranslatableComponent("options.controls"), dni -> this.minecraft.setScreen(new ControlsScreen(this, this.options))));
        this.<Button>addButton(new Button(this.width / 2 - 155, this.height / 6 + 96 - 6, 150, 20, new TranslatableComponent("options.language"), dni -> this.minecraft.setScreen(new LanguageSelectScreen(this, this.options, this.minecraft.getLanguageManager()))));
        this.<Button>addButton(new Button(this.width / 2 + 5, this.height / 6 + 96 - 6, 150, 20, new TranslatableComponent("options.chat.title"), dni -> this.minecraft.setScreen(new ChatOptionsScreen(this, this.options))));
        this.<Button>addButton(new Button(this.width / 2 - 155, this.height / 6 + 120 - 6, 150, 20, new TranslatableComponent("options.resourcepack"), dni -> this.minecraft.setScreen(new ResourcePackSelectScreen(this, this.minecraft.getResourcePackRepository(), this::updatePackList, this.minecraft.getResourcePackDirectory()))));
        this.<Button>addButton(new Button(this.width / 2 + 5, this.height / 6 + 120 - 6, 150, 20, new TranslatableComponent("options.accessibility.title"), dni -> this.minecraft.setScreen(new AccessibilityOptionsScreen(this, this.options))));
        this.<Button>addButton(new Button(this.width / 2 - 100, this.height / 6 + 168, 200, 20, CommonComponents.GUI_DONE, dni -> this.minecraft.setScreen(this.lastScreen)));
    }
    
    private void updatePackList(final PackRepository<ResourcePack> aar) {
        final List<String> list3 = ImmutableList.copyOf(this.options.resourcePacks);
        this.options.resourcePacks.clear();
        this.options.incompatibleResourcePacks.clear();
        for (final ResourcePack eml5 : aar.getSelectedPacks()) {
            if (!eml5.isFixedPosition()) {
                this.options.resourcePacks.add(eml5.getId());
                if (eml5.getCompatibility().isCompatible()) {
                    continue;
                }
                this.options.incompatibleResourcePacks.add(eml5.getId());
            }
        }
        this.options.save();
        final List<String> list4 = ImmutableList.copyOf(this.options.resourcePacks);
        if (!list4.equals(list3)) {
            this.minecraft.reloadResourcePacks();
        }
    }
    
    private Component getDifficultyText(final Difficulty and) {
        return new TranslatableComponent("options.difficulty").append(": ").append(and.getDisplayName());
    }
    
    private void lockCallback(final boolean boolean1) {
        this.minecraft.setScreen(this);
        if (boolean1 && this.minecraft.level != null) {
            this.minecraft.getConnection().send(new ServerboundLockDifficultyPacket(true));
            this.lockButton.setLocked(true);
            this.lockButton.active = false;
            this.difficultyButton.active = false;
        }
    }
    
    @Override
    public void removed() {
        this.options.save();
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBackground(dhl);
        this.drawCenteredString(dhl, this.font, this.title, this.width / 2, 15, 16777215);
        super.render(dhl, integer2, integer3, float4);
    }
    
    static {
        OPTION_SCREEN_OPTIONS = new Option[] { Option.FOV };
    }
}
