@model LANCommander.Models.EditKeysViewModel

@{
    ViewData["Title"] = "Edit Keys | " + Model.Game.Title;
}

<div class="container-xl">
    <!-- Page title -->
    <div class="page-header d-print-none">
        <div class="row align-items-center">
            <div class="col">
                <div class="page-pretitle">@Model.Game.Title</div>
                <h2 class="page-title">
                    Edit Keys
                </h2>
            </div>
        </div>
    </div>
</div>

<div class="page-body">
    <div class="container-xl">
        <div class="row row-cards">
            <div class="col-12">
                <form asp-action="Edit" class="card">
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>

                    <input type="hidden" asp-for="Keys" />
                    <input type="hidden" asp-for="Game.Id" />

                    <div id="KeyEditor" style="height: 100%; min-height: 600px;"></div>

                    <div class="card-footer">
                        <div class="d-flex">
                            <a asp-action="Details" asp-route-id="@Model.Game.Id" class="btn btn-ghost-primary">Cancel</a>
                            <button type="submit" class="btn btn-primary ms-auto">Save</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }

    <script src="~/lib/monaco-editor/min/vs/loader.js"></script>

    <script>
        require.config({ paths: { vs: '/lib/monaco-editor/min/vs' } });
        require(['vs/editor/editor.main'], function () {
            var editor = monaco.editor.create(document.getElementById('KeyEditor'), {
                value: $('#Keys').val(),
                readOnly: false,
                theme: 'vs-dark',
                automaticLayout: true
            });

            editor.onDidChangeModelContent(function (e) {
                $('#Keys').val(editor.getModel().getValue());
            });
        });

        document.addEventListener('keydown', e => {
            if (e.ctrlKey && e.key === 's') {
                e.preventDefault();
            
                $('form').submit();
            }
        });
    </script>
}
