// Input handler for Kafaball
// Captures keyboard input and sends to server at fixed rate

class InputHandler {
    constructor() {
        this.keys = {
            up: false,
            down: false,
            left: false,
            right: false,
            kick: false
        };
        this.prevKeys = { ...this.keys };
        this.enabled = false;
        this.chatFocused = false;

        this._onKeyDown = this._onKeyDown.bind(this);
        this._onKeyUp = this._onKeyUp.bind(this);

        document.addEventListener('keydown', this._onKeyDown);
        document.addEventListener('keyup', this._onKeyUp);

        // Send input at 60Hz
        this.sendInterval = setInterval(() => {
            if (this.enabled && !this.chatFocused) {
                // Only send if input changed
                if (this.keys.up !== this.prevKeys.up ||
                    this.keys.down !== this.prevKeys.down ||
                    this.keys.left !== this.prevKeys.left ||
                    this.keys.right !== this.prevKeys.right ||
                    this.keys.kick !== this.prevKeys.kick) {
                    network.sendInput(this.keys);
                    this.prevKeys = { ...this.keys };
                }
            }
        }, 1000 / 60);
    }

    _onKeyDown(e) {
        if (this.chatFocused) return;
        if (!this.enabled) return;

        switch (e.code) {
            case 'ArrowUp':
            case 'KeyW':
                e.preventDefault();
                this.keys.up = true;
                break;
            case 'ArrowDown':
            case 'KeyS':
                e.preventDefault();
                this.keys.down = true;
                break;
            case 'ArrowLeft':
            case 'KeyA':
                e.preventDefault();
                this.keys.left = true;
                break;
            case 'ArrowRight':
            case 'KeyD':
                e.preventDefault();
                this.keys.right = true;
                break;
            case 'Space':
            case 'KeyX':
                e.preventDefault();
                this.keys.kick = true;
                break;
        }
    }

    _onKeyUp(e) {
        switch (e.code) {
            case 'ArrowUp':
            case 'KeyW':
                this.keys.up = false;
                break;
            case 'ArrowDown':
            case 'KeyS':
                this.keys.down = false;
                break;
            case 'ArrowLeft':
            case 'KeyA':
                this.keys.left = false;
                break;
            case 'ArrowRight':
            case 'KeyD':
                this.keys.right = false;
                break;
            case 'Space':
            case 'KeyX':
                this.keys.kick = false;
                break;
        }
    }

    enable() {
        this.enabled = true;
        this.resetKeys();
    }

    disable() {
        this.enabled = false;
        this.resetKeys();
        network.sendInput(this.keys);
    }

    resetKeys() {
        this.keys = { up: false, down: false, left: false, right: false, kick: false };
        this.prevKeys = { ...this.keys };
    }

    setChatFocused(focused) {
        this.chatFocused = focused;
        if (focused) {
            // Release all keys when chat is focused
            this.keys = { up: false, down: false, left: false, right: false, kick: false };
            network.sendInput(this.keys);
        }
    }
}

// Global instance
const inputHandler = new InputHandler();
