#include rng.js

const OsuKeys = (() => {
    const rng = new MamiRNG;
    let sndRng = false;

    const keyHandler = ev => {
        if(ev.key === 'Control' || ev.key === 'Alt' || ev.key === 'Shift')
            return;

        let soundName;
        if(ev.key === 'Enter' || ev.key === 'NumpadEnter')
            soundName = 'confirm';
        else if(ev.key === 'Backspace' || ev.key === 'Delete')
            soundName = 'delete';
        else if(ev.key === 'NumLock' || ev.key === 'CapsLock' || ev.key === 'ScrollLock')
            soundName = 'caps';
        else if(ev.key === 'ArrowUp' || ev.key === 'ArrowDown' || ev.key === 'ArrowLeft' || ev.key === 'ArrowRight')
            soundName = 'movement';
        else
            soundName = `press${rng.next(1, 5)}`;

        mami.sound.library.play(
            `osu:key:${soundName}`,
            undefined,
            sndRng ? (1.8 - (rng.sample() * 1.5)) : undefined
        );
    };

    return {
        setEnable: function(value) {
            if(value)
                window.addEventListener('keydown', keyHandler);
            else
                window.removeEventListener('keydown', keyHandler);
        },
        setRandomRate: function(value) { sndRng = !!value; },
    };
})();
