package top.leisuretimedock.dglabgame.config;

import net.minecraftforge.common.ForgeConfigSpec;

public class CommonConfig {
    public static final ForgeConfigSpec SPEC;

    public static final ForgeConfigSpec.ConfigValue<String> ADDRESS;
    public static final ForgeConfigSpec.IntValue PORT;

    public static final ForgeConfigSpec.IntValue MAX_STRENGTH_A;
    public static final ForgeConfigSpec.IntValue MAX_STRENGTH_B;
    public static final ForgeConfigSpec.IntValue MIN_STRENGTH_A;
    public static final ForgeConfigSpec.IntValue MIN_STRENGTH_B;

    public static final ForgeConfigSpec.IntValue MAX_FREQUENCY;
    public static final ForgeConfigSpec.IntValue MIN_FREQUENCY;
    public static final ForgeConfigSpec.IntValue MAX_PULSE_STRENGTH;
    public static final ForgeConfigSpec.IntValue ANTI_SHAKE_DELAY;

    public static final ForgeConfigSpec.IntValue COUNTDOWN_SECONDS;

    public static final ForgeConfigSpec.IntValue SERVER_PORT;
    public static final ForgeConfigSpec.IntValue MAX_CONNECTIONS;

    static {
        ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();

        builder.push("websocket");
        ADDRESS = builder
                .comment("WebSocket server address for the DGLab PowerBox relay.",
                        "Change this if the relay is running on a different machine.")
                .define("address", "127.0.0.1",
                        o -> o instanceof String && !((String) o).isBlank());
        PORT = builder
                .comment("WebSocket server port for the DGLab PowerBox relay.")
                .defineInRange("port", 9000, 1, 65535);
        builder.pop();

        builder.push("strength_limits");
        MAX_STRENGTH_A = builder
                .comment("Maximum allowed strength for channel A.")
                .defineInRange("maxStrengthA", 200, 0, 200);
        MAX_STRENGTH_B = builder
                .comment("Maximum allowed strength for channel B.")
                .defineInRange("maxStrengthB", 200, 0, 200);
        MIN_STRENGTH_A = builder
                .comment("Minimum allowed strength for channel A.")
                .defineInRange("minStrengthA", 0, 0, 200);
        MIN_STRENGTH_B = builder
                .comment("Minimum allowed strength for channel B.")
                .defineInRange("minStrengthB", 0, 0, 200);
        builder.pop();

        builder.push("pulse");
        MAX_FREQUENCY = builder
                .comment("Maximum pulse frequency in Hz.")
                .defineInRange("maxFrequency", 240, 10, 240);
        MIN_FREQUENCY = builder
                .comment("Minimum pulse frequency in Hz.")
                .defineInRange("minFrequency", 10, 10, 240);
        MAX_PULSE_STRENGTH = builder
                .comment("Maximum pulse strength (0-100).")
                .defineInRange("maxPulseStrength", 100, 0, 100);
        ANTI_SHAKE_DELAY = builder
                .comment("Anti-shake debounce delay in milliseconds.",
                        "Prevents rapid repeated pulse triggers within this window.")
                .defineInRange("antiShakeDelay", 500, 0, 5000);
        builder.pop();

        builder.push("agreement");
        COUNTDOWN_SECONDS = builder
                .comment("Countdown duration in seconds for the user agreement confirmation screen.",
                        "Set to 0 to allow instant confirmation (not recommended).")
                .defineInRange("countdownSeconds", 5, 0, 60);
        builder.pop();

        builder.push("server");
        SERVER_PORT = builder
                .comment("Port for the DGLab WebSocket server to listen on.",
                        "Restart the server for changes to take effect.")
                .defineInRange("serverPort", 9000, 1, 65535);
        MAX_CONNECTIONS = builder
                .comment("Maximum number of concurrent WebSocket connections allowed.",
                        "Additional connections beyond this limit will be rejected.")
                .defineInRange("maxConnections", 128, 1, 1024);
        builder.pop();

        SPEC = builder.build();
    }
}
