<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Script Maker Develop by SzXCo</title>
    <style>
        body {
            background-color: #2d2d2d;
            color: #ffffff;
            font-family: 'Helvetica', sans-serif;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #404040;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        }

        h1, h2, h3 {
            text-align: center;
        }

        label {
            display: block;
            margin: 10px 0 5px;
        }

        input[type="text"], input[type="number"], textarea {
            width: calc(100% - 20px);
            padding: 10px;
            margin-bottom: 20px;
            border: none;
            border-radius: 4px;
            background-color: #2d2d2d;
            color: #ffffff;
        }

        button {
            display: block;
            width: 100%;
            padding: 10px;
            margin-top: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background-color: #45a049;
        }

        .hidden {
            display: none;
        }
    </style>
</head>
<body>

<div class="container" id="license-screen">
    <h1>Script Maker</h1>
    <p>Develop by SzXCo</p>
    <label for="license-key">license key</label>
    <input type="text" id="license-key" placeholder="Enter your license key">
    <button onclick="verifyLicense()">decision</button>
</div>

<div class="container hidden" id="main-menu">
    <h2>Main Menu</h2>
    <button onclick="setMenuCount()">メニュー数を設定</button>
    <button onclick="exitProgram()">終了</button>
</div>

<div class="container hidden" id="menu-count-screen">
    <h2>メニュー数を入力してください</h2>
    <input type="number" id="menu-count" placeholder="Enter menu count">
    <button onclick="saveMenuCount()">確認</button>
</div>

<div class="container hidden" id="menu-name-screen">
    <h2>各メニュー名を入力してください</h2>
    <div id="menu-name-fields"></div>
    <button onclick="saveMenuNames()">次へ</button>
</div>

<div class="container hidden" id="menu-settings-screen">
    <h2>各メニューの数値と変更地を設定してください</h2>
    <p>現在byte値のみ対応<br>例(h 01 01 01 01)</p>
    <div id="menu-settings-fields"></div>
    <button onclick="displayGeneratedScript()">完了</button>
</div>

<div class="container hidden" id="script-output-screen">
    <h2>生成されたスクリプト</h2>
    <textarea id="script-output" rows="20" readonly></textarea>
    <button onclick="closeScriptOutput()">閉じる</button>
</div>

<script>
    const LICENSE_KEY = "SzXCo";
    let menuCount = 0;
    let menuNames = [];
    let menuSettings = {};

    function verifyLicense() {
        const key = document.getElementById('license-key').value;
        if (key !== LICENSE_KEY) {
            alert("無効なライセンスキーです。プログラムを終了します。");
            window.close();
            return;
        }
        alert("ライセンスキーが認証されました。");
        switchScreen('main-menu');
    }

    function setMenuCount() {
        switchScreen('menu-count-screen');
    }

    function saveMenuCount() {
        const count = parseInt(document.getElementById('menu-count').value, 10);
        if (isNaN(count) || count <= 0) {
            alert("メニュー数は1以上でなければなりません。");
            return;
        }
        menuCount = count;
        createMenuNameFields();
    }

    function createMenuNameFields() {
        const container = document.getElementById('menu-name-fields');
        container.innerHTML = '';
        for (let i = 1; i <= menuCount; i++) {
            const label = document.createElement('label');
            label.textContent = `メニュー ${i} の名前:`;
            const input = document.createElement('input');
            input.type = 'text';
            input.id = `menu-name-${i}`;
            container.appendChild(label);
            container.appendChild(input);
        }
        switchScreen('menu-name-screen');
    }

    function saveMenuNames() {
        menuNames = [];
        for (let i = 1; i <= menuCount; i++) {
            const name = document.getElementById(`menu-name-${i}`).value;
            if (!name.trim()) {
                alert("全てのメニュー名を入力してください。");
                return;
            }
            menuNames.push(name.trim());
        }
        createMenuSettingsFields();
    }

    function createMenuSettingsFields() {
        const container = document.getElementById('menu-settings-fields');
        container.innerHTML = '';
        menuSettings = {};
        menuNames.forEach((name, index) => {
            const frame = document.createElement('div');

            const numLabel = document.createElement('label');
            numLabel.textContent = `${name} の数値:`;
            const numInput = document.createElement('input');
            numInput.type = 'text';
            numInput.id = `menu-num-${index}`;

            const changeLabel = document.createElement('label');
            changeLabel.textContent = "変更地:";
            const changeInput = document.createElement('input');
            changeInput.type = 'text';
            changeInput.id = `menu-change-${index}`;

            frame.appendChild(numLabel);
            frame.appendChild(numInput);
            frame.appendChild(changeLabel);
            frame.appendChild(changeInput);

            container.appendChild(frame);
            menuSettings[name] = { numInput, changeInput };
        });
        switchScreen('menu-settings-screen');
    }

    function displayGeneratedScript() {
        let script = "gg.setVisible(true)\n";
        script += "local on = \" 《ON》\"\n";
        script += "local off = \"《OFF》\"\n\n";

        for (let i = 0; i < menuCount; i++) {
            script += `local change${60 + i} = off\n`;
        }

        script += "\nfunction main()\n";
        script += "menu = gg.choice({\n";

        menuNames.forEach((name, index) => {
            script += `\t\"${name}丨使用状況\" .. change${60 + index},\n`;
        });

        script += "\t\"終了\",\n";
        script += "}, nil, os.date(\"made by SzXCo Ver.11.1.0\"))\n";
        script += "if menu == nil then\n\treturn\nend\n";

        menuNames.forEach((_, index) => {
            script += `if menu == ${index + 1} then\n`;
            script += `\t${generateFunctionName(index + 1)}()\n`;
            script += "end\n";
        });

        script += `if menu == ${menuCount + 1} then\n\tEXIT()\nend\n`;
        script += "CODM = -1\nend\n\n";

        menuNames.forEach((name, index) => {
            const num = menuSettings[name].numInput.value;
            const change = menuSettings[name].changeInput.value;
            const changeVar = `change${60 + index}`;
            const functionName = generateFunctionName(index + 1);

            script += `function ${functionName}()\n`;
            script += `if ${changeVar} == on then\n`;
            script += `${changeVar} = off\n`;
            script += `gg.searchNumber(\"${num}\", gg.TYPE_BYTE, false, gg.SIGN_EQUAL, 0, -1, 0)\n`;
            script += `revert = gg.getResults(100, nil, nil, nil, nil, nil, nil, nil, nil)\n`;
            script += `gg.editAll(\"${change}\", gg.TYPE_BYTE)\n`;
            script += "gg.clearResults()\n";
            script += "gg.toast(\"success\")\n";
            script += "else\n";
            script += `${changeVar} = on\n`;
            script += `gg.searchNumber(\"${num}\", gg.TYPE_BYTE, false, gg.SIGN_EQUAL, 0, -1)\n`;
            script += `revert = gg.getResults(100, nil, nil, nil, nil, nil, nil, nil, nil)\n`;
            script += `gg.editAll(\"${change}\", gg.TYPE_BYTE)\n`;
            script += "gg.clearResults()\n";
            script += "gg.toast(\"success\")\n";
            script += "end\n";
            script += "end\n\n";
        });

        script += "function EXIT()\n";
        script += "\tgg.alert(\"スクリプトが正常に終了しました\")\n";
        script += "print(\"made by SzXCo 作成者に感謝しよう\")\n";
        script += "gg.clearResults()\n";
        script += "os.exit()\n";
        script += "end\n\n";

        script += "while true do\n";
        script += "if gg.isVisible() then\n";
        script += "gg.setVisible(false)\n";
        script += "main()\n";
        script += "end\n";
        script += "gg.sleep(10)\n";
        script += "end";

        document.getElementById('script-output').value = script;
        switchScreen('script-output-screen');
    }

    function closeScriptOutput() {
        switchScreen('main-menu');
    }

    function switchScreen(screenId) {
        document.querySelectorAll('.container').forEach(container => {
            container.classList.add('hidden');
        });
        document.getElementById(screenId).classList.remove('hidden');
    }

    function generateFunctionName(index) {
        return String.fromCharCode(96 + index);
    }

    function exitProgram() {
        alert("プログラムを終了します。");
        window.close();
    }
</script>

</body>
</html>
