<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Discord Bot Dashboard</title>
    <style>
        body {
            background: #0d0d0d; /* Dark background */
            color: white;
            font-family: Arial, sans-serif;
            padding: 20px;
            margin: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: flex-start;
        }

        h1, h2 {
            color: #fff;
            margin-bottom: 10px;
            text-align: center;
        }

        .status-container {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-bottom: 20px;
        }

        #status {
            font-size: 18px;
            margin-top: 10px;
            padding: 10px;
            border-radius: 5px;
            display: inline-block;
            width: 200px;
            text-align: center;
            background: #000; /* Pure black background */
            border: 0.1px solid #3c3c3c; /* Border color */
            transition: box-shadow 0.3s ease; /* Smooth transition for glowing effect */
        }

        .running {
            background: #000; /* Pure black background */
            border: 0.1px solid #3c3c3c; /* Border color */
            box-shadow: 0 0 8px 2px rgba(0, 255, 0, 0.6); /* Slight green glow */
        }

        .stopped {
            background: #000; /* Pure black background */
            border: 0.1px solid #3c3c3c; /* Border color */
            box-shadow: 0 0 8px 2px rgba(255, 0, 0, 0.6); /* Slight red glow */
        }

        .container {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            gap: 20px;
            width: 100%;
            margin-bottom: 20px;
        }

        #editor-container,
        #terminal-container {
            background: #000; /* Pure black background */
            border-radius: 10px;
            padding: 20px;
            box-sizing: border-box;
            overflow-y: auto;
            border: 0.1px solid #3c3c3c; /* Border color */
        }

        #editor-container {
            flex: 1;
            min-width: 45%;
            height: 500px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }

        #terminal-container {
            flex: 1;
            min-width: 45%;
            height: 500px;
            color: #505050;
            white-space: pre-wrap;
            word-wrap: break-word;
            box-shadow: none; /* Removed the glowing effect */
        }

        .button-container {
            display: flex;
            justify-content: center;
            gap: 30px;
            margin: 20px 0;
        }

        button {
            padding: 12px 25px;
            font-size: 16px;
            font-weight: bold;
            border: 0.1px solid #3c3c3c; /* Border color */
            cursor: pointer;
            border-radius: 8px;
            width: 150px;
            background-color: #000; /* Pure black background */
            color: rgb(150, 150, 150); /* Button text color */
        }

        button:focus {
            outline: none;
        }

        textarea {
            background-color: #000; /* Pure black background */
            color: white;
            border: 1px solid #444;
            border-radius: 8px;
            padding: 15px;
            font-family: monospace;
            width: 100%;
            height: 100%;
            resize: none;
            box-sizing: border-box;
            display: block;
            overflow: auto;
        }

        .button-container {
            width: 100%;
            display: flex;
            justify-content: center;
            gap: 30px;
        }
    </style>
</head>
<body>
    <h1>Discord Bot Hosting Dashboard</h1>
    <div class="status-container">
        <div id="status" class="stopped">Bot is Stopped</div>
    </div>

    <div class="container">
        <div id="editor-container">
            <textarea id="editor-textarea" placeholder="Write your bot code here..."></textarea>
        </div>
        <div id="terminal-container">Waiting for bot to start...</div>
    </div>

    <div class="button-container">
        <button class="start-btn" onclick="runBot()">Start Bot</button>
        <button class="stop-btn" onclick="stopBot()">Stop Bot</button>
    </div>

    <script>
        // Load the saved code from localStorage when the page is loaded
        window.onload = function() {
            const savedCode = localStorage.getItem('bot_code');
            if (savedCode) {
                document.getElementById('editor-textarea').value = savedCode;
            }

            // Check if the bot is still running
            if (localStorage.getItem('bot_running') === 'true') {
                updateStatus(true);
                appendToTerminal("Bot is running.");
            } else {
                updateStatus(false);
                appendToTerminal("Bot is currently not running. Please start the bot.");
            }
        };

        // Save code to localStorage as the user types in the textarea
        document.getElementById('editor-textarea').addEventListener('input', function() {
            const code = this.value;
            localStorage.setItem('bot_code', code);
        });

        // Update the status of the bot (Running/Stopped)
        function updateStatus(running) {
            const status = document.getElementById('status');
            status.textContent = running ? 'Bot is Running' : 'Bot is Stopped';
            status.className = running ? 'running' : 'stopped';
        }

        // Append messages to the terminal container
        function appendToTerminal(message) {
            const terminal = document.getElementById("terminal-container");
            const timestamp = new Date().toLocaleTimeString();
            terminal.innerHTML += `[${timestamp}] ${message}\n`;
            terminal.scrollTop = terminal.scrollHeight;
        }

        // Start the bot
        window.runBot = function() {
            const script = document.getElementById('editor-textarea').value;
            fetch("/run-bot", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ code: script })
            })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                updateStatus(true);
                appendToTerminal("Bot is now running.");
                localStorage.setItem('bot_running', 'true');
            })
            .catch(error => alert("Error: " + error));
        };

        // Stop the bot
        window.stopBot = function() {
            fetch("/stop-bot", { method: "POST" })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                updateStatus(false);
                appendToTerminal("Bot has been stopped.");
                localStorage.setItem('bot_running', 'false');
            })
            .catch(error => alert("Error: " + error));
        };
    </script>
</body>
</html>
