using AyCode.Core.Helpers;
using AyCode.Core.Loggers;
using DevExpress.Blazor;
using DevExpress.ClipboardSource.SpreadsheetML;
using FruitBank.Common.Dtos;
using FruitBank.Common.Entities;
using FruitBank.Common.Interfaces;
using FruitBank.Common.Models;
using FruitBankHybrid.Shared.Components;
using FruitBankHybrid.Shared.Components.Grids.ShippingItems;
using FruitBankHybrid.Shared.Components.Grids.Shippings;
using FruitBankHybrid.Shared.Components.Grids.Partners;
using FruitBankHybrid.Shared.Databases;
using FruitBankHybrid.Shared.Services.Loggers;
using FruitBankHybrid.Shared.Services.SignalRs;
using Mango.Nop.Core.Loggers;
using Microsoft.AspNetCore.Components;
using FruitBankHybrid.Shared.Components.Grids.Cargos;

namespace FruitBankHybrid.Shared.Pages;

public partial class ShippingsAdmin : ComponentBase
{
    [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; }
    [Inject] public required DatabaseClient Database { get; set; }

    private GridProductDtoTemplate gridProductDtoTemplate;
    private GridPartner gridPartner;
    private GridCargoPartner gridCargoPartner;
    private GridShipping gridShipping;
    private GridShippingDocument gridShippingDocument;
    private GridShippingItemTemplate gridShippingItemTemplate;
    private GridShippingItemPallets gridShippingItemPallet;

    public AcObservableCollection<Shipping> Shippings { get; set; } = [];
    public AcObservableCollection<ShippingDocument> ShippingDocuments { get; set; } = [];
    public AcObservableCollection<ShippingItem> ShippingItems { get; set; } = [];
    public List<ShippingItemPallet> ShippingItemPallets { get; set; } = [];
    public AcObservableCollection<Partner> Partners { get; set; } = [];
    public AcObservableCollection<CargoPartner> CargoPartners { 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");

        LoadingPanelVisibility.Visible = true;

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

        await RefreshOrdersFromDb(DateTime.Now);
        await base.OnInitializedAsync();
    }

    private async Task RefreshOrdersFromDb(DateTime dateTime)
    {
        LoadingPanelVisibility.Visible = true;

        //Shippings.Clear();
        //Shippings.AddRange((await FruitBankSignalRClient.GetShippings() ?? []).OrderByDescending(o => o.Id));//.Where(o => o.HasMeasuringAccess(LoggedInModel.CustomerDto?.Id, LoggedInModel.IsRevisor)).OrderBy(o => o.DateOfReceipt).ToList();

        LoadingPanelVisibility.Visible = false;
    }

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

        switch (ActiveTabIndex)
        {
            case 0:
                //await Database.ProductDtoTable.LoadDataAsync(true);
                //gridProductDtoTemplate.Grid.Reload();
                break;
            case 1:
                if (Shippings.Count == 0)
                {
                    //Shippings.AddRange((await FruitBankSignalRClient.GetShippings() ?? []).OrderByDescending(o => o.Id)); //.Where(o => o.HasMeasuringAccess(LoggedInModel.CustomerDto?.Id, LoggedInModel.IsRevisor)).OrderBy(o => o.DateOfReceipt).ToList();}
                }

                //gridShipping.Grid.Reload();

                break;
            case 2:
                if (ShippingDocuments.Count == 0)
                {
                    //ShippingDocuments.AddRange((await FruitBankSignalRClient.GetShippingDocuments() ?? []).OrderByDescending(o => o.Id)); //.Where(o => o.HasMeasuringAccess(LoggedInModel.CustomerDto?.Id, LoggedInModel.IsRevisor)).OrderBy(o => o.DateOfReceipt).ToList();
                }

                //gridShippingDocument.Grid.Reload();
                break;
            case 3:
                //gridShippingItemTemplate.Grid.Reload();
                //if(ShippingItems.Count=0) ShippingItems.AddRange((await FruitBankSignalRClient.GetShippingItems() ?? []).OrderByDescending(o => o.Id)); //.Where(o => o.HasMeasuringAccess(LoggedInModel.CustomerDto?.Id, LoggedInModel.IsRevisor)).OrderBy(o => o.DateOfReceipt).ToList();
                break;
            case 4:
                //gridShippingItemPallet.gridOrderItemPallet.Reload();
                break;
        }

        LoadingPanelVisibility.Visible = false;
    }
}