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

import net.minecraft.client.gui.components.AbstractSelectionList;
import com.google.common.collect.ImmutableList;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.components.Button;
import java.util.Collections;
import net.minecraft.client.gui.components.events.GuiEventListener;
import java.util.List;
import net.minecraft.client.gui.Font;
import net.minecraft.client.resources.language.I18n;
import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.ContainerObjectSelectionList;

public class ControlList extends ContainerObjectSelectionList<Entry> {
    private final ControlsScreen controlsScreen;
    private int maxNameWidth;
    
    public ControlList(final ControlsScreen dgs, final Minecraft dbn) {
        super(dbn, dgs.width + 45, dgs.height, 43, dgs.height - 32, 20);
        this.controlsScreen = dgs;
        final KeyMapping[] arr4 = (KeyMapping[])ArrayUtils.clone((Object[])dbn.options.keyMappings);
        Arrays.sort(arr4);
        String string5 = null;
        for (final KeyMapping dbk9 : arr4) {
            final String string6 = dbk9.getCategory();
            if (!string6.equals(string5)) {
                string5 = string6;
                ((AbstractSelectionList<CategoryEntry>)this).addEntry(new CategoryEntry(string6));
            }
            final int integer11 = dbn.font.width(I18n.get(dbk9.getName()));
            if (integer11 > this.maxNameWidth) {
                this.maxNameWidth = integer11;
            }
            ((AbstractSelectionList<KeyEntry>)this).addEntry(new KeyEntry(dbk9));
        }
    }
    
    @Override
    protected int getScrollbarPosition() {
        return super.getScrollbarPosition() + 15;
    }
    
    @Override
    public int getRowWidth() {
        return super.getRowWidth() + 32;
    }
    
    public abstract static class Entry extends ContainerObjectSelectionList.Entry<Entry> {
    }
    
    public class CategoryEntry extends Entry {
        private final String name;
        private final int width;
        
        public CategoryEntry(final String string) {
            this.name = I18n.get(string);
            this.width = ControlList.this.minecraft.font.width(this.name);
        }
        
        @Override
        public void render(final int integer1, final int integer2, final int integer3, final int integer4, final int integer5, final int integer6, final int integer7, final boolean boolean8, final float float9) {
            final Font font = ControlList.this.minecraft.font;
            final String name = this.name;
            final float float10 = (float)(ControlList.this.minecraft.screen.width / 2 - this.width / 2);
            final int n = integer2 + integer5;
            ControlList.this.minecraft.font.getClass();
            font.draw(name, float10, (float)(n - 9 - 1), 16777215);
        }
        
        @Override
        public boolean changeFocus(final boolean boolean1) {
            return false;
        }
        
        @Override
        public List<? extends GuiEventListener> children() {
            return Collections.emptyList();
        }
    }
    
    public class KeyEntry extends Entry {
        private final KeyMapping key;
        private final String name;
        private final Button changeButton;
        private final Button resetButton;
        
        private KeyEntry(final KeyMapping dbk) {
            this.key = dbk;
            this.name = I18n.get(dbk.getName());
            this.changeButton = new Button(0, 0, 75, 20, this.name, dcv -> ControlList.this.controlsScreen.selectedKey = dbk) {
                @Override
                protected String getNarrationMessage() {
                    if (dbk.isUnbound()) {
                        return I18n.get("narrator.controls.unbound", KeyEntry.this.name);
                    }
                    return I18n.get("narrator.controls.bound", KeyEntry.this.name, super.getNarrationMessage());
                }
            };
            this.resetButton = new Button(0, 0, 50, 20, I18n.get("controls.reset"), dcv -> {
                ControlList.this.minecraft.options.setKey(dbk, dbk.getDefaultKey());
                KeyMapping.resetMapping();
            }) {
                @Override
                protected String getNarrationMessage() {
                    return I18n.get("narrator.controls.reset", KeyEntry.this.name);
                }
            };
        }
        
        @Override
        public void render(final int integer1, final int integer2, final int integer3, final int integer4, final int integer5, final int integer6, final int integer7, final boolean boolean8, final float float9) {
            final boolean boolean9 = ControlList.this.controlsScreen.selectedKey == this.key;
            final Font font = ControlList.this.minecraft.font;
            final String name = this.name;
            final float float10 = (float)(integer3 + 90 - ControlList.this.maxNameWidth);
            final int n = integer2 + integer5 / 2;
            ControlList.this.minecraft.font.getClass();
            font.draw(name, float10, (float)(n - 9 / 2), 16777215);
            this.resetButton.x = integer3 + 190;
            this.resetButton.y = integer2;
            this.resetButton.active = !this.key.isDefault();
            this.resetButton.render(integer6, integer7, float9);
            this.changeButton.x = integer3 + 105;
            this.changeButton.y = integer2;
            this.changeButton.setMessage(this.key.getTranslatedKeyMessage());
            boolean boolean10 = false;
            if (!this.key.isUnbound()) {
                for (final KeyMapping dbk16 : ControlList.this.minecraft.options.keyMappings) {
                    if (dbk16 != this.key && this.key.same(dbk16)) {
                        boolean10 = true;
                        break;
                    }
                }
            }
            if (boolean9) {
                this.changeButton.setMessage(ChatFormatting.WHITE + "> " + ChatFormatting.YELLOW + this.changeButton.getMessage() + ChatFormatting.WHITE + " <");
            }
            else if (boolean10) {
                this.changeButton.setMessage(ChatFormatting.RED + this.changeButton.getMessage());
            }
            this.changeButton.render(integer6, integer7, float9);
        }
        
        @Override
        public List<? extends GuiEventListener> children() {
            return ImmutableList.<Button>of(this.changeButton, this.resetButton);
        }
        
        @Override
        public boolean mouseClicked(final double double1, final double double2, final int integer) {
            return this.changeButton.mouseClicked(double1, double2, integer) || this.resetButton.mouseClicked(double1, double2, integer);
        }
        
        @Override
        public boolean mouseReleased(final double double1, final double double2, final int integer) {
            return this.changeButton.mouseReleased(double1, double2, integer) || this.resetButton.mouseReleased(double1, double2, integer);
        }
    }
}
