/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.mojang.brigadier.arguments.IntegerArgumentType
 *  com.mojang.brigadier.builder.LiteralArgumentBuilder
 *  com.mojang.brigadier.context.CommandContext
 *  com.mojang.datafixers.util.Pair
 *  meteordevelopment.orbit.EventHandler
 *  meteordevelopment.orbit.IEventBus
 *  net.minecraft.class_1074
 *  net.minecraft.class_2172
 *  net.minecraft.class_304
 */
package meteordevelopment.meteorclient.commands.commands;

import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.datafixers.util.Pair;
import java.util.ArrayList;
import java.util.List;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.commands.Command;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.mixin.KeyBindingAccessor;
import meteordevelopment.orbit.EventHandler;
import meteordevelopment.orbit.IEventBus;
import net.minecraft.class_1074;
import net.minecraft.class_2172;
import net.minecraft.class_304;

public class InputCommand
extends Command {
    private static final List<KeypressHandler> activeHandlers = new ArrayList<KeypressHandler>();
    private static final List<Pair<class_304, String>> holdKeys = List.of(new Pair((Object)InputCommand.mc.field_1690.field_1894, (Object)"forwards"), new Pair((Object)InputCommand.mc.field_1690.field_1881, (Object)"backwards"), new Pair((Object)InputCommand.mc.field_1690.field_1913, (Object)"left"), new Pair((Object)InputCommand.mc.field_1690.field_1849, (Object)"right"), new Pair((Object)InputCommand.mc.field_1690.field_1903, (Object)"jump"), new Pair((Object)InputCommand.mc.field_1690.field_1832, (Object)"sneak"), new Pair((Object)InputCommand.mc.field_1690.field_1867, (Object)"sprint"), new Pair((Object)InputCommand.mc.field_1690.field_1904, (Object)"use"), new Pair((Object)InputCommand.mc.field_1690.field_1886, (Object)"attack"));
    private static final List<Pair<class_304, String>> pressKeys = List.of(new Pair((Object)InputCommand.mc.field_1690.field_1831, (Object)"swap"), new Pair((Object)InputCommand.mc.field_1690.field_1869, (Object)"drop"));

    public InputCommand() {
        super("input", "Keyboard input simulation.", new String[0]);
    }

    @Override
    public void build(LiteralArgumentBuilder<class_2172> builder) {
        for (Pair<class_304, String> keyBinding : holdKeys) {
            builder.then(((LiteralArgumentBuilder)InputCommand.literal((String)keyBinding.getSecond()).executes(context -> {
                activeHandlers.add(new KeypressHandler((class_304)keyBinding.getFirst(), 1));
                return 1;
            })).then(InputCommand.argument("ticks", IntegerArgumentType.integer((int)1)).executes(context -> {
                activeHandlers.add(new KeypressHandler((class_304)keyBinding.getFirst(), (Integer)context.getArgument("ticks", Integer.class)));
                return 1;
            })));
        }
        for (Pair<class_304, String> keyBinding : pressKeys) {
            builder.then(InputCommand.literal((String)keyBinding.getSecond()).executes(context -> {
                InputCommand.press((class_304)keyBinding.getFirst());
                return 1;
            }));
        }
        for (class_304 keyBinding : InputCommand.mc.field_1690.field_1852) {
            builder.then(InputCommand.literal(keyBinding.method_1431().substring(4)).executes(context -> {
                InputCommand.press(keyBinding);
                return 1;
            }));
        }
        builder.then(InputCommand.literal("clear").executes(ctx -> {
            if (activeHandlers.isEmpty()) {
                this.warning("No active keypress handlers.", new Object[0]);
            } else {
                this.info("Cleared all keypress handlers.", new Object[0]);
                activeHandlers.forEach(arg_0 -> ((IEventBus)MeteorClient.EVENT_BUS).unsubscribe(arg_0));
                activeHandlers.clear();
            }
            return 1;
        }));
        builder.then(InputCommand.literal("list").executes(ctx -> {
            if (activeHandlers.isEmpty()) {
                this.warning("No active keypress handlers.", new Object[0]);
            } else {
                this.info("Active keypress handlers: ", new Object[0]);
                for (int i = 0; i < activeHandlers.size(); ++i) {
                    KeypressHandler handler = activeHandlers.get(i);
                    this.info("(highlight)%d(default) - (highlight)%s %d(default) ticks left out of (highlight)%d(default).", i, class_1074.method_4662((String)handler.key.method_1431(), (Object[])new Object[0]), handler.ticks, handler.totalTicks);
                }
            }
            return 1;
        }));
        builder.then(InputCommand.literal("remove").then(InputCommand.argument("index", IntegerArgumentType.integer((int)0)).executes(ctx -> {
            int index = IntegerArgumentType.getInteger((CommandContext)ctx, (String)"index");
            if (index >= activeHandlers.size()) {
                this.warning("Index out of range.", new Object[0]);
            } else {
                this.info("Removed keypress handler.", new Object[0]);
                MeteorClient.EVENT_BUS.unsubscribe((Object)activeHandlers.get(index));
                activeHandlers.remove(index);
            }
            return 1;
        })));
    }

    private static void press(class_304 keyBinding) {
        KeyBindingAccessor accessor = (KeyBindingAccessor)keyBinding;
        accessor.meteor$setTimesPressed(accessor.meteor$getTimesPressed() + 1);
    }

    private static class KeypressHandler {
        private final class_304 key;
        private final int totalTicks;
        private int ticks;

        public KeypressHandler(class_304 key, int ticks) {
            this.key = key;
            this.totalTicks = ticks;
            this.ticks = ticks;
            MeteorClient.EVENT_BUS.subscribe((Object)this);
        }

        @EventHandler
        private void onTick(TickEvent.Post event) {
            if (this.ticks == this.totalTicks) {
                InputCommand.press(this.key);
            }
            if (this.ticks-- > 0) {
                this.key.method_23481(true);
            } else {
                this.key.method_23481(false);
                MeteorClient.EVENT_BUS.unsubscribe((Object)this);
                activeHandlers.remove(this);
            }
        }
    }
}

