// ==UserScript==
// 
@name                Modifying Input Var
// @namespace         Violentmonkey Scripts
// @match                 *://*/play/*/mygame/*
// @match                 
https://cogdemos.ink/play/*
// @match                 
https://dashingdon.com/play/*
// @match                 https://*/play/*/index.html
// @match                 *://*/*/mygame/index.html
// @grant                 none
// @version             1.0
// 
@author            -
// ==/UserScript==
(async function() {
    function wait(time) {
        //alert(`Waiting ${time} seconds.`)
        return new Promise((resolve) => { setTimeout(resolve, time*1000)})
    }
    await wait(5)
    let buttonContainer
    // Create overlay
    const overlay = document.createElement('div');
    overlay.style.visibility = "collapse";
    overlay.style.position = 'fixed';
    overlay.style.top = '0';
    overlay.style.left = '0';
    overlay.style.width = '100%';
    overlay.style.height = '100%';
    overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
    overlay.style.zIndex = '999';
    overlay.style.display = 'flex';
    overlay.style.alignItems = 'center';
    overlay.style.justifyContent = 'center';
    overlay.style.maxWidth = "100%"
    // Create popup container
    const popup = document.createElement('div');
    popup.style.padding = '20px';
    popup.style.backgroundColor = 'white';
    popup.style.border = '1px solid #ccc';
    popup.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.2)';
    popup.style.zIndex = '1000';
    popup.style.textAlign = 'center';
    popup.style.maxWidth = "100%"
    // Add message to popup
    const message = document.createElement('p');
    popup.appendChild(message);
    // Append popup to overlay
    overlay.appendChild(popup);
    // Append overlay to body
    document.body.appendChild(overlay);
    function createButtonCont() {
        // Create custom buttons container
        if (buttonContainer) {buttonContainer.remove();}
        buttonContainer = document.createElement('div');
        buttonContainer.style.display = 'block flex';
        buttonContainer.style.justifyContent = 'flex-start';
        buttonContainer.style.overflowX = "auto"    //Enable horizontal scrolling
        buttonContainer.style.maxWidth = "100%"
        buttonContainer.style.whiteSpace = "nowrap"
        popup.appendChild(buttonContainer);
    }
  window.inputVarName = function() {
        function confirmVar(key) {
            if (typeof stats[key] == "undefined") {
                alert(`Variable "${key}" is undefined.`)
                buttonContainer.remove();
                overlay.style.visibility = "collapse"
                return;
            }
            else if (typeof stats[key] == "number") {
                input.type = "number"; input.value = stats[key]; input.step = "any";
            }
            else if (typeof stats[key] == "boolean") {
                input.type = "checkbox"; input.id = "bool"; input.checked = stats[key];
                let label = document.createElement("label"); label.for = "bool"; label.textContent = stats[key];
            }
            else if (typeof stats[key] == "string") {
                input.value = stats[key];
            }
            input.focus();
            message.textContent = `Please enter the value you would like to set to the variable, "${key}". (It is currently set to "${stats[key]}")`
            confirm.onclick = function() {
                //document.removeEventListener("keydown", clickConf);
                if (input.type == "checkbox") {
                    stats[key] = input.checked;
                } else {
                    stats[key] = input.value;
                }
                overlay.style.visibility = "collapse"
            }
        }
        message.textContent = "Please enter the name/identifier of the game variable you would like to modify."
        overlay.style.visibility = "visible"
        createButtonCont();
        let input = document.createElement("input"); input.type = "text"; input.minlength = "1"; input.style.width = "70%"; input.value = ""; input.focus();
        buttonContainer.appendChild(input);
        let confirm = document.createElement("button"); confirm.textContent = "Confirm"; confirm.style.whiteSpace = "nowrap";
        confirm.onclick = function() {
            confirmVar(input.value)
        }
        buttonContainer.appendChild(confirm);
        let exitBtn = document.createElement("button"); exitBtn.textContent = "Return"; exitBtn.style.whiteSpace = "nowrap";
        exitBtn.onclick = function() {
            //document.removeEventListener("keydown", clickConf);
            overlay.style.visibility = "collapse"
        }
        buttonContainer.appendChild(exitBtn);
/*
        // Adding functionality to allow the enter character on a keyboard to imitate clicking the confirm button
        function clickConf(e) {
            if (e.keyCode == 13) {
                confirm.click();
            }
        }
        document.addEventListener("keydown", clickConf)
*/
  }
    let btns = document.getElementById("buttons");
    btns.innerHTML = btns.innerHTML + "<button id='cheatButton' class='spacedLink' onclick='inputVarName()'>Modify Input Var</button>";
})()