@using Moonlight.App.Services
@using Moonlight.App.Database.Entities
@using Moonlight.App.Helpers
@using Moonlight.App.Helpers.Wings
@using Moonlight.App.Helpers.Wings.Enums
@using Moonlight.App.Plugin.UI
@using Moonlight.App.Plugin.UI.Servers
@using Moonlight.App.Services.Sessions
@using Moonlight.App.ApiClients.Wings

@inject SmartTranslateService TranslationService
@inject ServerService ServerService
@inject IdentityService IdentityService

<div class="align-items-center">
    <div class="row">
        <div class="card card-body me-6 pb-0">
            <div class="row">
                <div class="col-8">
                    <div class="d-flex align-items-center">
                        <div class="symbol symbol-circle me-5">
                            <div class="symbol-label bg-transparent text-primary border border-secondary border-dashed">
                                <i class="bx bx-server bx-md"></i>
                            </div>
                        </div>
                        <div class="d-flex flex-column">
                            <div class="mb-1 fs-4">@(CurrentServer.Name)</div>
                            <div class="text-muted fs-5">@(CurrentServer.Cpu <= 100 ? Math.Round(CurrentServer.Cpu / 100f, 2) + $" {TranslationService.Translate("Core")}" : Math.Round(CurrentServer.Cpu / 100f, 2) + $" {TranslationService.Translate("Cores")}") / @(Math.Round(CurrentServer.Memory / 1024f, 2)) GB @(TranslationService.Translate("Memory")) / @(Math.Round(CurrentServer.Disk / 1024f, 2)) GB @(TranslationService.Translate("Disk")) / @(CurrentServer.Node.Name) - <span class="text-muted">@(CurrentServer.Image.Name)</span></div>
                        </div>
                    </div>
                </div>
                <div class="col-4 d-flex flex-column flex-end mb-1">
                    <div class="btn-group btn-group-md">
                        <button class="nav-link btn btn-md btn-success fw-bold px-4 me-1 @(Console.ServerState == ServerState.Offline ? "" : "disabled")" aria-selected="true" role="tab" @onclick="Start">
                            <TL>Start</TL>
                        </button>
                        <button class="nav-link btn btn-md btn-primary fw-bold px-4 me-1 @(Console.ServerState == ServerState.Running ? "" : "disabled")" aria-selected="true" role="tab" @onclick="Restart">
                            <TL>Restart</TL>
                        </button>
                        @if (Console.ServerState == ServerState.Stopping)
                        {
                            <button class="nav-link btn btn-md btn-danger fw-bold px-4 me-1" aria-selected="true" role="tab" @onclick="Kill">
                                <TL>Kill</TL>
                            </button>
                        }
                        else
                        {
                            <button class="nav-link btn btn-md btn-danger fw-bold px-4 me-1 @(Console.ServerState == ServerState.Running || Console.ServerState == ServerState.Starting ? "" : "disabled")"
                                    aria-selected="true" role="tab" @onclick="Stop">
                                <TL>Stop</TL>
                            </button>
                        }
                    </div>
                </div>
            </div>
            <div class="row">
                <ul class="nav nav-stretch nav-line-tabs nav-line-tabs-2x border-transparent fs-5 fw-bold text-nowrap flex-nowrap hide-scrollbar" style="overflow-x: auto; overflow-y: hidden;">
                    @foreach (var tab in Context.Tabs)
                    {
                        <li class="nav-item mt-2 @(tab == Context.Tabs.First() ? "ms-5" : "")">
                            <a class="nav-link text-active-primary ms-0 me-10 py-5 @(Route == tab.Route ? "active" : "")" href="/server/@(CurrentServer.Uuid + tab.Route)">
                                <i class="bx bx-@(tab.Icon) bx-sm me-2"></i><TL>@(tab.Name)</TL>
                            </a>
                        </li>
                    }
                </ul>
            </div>
        </div>
        <div class="row mt-3">
            <div class="card card-body">
                <div class="row align-items-center">
                    <div class="col fs-5 text-nowrap py-2">
                        <span class="fw-bold"><TL>Shared IP</TL>:</span>
                        <span class="ms-1 text-muted @(IdentityService.User.StreamerMode ? "blur" : "")">@($"{CurrentServer.Node.Fqdn}:{CurrentServer.MainAllocation?.Port ?? 0}")</span>
                    </div>
                    <div class="col fs-5 py-2">
                        <span class="fw-bold"><TL>Server ID</TL>:</span>
                        <span class="ms-1 text-muted">
                            @if (IdentityService.User.Admin)
                            {
                                <a href="/admin/servers/view/@(CurrentServer.Id)">@(CurrentServer.Id)</a>
                            }
                            else
                            {
                                @(CurrentServer.Id)
                            }
                        </span>
                    </div>
                    <div class="col fs-5 py-2">
                        <span class="fw-bold"><TL>Status</TL>:</span>
                        <span class="ms-1 text-muted">
                            @switch (Console.ServerState)
                            {
                                case ServerState.Offline:
                                    <span class="text-danger">
                                        <TL>Offline</TL>
                                    </span>
                                    break;

                                case ServerState.Starting:
                                    <span class="text-warning">
                                        <TL>Starting</TL>
                                    </span>
                                    <span class="text-gray-700 pt-1 fw-semibold">(@(Formatter.FormatUptime(Console.Resource.Uptime)))</span>
                                    break;

                                case ServerState.Stopping:
                                    <span class="text-warning">
                                        <TL>Stopping</TL>
                                    </span>
                                    <span class="text-gray-700 pt-1 fw-semibold">(@(Formatter.FormatUptime(Console.Resource.Uptime)))</span>
                                    break;

                                case ServerState.Running:
                                    <span class="text-success">
                                        <TL>Online</TL>
                                    </span>
                                    <span class="text-gray-700 pt-1 fw-semibold">(@(Formatter.FormatUptime(Console.Resource.Uptime)))</span>
                                    break;
                            }
                        </span>
                    </div>
                    <div class="col fs-5 py-2">
                        <span class="fw-bold"><TL>Cpu</TL>:</span>
                        <span class="ms-1 text-muted">@(Math.Round(Console.Resource.CpuAbsolute / (CurrentServer.Cpu / 100f), 2))%</span>
                    </div>
                    <div class="col fs-5 py-2">
                        <span class="fw-bold"><TL>Memory</TL>:</span>
                        <span class="ms-1 text-muted">@(Formatter.FormatSize(Console.Resource.MemoryBytes)) / @Math.Round(CurrentServer.Memory / 1024f, 2) GB</span>
                    </div>
                    <div class="col fs-5 py-2">
                        <span class="fw-bold"><TL>Disk</TL>:</span>
                        <span class="ms-1 text-muted">@(Formatter.FormatSize(Console.Resource.DiskBytes)) / @(Math.Round(CurrentServer.Disk / 1024f, 2)) GB</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="mt-5 row">
            <div class="d-flex flex-column flex-md-row card card-body p-5">
                @*
<ul class="nav nav-tabs nav-pills flex-row border-0 flex-md-column fs-6 pe-5 mb-5">
                    @foreach (var tab in Context.Tabs)
                    {
                        <li class="nav-item w-100 me-0 mb-md-2">
                            <a href="/server/@(CurrentServer.Uuid + tab.Route)" class="nav-link w-100 btn btn-flex @(Route == tab.Route ? "active" : "") btn-active-light-primary">
                                <i class="bx @(tab.Icon) bx-sm me-2"></i>
                                <span class="d-flex flex-column align-items-start">
                                    <span class="fs-5">
                                        <TL>@(tab.Name)</TL>
                                    </span>
                                </span>
                            </a>
                        </li>
                    }
                </ul>
*@
                <div class="tab-content w-100">
                    <div class="tab-pane fade show active">
                        @ChildContent
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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

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

    [CascadingParameter]
    public ServerPageContext Context { get; set; }

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public string Route { get; set; } = "/";

    //TODO: NodeIpService which loads and caches raw ips for nodes (maybe)

    protected override void OnInitialized()
    {
        Console.OnServerStateUpdated += async (_, _) => { await InvokeAsync(StateHasChanged); };
        Console.OnResourceUpdated += async (_, _) => { await InvokeAsync(StateHasChanged); };
    }

    #region Power Actions

    private async Task Start()
    {
        await ServerService.SetPowerState(CurrentServer, PowerSignal.Start);
    }

    private async Task Stop()
    {
        await ServerService.SetPowerState(CurrentServer, PowerSignal.Stop);
    }

    private async Task Kill()
    {
        await ServerService.SetPowerState(CurrentServer, PowerSignal.Kill);
    }

    private async Task Restart()
    {
        await ServerService.SetPowerState(CurrentServer, PowerSignal.Restart);
    }

    #endregion
}