@using Moonlight.App.Helpers
@using Moonlight.App.Repositories
@using Moonlight.App.Services
@using Moonlight.App.Database.Entities
@using Moonlight.App.Helpers.Wings
@using Moonlight.App.Helpers.Wings.Data
@using Moonlight.App.Services.Interop
@using Moonlight.Shared.Components.Xterm

@implements IDisposable

@inject ClipboardService ClipboardService
@inject AlertService AlertService
@inject SmartTranslateService TranslationService

<div class="card card-body rounded bg-black p-3">
    <div class="d-flex flex-column">
        <Terminal @ref="Terminal" RunOnFirstRender="RunOnFirstRender"></Terminal>
        <div class="input-group">
            <script suppress-error="BL9992">
                                        function checkEnter(event) 
                                        {
                                            if (event.keyCode === 13) {
                                                event.preventDefault();
                                                document.getElementById("sendCmd").click();
                                            }
                                        }
                                    </script>
            <input @bind="@CommandInput" class="form-control rounded-start" onkeyup="checkEnter(event)" placeholder="@(TranslationService.Translate("Enter command"))"/>
            <button id="sendCmd" @onclick="SendCommand" class="input-group-text btn btn-primary">@(TranslationService.Translate("Execute"))</button>
        </div>
    </div>
</div>

@code
{
    [CascadingParameter]
    public WingsConsole Console { get; set; }

    [CascadingParameter]
    public Server CurrentServer { get; set; }

    private Terminal? Terminal;

    private string CommandInput = "";

    protected override void OnInitialized()
    {
        Console.OnMessage += OnMessage;
    }

    private async void OnMessage(object? sender, ConsoleMessage message)
    {
        if (Terminal != null)
        {
            if (message.IsInternal)
            {
                await Terminal.WriteLine("\x1b[38;5;16;48;5;135m\x1b[39m\x1b[1m Moonlight \x1b[0m " + message.Content + "\x1b[0m");
            }
            else
            {
                var s = message.Content;

                if (s.Contains("Moonlight Daemon") || s.Contains("Pterodactyl Daemon"))
                {
                    s = s.Replace("[39m", "\x1b[0m");
                    s = s.Replace("[33m", "[38;5;16;48;5;135m\x1b[39m");
                }

                s = s.Replace("[Pterodactyl Daemon]:", " Moonlight ");
                s = s.Replace("[Moonlight Daemon]:", " Moonlight ");
                s = s.Replace("Checking server disk space usage, this could take a few seconds...", TranslationService.Translate("Checking disk space"));
                s = s.Replace("Updating process configuration files...", TranslationService.Translate("Updating config files"));
                s = s.Replace("Ensuring file permissions are set correctly, this could take a few seconds...", TranslationService.Translate("Checking file permissions"));
                s = s.Replace("Pulling Docker container image, this could take a few minutes to complete...", TranslationService.Translate("Downloading server image"));
                s = s.Replace("Finished pulling Docker container image", TranslationService.Translate("Downloaded server image"));
                s = s.Replace("container@pterodactyl~", "server@moonlight >");

                await Terminal.WriteLine(s);
            }
        }
    }

    public void Dispose()
    {
        Console.OnMessage -= OnMessage;
        Terminal!.Dispose();
    }

    private async Task SendCommand()
    {
        CommandInput = CommandInput.Replace("\n", "");
        await Console.EnterCommand(CommandInput);
        CommandInput = "";
        StateHasChanged();
    }

    private void RunOnFirstRender()
    {
        lock (Console.Messages)
        {
            foreach (var message in Console.Messages)
            {
                OnMessage(null, message);
            }
        }
    }
}