<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
	<style type="text/css">
		:root {
			--url: url("");
		}

		html,
		body {
			background-color: #000000;
			width: 100%;
			height: 100%;
			margin: 0;
			padding: 0;
			overflow: hidden;
			font-family: Consolas, 'Courier New', monospace;
			color: #ffffff;
		}

		.margin:first-child {
			background-image: var(--url);
			background-size: cover;
			background-repeat: no-repeat;
			background-attachment: fixed;
			background-color: #000000;
		}

		.lines-content:nth-of-type(1),
		.monaco-editor-background:nth-of-type(1) {
			background-image: var(--url);
			background-size: cover;
			background-repeat: no-repeat;
			background-attachment: fixed;
			background-color: #000000;
		}
	</style>
	<meta charset="utf-8" />
	<title></title>
</head>

<body>
	<div id="container" style="width:100%;height:100%;"></div>
	<script src="vs/loader.js"></script>
	<script type="text/javascript">
		require.config({
			paths: {
				'vs': 'vs',
			}
		});

		// API
		var GetText;
		var SetText;
		var SetTheme;
		var SetScroll;
		var ShowErr;
		var Refresh;

		// Enablers
		var SwitchMinimap;
		var SwitchReadonly;
		var SwitchRenderWhitespace;
		var SwitchLinks;
		var SwitchLineHeight;
		var SwitchFontSize;
		var SwitchFolding;
		var SwitchAutoIndent;
		var SwitchFontFamily;
		var SwitchFontLigatures;
		var AddIntellisense;

		// Variables
		var editor;
		var Proposals = [];

		require(['vs/editor/editor.main'], function () {
			function getDependencyProposals() {
				let newProposals = [];
				for (let index = 0; index < Proposals.length; index++) {
					const element = Proposals[index];
					let newElement = {}
					for (const key in element) {
						if (key == "__children__") {
							continue;
						};
						newElement[key] = element[key];
					}
					newProposals.push(newElement)
				}
				return newProposals;
			}

			// SetText function definition goes here, after dependencies are set
        SetText = function (x) {
            try {
                editor.setValue(JSON.parse(`${String(x)}`)); // Try parsing if possible
            } catch (e) {
                editor.setValue(String(x)); // If parsing fails, just set as string
            }
        };

			monaco.editor.defineTheme('net-theme-light', {
				base: 'vs',
				inherit: true,
				rules: [
					{ token: 'global', foreground: '000000' },
					{ token: 'keyword', foreground: '569CD6' },
					{ token: 'comment', foreground: '6A9955' },
					{ token: 'number', foreground: 'B5CEA8' },
					{ token: 'string', foreground: 'ff8c3a' },
				]
			});

			monaco.editor.defineTheme('net-theme-dark', {
				base: 'vs-dark',
				inherit: true,
				rules: [
					{ token: 'global', foreground: 'FFFFFF', fontStyle: "bold" },
					{ token: 'keyword', foreground: '569CD6', fontStyle: "bold" },
					{ token: 'comment', foreground: '6A9955' },
					{ token: 'number', foreground: 'B5CEA8' },
					{ token: 'string', foreground: 'CE9178' },
				]
			});

			monaco.editor.defineTheme('VortexDark', {
				base: 'vs-dark',
				inherit: true,
				rules: [
					{ token: '', foreground: '#c0c0c0' },
					{ token: 'variable.language.self', foreground: '#569CD6' },
					{ token: 'variable.parameter.variadic', foreground: '#569CD6' },
					{ token: 'variable.parameter.function', foreground: '#569CD6' },
					{ token: 'variable.other.constant', foreground: '#569CD6' },
					{ token: 'variable.property', foreground: '#569CD6' },
					{ token: 'variable.object.property', foreground: '#569CD6' },

					{ token: 'keyword', foreground: '#569CD6' },
					{ token: 'keyword.local', foreground: '#569CD6', fontStyle: 'bold' },
					{ token: 'keyword.operator', foreground: '#D4D4D4' },
					{ token: 'keyword.operator.type.annotation', foreground: '#569CD6' },
					{ token: 'keyword.operator.typedef.annotation', foreground: '#569CD6' },
					{ token: 'keyword.control.export', foreground: '#569CD6', fontStyle: 'italic' },

					{ token: 'operator', foreground: '#569cd6' },
					{ token: 'operator.type', foreground: '#569cd6' },
					{ token: 'operator.special', foreground: '#569cd6' },

					{ token: 'entity.name.type.alias', foreground: '#4EC9B0' },
					{ token: 'entity.name.function', foreground: '#DCDCAA', fontStyle: 'bold' },

					{ token: 'global', foreground: '#569cd6' },

					{ token: 'storage.type', foreground: '#569CD6' },

					{ token: 'comment', foreground: '#6A9955' },
					{ token: 'comment.highlight.title', foreground: '#6A9955', fontStyle: 'italic' },
					{ token: 'comment.highlight.name', foreground: '#6A9955', fontStyle: 'italic' },
					{ token: 'comment.delimiter.modifier', foreground: '#6A9955', fontStyle: 'italic' },
					{ token: 'comment.highlight.modifier', foreground: '#6A9955', fontStyle: 'italic' },
					{ token: 'comment.highlight.descriptor', foreground: '#6A9955', fontStyle: 'italic' },

					{ token: 'delimiter.longstring', foreground: '#89DDFF' },
					{ token: 'delimiter.bracket', foreground: '#ffc93d' },
					{ token: 'delimiter.array', foreground: '#ffc93d' },
					{ token: 'delimiter.parenthesis', foreground: '#ffc93d' },
					{ token: 'delimiter', foreground: '#a6afbd' },

					{ token: 'string', foreground: '#CE9178' },
					{ token: 'longstring', foreground: '#CE9178' },
					{ token: 'string.delimeter', foreground: '#CE9178' },
					{ token: 'string.escape', foreground: '#CE9178' },

					{ token: 'punctuation.separator.arguments', foreground: '#CE9178' },
					{ token: 'punctuation.separator.parameter', foreground: '#CE9178' },
					{ token: 'punctuation.separator.table', foreground: '#CE9178' },
					{ token: 'punctuation.definition.block', foreground: '#CE9178' },
					{ token: 'punctuation.definition.parameters', foreground: '#CE9178' },
					{ token: 'punctuation.definition.typeparameters', foreground: '#CE9178' },

					{ token: 'constant.language', foreground: '#569CD6' },
					{ token: 'number', foreground: '#B5CEA8' },
					{ token: 'constants', foreground: '#569CD6', fontStyle: 'bold' },

					{ token: 'support.function', foreground: '#DCDCAA', fontStyle: 'bold' },
					{ token: 'support.function.library', foreground: '#9CDCFE' },
					{ token: 'support.type', foreground: '#4EC9B0' },
				],
				colors: {
					'editor.background': '#000000',
					'editorLineNumber.foreground': '#444444',
					'editorLineNumber.activeForeground': '#AAAAAA',
					'editorIndentGuide.background': '#333333',
					'editorSuggestWidget.background': "#1f2228",
					'editorSuggestWidget.border': "#000000",
					'editorSuggestWidget.foreground': "#D5D5D5",
					'editorSuggestWidget.selectedBackground': "#2b2b2b",
					'editorSuggestWidget.highlightForeground': "#18A0FB",
					'textCodeBlock.background': '#111111',
				}
			});

			require(['vs/basic-languages/monaco.contribution'], function () {
				monaco.languages.registerCompletionItemProvider('lua', {
					provideCompletionItems: function (model, position) {
						return {
							suggestions: getDependencyProposals(),
						};
					},
					triggerCharacters: ['.', ':', '"'],
				});

				editor = monaco.editor.create(document.getElementById('container'), {
					value: '',
					language: 'lua',
					theme: "VortexDark",
					fontSize: "14px",
					fontFamily: "cascadia mono",
					folding: true,
					dragAndDrop: false,
					links: true,
					minimap: {
						enabled: false,
					},
					showFoldingControls: "always",
					smoothScrolling: true,
					stopRenderingLineAfter: 6500,
					cursorBlinking: "smooth",
					cursorSmoothCaretAnimation: true,
					foldingHighlight: false,
					fontLigatures: true,
					formatOnPaste: true,
					showDeprecated: true,
					suggest: {
						snippetsPreventQuickSuggestions: false,
					},
					padding: {
						top: 10,
					}
				});

				try {
					if (CefSharp) {
					editor.getModel().onDidChangeContent(function(e) {
						if (!e.isFlush) {
							CefSharp.PostMessage('keydown')
						}
					})
					
					CefSharp.PostMessage('init')
					
					setInterval(() => {
						CefSharp.PostMessage('save_tabs')
					}, 5000)
				} } catch { }

				editor.getModel().updateOptions({ insertSpaces: false })
			});

			window.onresize = function () {
				editor.layout();
			}

			Cut = function() {
				editor.focus();
				document.execCommand('cut');
			}

			Copy = function() {
				editor.focus();
				document.execCommand('copy');
			}

			Paste = function() {
				editor.focus();
				document.execCommand('paste');
			}

			Undo = function() {
				editor.focus();
				document.execCommand('undo');
			}

			Redo = function() {
				editor.focus();
				document.execCommand('redo');
			}

			Delete = function() {
				editor.focus();
				document.execCommand('delete');
			}

			SelectAll = function() {
				editor.focus();
				document.execCommand('selectAll');
			}

			GetText = function () {
				return String(editor.getValue());
			}

			SetText = function (x) {
				try {
					editor.setValue(JSON.parse(`${String(x)}`));
				} catch (e) {
					editor.setValue(String(x))
				}
			}

			GetProposals = function () {
				return Proposals;
			}


			OnDidChangeContent = function (callback) {
				return editor.onDidChangeModelContent((event) => {
					callback(editor.getValue());
				});
			}

			OnDidChangeCursorPosition = function (callback) {
				return editor.onDidChangeCursorPosition((event) => {
					callback(event.position);
				});
			}

			SetTheme = function (themeName) {
				if (themeName == 'krnlDark') {
					monaco.editor.setTheme('krnlDark')
				}
				if (themeName == "Dark") {
					monaco.editor.setTheme("net-theme-dark");
				}
				if (themeName == "Light") {
					monaco.editor.setTheme("net-theme-light");
				}
			}

			SwitchMinimap = function (flag) {
    if (editor) {
        editor.updateOptions({
            minimap: {
                enabled: flag,
            }
        });
    }
};



			SwitchReadonly = function (flag) {
				editor.updateOptions({
					readOnly: flag,
				});
			}

			SwitchRenderWhitespace = function (op) {
				editor.updateOptions({
					renderWhitespace: op,
				});
			}

			SwitchLinks = function (flag) {
				editor.updateOptions({
					links: flag,
				});
			}

			SwitchLineHeight = function (num) {
				editor.updateOptions({
					lineHeight: num,
				});
			}

			SwitchFontSize = function (num) {
				editor.updateOptions({
					fontSize: num,
				});
			}

			SwitchFolding = function (flag) {
				editor.updateOptions({
					folding: flag,
				});
			}

			SwitchAutoIndent = function (flag) {
				editor.updateOptions({
					autoIndent: flag,
				});
			}

			SwitchFontFamily = function (name) {
				editor.updateOptions({
					fontFamily: name,
				});
			}

			SwitchFontLigatures = function (flag) {
				editor.updateOptions({
					fontLigatures: flag,
				});
			}

			ShowErr = function (line, column, endline, endcolumn, errMessage) {
				editor.revealPositionInCenter({ lineNumber: line, column: column });
				editor.deltaDecorations([], [
					{
						range: new monaco.Range(line, column, endline, endcolumn),
						options: {
							inlineClassName: 'squiggly-error',
							hoverMessage: {
								value: errMessage,
							}
						},
					},
				]);
			}

			AddSnippet = function (kindName, snippetName, data) {
				let snippet = {
					insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
					kind: monaco.languages.CompletionItemKind[kindName],
				};
				for (const key in data) {
					let value = data[key];
					if (key == "insertText" && typeof value == "object") {
						value = value.join("\n");
					};
					snippet[key] = value;
				}
				if (!snippet.label) {
					snippet.label = snippetName
				}
				if (!snippet.insertText) {
					snippet.insertText = snippetName
				}
				Proposals.push(snippet);
			}

			AddRawSnippet = function (data) {
				Proposals.push(data);
			}

			SetScroll = function (line) {
				editor.revealLineInCenter({ lineNumber: line });
			}

			Refresh = function () {
				var text = getText();
				setText("");
				editor.trigger('keyboard', 'type', { text: text });
			}

		});
	</script>
</body>

</html>