// by VienDesu! Poring Team 2023

function create_fps_button() {
    // Remove in release 
    const keys = [{ key: 'F2', keyCode: 113 }];

    const FPSBtn = document.createElement("div");
    FPSBtn.className = "fps-button";
    FPSBtn.id = "buttonF2";
    document.body.appendChild(FPSBtn); 

    for (const key of keys) {
        const elementId = key.id === undefined ? `button${key.key}` : key.id;
        console.log(`Setting up the ${elementId} key...`);
        setupKey(elementId, key.key, key.keyCode);
    }
}

function sendEvent(isUp, keycode) {
    document.getElementById("GameCanvas").focus();
    document.dispatchEvent(new KeyboardEvent(isUp ? 'keyup' : 'keydown', { 'key': '', keyCode: keycode }))
}

function setupKey(id, key, keyCode) {
    const element = document.getElementById(id);
    element.addEventListener("pointerdown", (event) => {
        console.log(`Pointer down for ${id}`); //Remove in release
        event.stopImmediatePropagation();
        event.preventDefault();
        sendEvent(false, keyCode);
    });

    element.addEventListener("pointerup", () => {
        sendEvent(true, keyCode);
    });
}

window.addEventListener('load', () => {
    create_fps_button(); // Remove in release 
});