@page "/admin/system/auditlog"

@using Moonlight.Shared.Components.Navigations
@using Moonlight.App.Repositories.LogEntries
@using Moonlight.App.Services
@using Moonlight.App.Database.Entities.LogsEntries
@using BlazorTable
@using Moonlight.App.Helpers
@using Moonlight.App.Models.Misc
@using Moonlight.Shared.Components.AuditLogEntrys

@inject AuditLogEntryRepository AuditLogEntryRepository

<OnlyAdmin>
    <AdminSystemNavigation Index="2"/>

    <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(DateTime))</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="Left" 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="Right" 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">
            <LazyLoader @ref="LazyLoader" Load="Load">
                @if (AuditLogEntries.Any())
                {
                    <div class="timeline">
                        @foreach (var entry in AuditLogEntries)
                        {
                            switch (entry.Type)
                            {
                                case AuditLogType.Login:
                                    <AuditLogEntryLogin Entry="entry"/>
                                    break;
                                case AuditLogType.Register:
                                    <AuditLogEntryRegister Entry="entry"/>
                                    break;
                                case AuditLogType.ChangePassword:
                                    <AuditLogEntryChangePassword Entry="entry"/>
                                    break;
                                case AuditLogType.ChangePowerState:
                                    <AuditLogEntryChangePowerState Entry="entry"/>
                                    break;
                            }
                        }
                    </div>
                }
                else
                {
                    <div class="alert alert-primary">
                        <TL>No records found for this day</TL>
                    </div>
                }
            </LazyLoader>
        </div>
    </div>
</OnlyAdmin>

@code
{
    private AuditLogEntry[] AuditLogEntries;
    private LazyLoader LazyLoader;
    private DateTime DateTime = DateTime.Today;

    private Task Load(LazyLoader arg)
    {
        AuditLogEntries = AuditLogEntryRepository
            .Get()
            .Where(x => x.CreatedAt.Date == DateTime.Date)
            .OrderByDescending(x => x.Id)
            .ToArray();

        return Task.CompletedTask;
    }

    private async Task Left()
    {
        DateTime = DateTime.AddDays(1);

        await LazyLoader.Reload();
    }

    private async Task Right()
    {
        DateTime = DateTime.AddDays(-1);

        await LazyLoader.Reload();
    }
}