using Insight.Infrastructure.Entities;
using Insight.Infrastructure.Services;
using Insight.Web.Components.Containers;
using Insight.Web.Constants;
using Microsoft.AspNetCore.Components;
using MongoDB.Bson;
using MongoDB.Driver;
using MudBlazor;
using System.Text.RegularExpressions;
using SortDirection = MudBlazor.SortDirection;

namespace Insight.Web.Pages.Management.Accounts;

[Route(Navigation.Management.Accounts.Index)]
public partial class Index
{
    [Inject] private IdentityService IdentityService { get; init; } = default!;
    [Inject] private AccountService AccountService { get; init; } = default!;
    [Inject] private ISnackbar Snackbar { get; init; } = default!;

    private TableContainer<InsightUser>? _container;
    private string? _search;
    private InsightUser? _selected;

    private readonly string _title = "Accounts｜Insight";
    private readonly List<BreadcrumbItem> _breadcrumbs = new()
    {
        new BreadcrumbItem(nameof(Navigation.Home), href: Navigation.Home),
        new BreadcrumbItem(nameof(Navigation.Management.Accounts), href: Navigation.Management.Accounts.Index, disabled: true)
    };

    private async Task<TableData<InsightUser>> LoadDataAsync(TableState state)
    {
        try
        {
            var filter = Builders<InsightUser>.Filter.Empty;

            if (string.IsNullOrWhiteSpace(_search) is false)
            {
                var regex = new BsonRegularExpression(new Regex(_search, RegexOptions.IgnoreCase));
                filter &= Builders<InsightUser>.Filter.Regex(x => x.Email, regex) |
                    Builders<InsightUser>.Filter.Regex(x => x.TwoFactorEnabled, regex) |
                    Builders<InsightUser>.Filter.Regex(x => x.LockoutEnabled, regex);
            }

            var sort = state.SortDirection switch
            {
                SortDirection.Ascending => state.SortLabel switch
                {
                    "Email" => Builders<InsightUser>.Sort.Ascending(p => p.Email),
                    "2FA" => Builders<InsightUser>.Sort.Ascending(p => p.TwoFactorEnabled),
                    "Lockout" => Builders<InsightUser>.Sort.Ascending(p => p.LockoutEnabled),
                    _ => null
                },
                SortDirection.Descending => state.SortLabel switch
                {
                    "Email" => Builders<InsightUser>.Sort.Descending(p => p.Email),
                    "2FA" => Builders<InsightUser>.Sort.Descending(p => p.TwoFactorEnabled),
                    "Lockout" => Builders<InsightUser>.Sort.Descending(p => p.LockoutEnabled),
                    _ => null
                },
                _ => Builders<InsightUser>.Sort.Ascending(p => p.Email)
            };

            var result = await AccountService.GetAsync(filter, sort, state.Page * state.PageSize, state.PageSize).ConfigureAwait(false);

            return new TableData<InsightUser>()
            {
                TotalItems = (int)result.Meta.Total,
                Items = result.Data
            };
        }
        catch (Exception)
        {
            Notification.Error(Snackbar);
            return new TableData<InsightUser>();
        }
    }

    private async Task OnRefreshAsync()
    {
        if (_container is null) return;
        await _container.RefreshAsync().ConfigureAwait(false);
    }
}