@page "/admin/statistics"

@using Moonlight.App.Models.Misc
@using Moonlight.App.Services.Statistics
@using Moonlight.App.Database.Entities
@using ApexCharts
@using Moonlight.App.Helpers
@using Moonlight.App.Services
@using Moonlight.Shared.Components.Navigations

@inject StatisticsViewService StatisticsViewService
@inject SmartTranslateService SmartTranslateService

@attribute [PermissionRequired(nameof(Permissions.AdminStatistics))]

<AdminStatisticsNavigation Index="0" />

<div class="row mb-2">
    <div class="col-12 col-lg-6 col-xl">
        <div class="card card-body">
            <select class="form-select" @bind="TimeSpanBind">
                <option value="1">
                    <TL>Hour</TL>
                </option>
                <option value="24">
                    <TL>Day</TL>
                </option>
                <option value="744">
                    <TL>Month</TL>
                </option>
                <option value="8760">
                    <TL>Year</TL>
                </option>
                <option value="867240">
                    <TL>All time</TL>
                </option>
            </select>
        </div>
    </div>
</div>

<LazyLoader @ref="Loader" Load="Load">
    @foreach (var charts in Charts.Chunk(2))
    {
        <div class="row">
            @foreach (var chart in charts)
            {
                <div class="col-sm-6">
                    <div class="card mt-4">
                        <div class="card-header">
                            <div class="card-title">
                                <TL>@chart.Key</TL>
                            </div>
                        </div>
                        <div class="card-body">
                            <ApexChart TItem="StatisticsData"
                                       Options="GenerateOptions()"
                                       OnRendered="OnChartRendered">
                                <ApexPointSeries TItem="StatisticsData"
                                                 Items="chart.Value"
                                                 SeriesType="SeriesType.Area"
                                                 Name=""
                                                 ShowDataLabels="false"
                                                 XValue="@(e => Formatter.FormatDate(e.Date))"
                                                 YValue="@(e => (decimal)Math.Round(e.Value))"/>
                            </ApexChart>
                        </div>
                    </div>
                </div>
            }
        </div>
    }
    <div class="row">
        <div class="col-sm-6">
            <div class="card mt-4">
                <div class="card-header">
                    <div class="card-title">
                        <TL>Active users</TL>
                    </div>
                </div>
                <div class="card-body">
                    <span class="fs-2">@(ActiveUsers)</span>
                </div>
            </div>
        </div>
    </div>
</LazyLoader>

@code
{
    private StatisticsTimeSpan StatisticsTimeSpan = StatisticsTimeSpan.Day;
    private LazyLoader Loader;

    private Dictionary<string, StatisticsData[]> Charts = new();
    private int ActiveUsers = 0;

    private int TimeSpanBind
    {
        get => (int)StatisticsTimeSpan;
        set
        {
            StatisticsTimeSpan = (StatisticsTimeSpan)value;
            Task.Run(async () => await Loader.Reload());
        }
    }

    private Task Load(LazyLoader loader)
    {
        Charts.Clear();

        Charts.Add(
            SmartTranslateService.Translate("Servers"),
            AvgHelper.Calculate(
                StatisticsViewService.GetData("serversCount", StatisticsTimeSpan)
                )
            );

        Charts.Add(
            SmartTranslateService.Translate("Users"),
            AvgHelper.Calculate(
                StatisticsViewService.GetData("usersCount", StatisticsTimeSpan)
                )
            );

        Charts.Add(
            SmartTranslateService.Translate("Domains"),
            AvgHelper.Calculate(
                StatisticsViewService.GetData("domainsCount", StatisticsTimeSpan)
                )
            );

        Charts.Add(
            SmartTranslateService.Translate("Databases"),
            AvgHelper.Calculate(
                StatisticsViewService.GetData("databasesCount", StatisticsTimeSpan)
                )
            );

        Charts.Add(
            SmartTranslateService.Translate("Webspaces"),
            AvgHelper.Calculate(
                StatisticsViewService.GetData("webspacesCount", StatisticsTimeSpan)
                )
            );

        Charts.Add(
            SmartTranslateService.Translate("Sessions"),
            AvgHelper.Calculate(
                StatisticsViewService.GetData("sessionsCount", StatisticsTimeSpan)
                )
            );

        ActiveUsers = StatisticsViewService.GetActiveUsers(StatisticsTimeSpan);

        return Task.CompletedTask;
    }

    private ApexChartOptions<StatisticsData> GenerateOptions()
    {
        return new()
        {
            Legend = new Legend()
            {
                Show = false
            },
            DataLabels = new DataLabels()
            {
                Enabled = false
            },
            Xaxis = new XAxis()
            {
                Labels = new XAxisLabels()
                {
                    Show = false
                }
            },
            Chart = new Chart()
            {
                RedrawOnParentResize = true,
                Toolbar = new Toolbar()
                {
                    Show = false
                },
                Height = 300
            }
        };
    }

    private Task OnChartRendered()
    {
        return Task.CompletedTask;
    }
}