@page "/"

@using Moonlight.App.Repositories.Servers
@using Microsoft.EntityFrameworkCore
@using Moonlight.App.Database.Entities
@using Moonlight.App.Helpers
@using Moonlight.App.Repositories
@using Moonlight.App.Repositories.Domains
@using Markdig
@using Moonlight.App.Services.Sessions

@inject ServerRepository ServerRepository
@inject Repository<WebSpace> WebSpaceRepository
@inject DomainRepository DomainRepository
@inject NewsEntryRepository NewsEntryRepository
@inject IdentityService IdentityService

<LazyLoader Load="Load">
@if (NewsEntries.Any())
{
    if (CurrentNewsIndex > NewsEntries.Count - 1)
        CurrentNewsIndex = 0;

    if (CurrentNewsIndex < 0)
        CurrentNewsIndex = NewsEntries.Count - 1;

    var currentEntry = NewsEntries[CurrentNewsIndex];

    <div class="mb-5">
        <div class="card">
            <div class="card-header card-header-stretch">
                <div class="card-title d-flex align-items-center">
                    <span class="me-3 lh-0">
                        <i class="bx bx-md bx-calendar"></i>
                    </span>
                    <h3 class="fw-bold m-0 text-gray-800">@(Formatter.FormatDateOnly(currentEntry.Date))</h3>
                </div>
                <div class="card-toolbar m-0">
                    <ul class="nav nav-tabs nav-line-tabs nav-stretch fs-6 border-0 fw-bold">
                        <li class="nav-item">
                            <button @onclick="() => ChangeNewsIndex(-1)" class="nav-link justify-content-center text-active-gray-800">
                                <i class="bx bx-md bx-left-arrow"></i>
                            </button>
                        </li>
                        <li class="nav-item">
                            <button @onclick="() => ChangeNewsIndex(1)" class="nav-link justify-content-center text-active-gray-800">
                                <i class="bx bx-md bx-right-arrow"></i>
                            </button>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="card-body">
                @{
                    var html = (MarkupString)Markdown.ToHtml(currentEntry.Markdown);
                }

                @(html)
            </div>
        </div>
    </div>
}

<div class="row mb-5">
    <div class="col-12 col-lg-6 col-xl">
        <a class="mt-4 card" href="/servers">
            <div class="card-body">
                <div class="row align-items-center gx-0">
                    <div class="col">
                        <h6 class="text-uppercase text-muted mb-2">
                            <TL>Servers</TL>
                        </h6>
                        <span class="h2 mb-0">
                            @(ServerCount)
                        </span>
                    </div>
                    <div class="col-auto">
                        <span class="h2 text-muted mb-0">
                            <i class="text-primary bx bx-server bx-lg"></i>
                        </span>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <div class="col-12 col-lg-6 col-xl">
        <a class="mt-4 card" href="/webspaces">
            <div class="card-body">
                <div class="row align-items-center gx-0">
                    <div class="col">
                        <h6 class="text-uppercase text-muted mb-2">
                            <TL>Webspaces</TL>
                        </h6>
                        <span class="h2 mb-0">
                            @(WebSpaceCount)
                        </span>
                    </div>
                    <div class="col-auto">
                        <span class="h2 text-muted mb-0">
                            <i class="text-primary bx bx-globe bx-lg"></i>
                        </span>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <div class="col-12 col-lg-6 col-xl">
        <a class="mt-4 card" href="/domains">
            <div class="card-body">
                <div class="row align-items-center gx-0">
                    <div class="col">
                        <h6 class="text-uppercase text-muted mb-2">
                            <TL>Domains</TL>
                        </h6>
                        <span class="h2 mb-0">
                            @(DomainCount)
                        </span>
                    </div>
                    <div class="col-auto">
                        <span class="h2 text-muted mb-">
                            <i class="text-primary bx bx-purchase-tag bx-lg"></i>
                        </span>
                    </div>
                </div>
            </div>
        </a>
    </div>
</div>

<div class="row">
    <div class="col">
        <div class="card mb-5">
            <div class="card-header card-header-stretch">
                <div class="card-title d-flex align-items-center">
                    <h3 class="fw-bold m-0 text-gray-800">
                        <TL>Create something new</TL>
                    </h3>
                </div>
            </div>
            <div class="card-body pt-3">
                <div class="flex-row">
                    <div class="d-flex align-items-center">
                        <div class="symbol symbol-50px me-3">
                            <i class="bx bx-md bx-server"></i>
                        </div>
                        <div class="d-flex justify-content-start flex-column">
                            <a href="/servers/create" class="text-gray-800 text-hover-primary mb-1 fs-5">
                                <TL>Create a gameserver</TL>
                            </a>
                            <span class="text-gray-400 fw-semibold d-block fs-6">
                                <TL>A new gameserver in just a few minutes</TL>
                            </span>
                        </div>
                    </div>
                    <div class="separator mb-2 mt-2"></div>
                    <div class="d-flex align-items-center">
                        <div class="symbol symbol-50px me-3">
                            <i class="bx bx-md bx-globe"></i>
                        </div>
                        <div class="d-flex justify-content-start flex-column">
                            <a href="/webspaces/create" class="text-gray-800 text-hover-primary mb-1 fs-5">
                                <TL>Create a webspace</TL>
                            </a>
                            <span class="text-gray-400 fw-semibold d-block fs-6">
                                <TL>Make your own websites with a webspace</TL>
                            </span>
                        </div>
                    </div>
                    <div class="separator mb-2 mt-2"></div>
                    <div class="d-flex align-items-center">
                        <div class="symbol symbol-50px me-3">
                            <i class="bx bx-md bx-purchase-tag"></i>
                        </div>
                        <div class="d-flex justify-content-start flex-column">
                            <a href="/domains/create" class="text-gray-800 text-hover-primary mb-1 fs-5">
                                <TL>Create a domain</TL>
                            </a>
                            <span class="text-gray-400 fw-semibold d-block fs-6">
                                <TL>Make your services accessible through your own domain</TL>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col">
        <div class="card mb-5">
            <div class="card-header card-header-stretch">
                <div class="card-title d-flex align-items-center">
                    <h3 class="fw-bold m-0 text-gray-800">
                        <TL>Manage your services</TL>
                    </h3>
                </div>
            </div>
            <div class="card-body pt-3">
                <div class="flex-row">
                    <div class="d-flex align-items-center">
                        <div class="symbol symbol-50px me-3">
                            <i class="bx bx-md bx-server"></i>
                        </div>
                        <div class="d-flex justify-content-start flex-column">
                            <a href="/servers" class="text-gray-800 text-hover-primary mb-1 fs-5">
                                <TL>Manage your gameservers</TL>
                            </a>
                            <span class="text-gray-400 fw-semibold d-block fs-6">
                                <TL>Adjust your gameservers</TL>
                            </span>
                        </div>
                    </div>
                    <div class="separator mb-2 mt-2"></div>
                    <div class="d-flex align-items-center">
                        <div class="symbol symbol-50px me-3">
                            <i class="bx bx-md bx-globe"></i>
                        </div>
                        <div class="d-flex justify-content-start flex-column">
                            <a href="/webspaces" class="text-gray-800 text-hover-primary mb-1 fs-5">
                                <TL>Manage your webspaces</TL>
                            </a>
                            <span class="text-gray-400 fw-semibold d-block fs-6">
                                <TL>Modify the content of your webspaces</TL>
                            </span>
                        </div>
                    </div>
                    <div class="separator mb-2 mt-2"></div>
                    <div class="d-flex align-items-center">
                        <div class="symbol symbol-50px me-3">
                            <i class="bx bx-md bx-purchase-tag"></i>
                        </div>
                        <div class="d-flex justify-content-start flex-column">
                            <a href="/domains" class="text-gray-800 text-hover-primary mb-1 fs-5">
                                <TL>Manage your domains</TL>
                            </a>
                            <span class="text-gray-400 fw-semibold d-block fs-6">
                                <TL>Add, edit and delete dns records</TL>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</LazyLoader>

@code
{
    private int ServerCount = 0;
    private int DomainCount = 0;
    private int WebSpaceCount = 0;

    private List<NewsEntry> NewsEntries;
    private int CurrentNewsIndex = 0;

    private Task Load(LazyLoader lazyLoader)
    {
        ServerCount = ServerRepository
            .Get()
            .Include(x => x.Owner)
            .Count(x => x.Owner.Id == IdentityService.User.Id);

        DomainCount = DomainRepository
            .Get()
            .Include(x => x.Owner)
            .Count(x => x.Owner.Id == IdentityService.User.Id);

        WebSpaceCount = WebSpaceRepository
            .Get()
            .Include(x => x.Owner)
            .Count(x => x.Owner.Id == IdentityService.User.Id);

        NewsEntries = NewsEntryRepository.Get().ToList();

        return Task.CompletedTask;
    }

    private async Task ChangeNewsIndex(int i)
    {
        CurrentNewsIndex += i;
        await InvokeAsync(StateHasChanged);
    }
}