<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nebula</title>
    <style>
        html, body {
            margin: 0;
            padding: 0;
            height: 100%;
            width: 100%;
            background-color: #111;
            color: #fff;
            font-family: Arial, sans-serif;
        }

        /* Sidebar */
        #sidebar {
            width: 70px;
            height: 150px;
            background: #1a1a1a;
            border: 0.1px solid #333;
            position: fixed;
            top: 50%;
            left: 10px;
            transform: translateY(-240%);
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            align-items: center;
            border-radius: 7px;
            z-index: 10;
        }

        /* Buttons */
        .sidebar-icon {
            width: 40px;
            height: 40px;
            background-color: #1a1a1a;
            border-radius: 5px;
            cursor: pointer;
            display: flex;
            justify-content: center;
            align-items: center;
            overflow: hidden;
            transition: background-color 0.3s ease, transform 0.3s ease, filter 0.3s ease;
        }

        .sidebar-icon img {
            width: 70%;
            height: 70%;
            object-fit: contain;
            filter: brightness(0.8);
            transition: filter 0.3s ease;
        }

        .sidebar-icon:hover {
            background-color: #d9534f;
            transform: scale(1.1);
        }

        .sidebar-icon:hover img {
            filter: brightness(1);
        }

        /* Main Content */
        #main {
            margin-left: 80px;
            padding: 0;
            height: 100%;
            width: calc(100% - 80px);
            display: flex;
            flex-direction: column;
        }

        #header {
            font-size: 1.5em;
            font-weight: bold;
            text-align: center;
            padding: 20px 10px 5px;
            margin: 0;
        }

        /* Outer Wrapper for Outline */
        #editor-outline {
            border: 2px solid #444;
            border-radius: 10px;
            padding: 7px;
            margin: 10px auto;
            width: 85%;
            max-width: 1200px;
            height: 90%;
            display: flex;
            flex-direction: column;
            box-sizing: border-box;
        }

        /* Connected Bar */
        #connected-bar {
            background-color: #1e1e1e;
            color: red;
            font-weight: bold;
            border-radius: 8px 8px 0 0;
            padding: 8px 15px;
            display: flex;
            align-items: center;
            gap: 8px;
            margin-bottom: 7px;
        }

        #connected-bar::before {
            content: "●";
            color: red;
            font-size: 1.2em;
        }

        /* Editor Wrapper */
        #editor-wrapper {
            flex-grow: 1;
            border-radius: 0 0 8px 8px;
            overflow: hidden;
            background-color: #1e1e1e;
        }

        /* Editor Container */
        #container {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <!-- Sidebar -->
    <div id="sidebar">
        <!-- Inject Button -->
        <div class="sidebar-icon" onclick="injectScript()">
            <img src="image.png" alt="Inject">
        </div>
        <!-- Execute Button -->
        <div class="sidebar-icon" onclick="executeScriptWithEditorContent()">
            <img src="image1.png" alt="Execute Script">
        </div>
    </div>

    <!-- Main Content -->
    <div id="main">
        <div id="header">Nebula</div>
        <div id="editor-outline">
            <!-- Connected Bar -->
            <div id="connected-bar" id="connection-status">Not Connected</div>
            <!-- Editor Wrapper -->
            <div id="editor-wrapper">
                <div id="container"></div>
            </div>
        </div>
    </div>

    <!-- Monaco Editor -->
    <script src="http://localhost:8080/vs/loader.js"></script>
    <script>
        let editor;

        document.addEventListener("DOMContentLoaded", function () {
            require.config({ paths: { 'vs': 'http://localhost:8080/vs' } });
            require(["vs/editor/editor.main"], function () {
                editor = monaco.editor.create(document.getElementById("container"), {
                    value: 'print("--Hello World")\n',
                    language: 'lua',
                    theme: 'vs-dark',
                    minimap: { enabled: true },
                    smoothScrolling: true
                });
            });
        });

        function injectScript() {
            fetch('http://localhost:8080/log', {
                method: 'POST',
                headers: { 'Content-Type': 'text/plain' },
                body: 'Injecting'
            })
            .then(response => response.text())
            .then(data => {
                console.log('Server Response:', data);

                const connectedBar = document.getElementById("connected-bar");

                // Simulate getting dynamic name from server
                const match = data.match(/: Attached Successfuly ! \n(.+)/);
                const name = match ? match[1] : "Unknown";

                connectedBar.textContent = `Connected to ${name} Client`;
                connectedBar.style.color = "green";
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }

        function executeScriptWithEditorContent() {
            const editorContent = editor.getValue();
            fetch('http://localhost:8080/execute-script', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ script: editorContent })
            })
            .then(response => response.text())
            .then(data => console.log('Script Execution Response:', data))
            .catch(error => console.error('Error:', error));
        }
    </script>
</body>
</html>
