import { Gamepad } from "./gamepad";
import { platform } from "os";
import { Keybaord } from "./keyboard";

export class GamepadManager
{
    private gamepads: Gamepad[] = [];
    private keyboard: Keybaord;
    private scanInterval: any;

    constructor()
    {
        this.scanGamepads();
        this.keyboard = new Keybaord();
        this.keyboard.init();
        // scan every second for new/disconnected devices
        this.scanInterval = setInterval(async () => this.scanGamepads(), 1000);
    }

    private async scanGamepads ()
    {
        const max = platform() === "win32" ? 4 : 8; // max controllers
        for (let i = 0; i < max; i++)
        {
            if (!this.gamepads[i])
            {
                try
                {
                    const pad = new Gamepad(i);
                    await pad.init();
                    if (pad.update())
                    {
                        this.gamepads[i] = pad;
                        console.log(`Gamepad ${i} connected`);
                    }
                } catch { }
            } else
            {
                const connected = this.gamepads[i].update() !== null;
                if (!connected)
                {
                    console.log(`Gamepad ${i} disconnected`);
                    this.gamepads[i].close();
                    delete this.gamepads[i];
                }
            }
        }
    }

    getKeyboard ()
    {
        return this.keyboard;
    }

    getGamepads ()
    {
        return this.gamepads.filter(Boolean);
    }

    stop ()
    {
        clearInterval(this.scanInterval);
        for (const pad of this.gamepads) pad.close?.();
    }
}