using AyCode.Core.Interfaces;
using AyCode.Core.Loggers;
using AyCode.Services.SignalRs;
using AyCode.Utils.Extensions;
using DevExpress.Blazor;
using FruitBank.Common.Dtos;
using FruitBank.Common.Models;
using FruitBank.Common.SignalRs;
using FruitBankHybrid.Shared.Databases;
using FruitBankHybrid.Shared.Services.Loggers;
using FruitBankHybrid.Shared.Services.SignalRs;
using Mango.Nop.Core.Loggers;
using Microsoft.AspNetCore.Components;
using System.Diagnostics;
using AyCode.Core.Helpers;

namespace FruitBankHybrid.Shared.Pages;

public partial class OrdersAdmin : ComponentBase
{
    [Inject] public required DatabaseClient Database { get; set; }
    [Inject] public required IEnumerable<IAcLogWriterClientBase> LogWriters { get; set; }
    [Inject] public required FruitBankSignalRClient FruitBankSignalRClient { get; set; }
    [Inject] public required NavigationManager NavManager { get; set; }
    [Inject] private IDialogService DialogService { get; set; } = null!;
    [Inject] public required LoggedInModel LoggedInModel { get; set; }

    public required IGrid gridOrder;
    private AcObservableCollection<ProductDto>? ProductDtos { get; set; } = [];
    private List<OrderDto>? OrderDtos { get; set; } = [];
    private List<OrderItemDto>? OrderItemDtos { get; set; } = [];

    public bool AutoCollapseDetailRow { get; set; }

    private ILogger _logger = null!;
    public int ActiveTabIndex;

    protected override async Task OnInitializedAsync()
    {
        if (!LoggedInModel.IsRevisor) NavManager.NavigateTo("/Login");

        _logger = new LoggerClient<OrdersAdmin>(LogWriters.ToArray());
        _logger.Info("OnInitializedAsync");

        await ReloadDataFromDb(false);
        await base.OnInitializedAsync();
    }

    private async Task ReloadDataFromDb(bool forceReload = false)
    {
        LoadingPanelVisibility.Visible = true;
        //using (await ObjectLock.GetSemaphore<ProductDto>().UseWaitAsync())
        {
            if (ProductDtos == null || !ProductDtos.Any() || forceReload) ProductDtos = new AcObservableCollection<ProductDto>(await Database.ProductDtoTable.LoadDataAsync(!forceReload));
        }

        //using (await ObjectLock.GetSemaphore<OrderItemDto>().UseWaitAsync())
        //{
        //    if (OrderItemDtos == null) OrderItemDtos = await FruitBankSignalRClient.GetAllOrderItemDtos() ?? [];
        //    else if (OrderItemDtos.Count == 0 || forceReload)
        //    {
        //        OrderItemDtos.Clear();
        //        OrderItemDtos.AddRange(await FruitBankSignalRClient.GetAllOrderItemDtos() ?? []);
        //    }
        //}

        LoadingPanelVisibility.Visible = false;
    }

    protected async Task OnActiveTabChanged(int activeTabIndex)
    {
        ActiveTabIndex = activeTabIndex;
        //LoadingPanelVisibility.Visible = true;

        switch (ActiveTabIndex)
        {
            case 0:
                //ProductDtos ??= (await FruitBankSignalRClient.GetProductDtos() ?? []); //.Where(o => o.HasMeasuringAccess(LoggedInModel.CustomerDto?.Id, LoggedInModel.IsRevisor)).OrderBy(o => o.DateOfReceipt).ToList();
                break;
            case 1:
                //OrderDtos ??= (await FruitBankSignalRClient.GetAllOrderDtos() ?? []).OrderByDescending(o => o.Id).ToList(); //.Where(o => o.HasMeasuringAccess(LoggedInModel.CustomerDto?.Id, LoggedInModel.IsRevisor)).OrderBy(o => o.DateOfReceipt).ToList();
                break;
            case 2:
                //OrderItemDtos ??= (await FruitBankSignalRClient.GetAllOrderItemDtos() ?? []).OrderByDescending(o => o.Id).ToList(); //.Where(o => o.HasMeasuringAccess(LoggedInModel.CustomerDto?.Id, LoggedInModel.IsRevisor)).OrderBy(o => o.DateOfReceipt).ToList();
                break;
        }
        
        //LoadingPanelVisibility.Visible = false;
    }
}