using AyCode.Core.Interfaces;
using AyCode.Utils.Extensions;
using DevExpress.Blazor;
using FruitBank.Common.Dtos;
using FruitBank.Common.Entities;
using FruitBank.Common.Interfaces;
using FruitBank.Common.SignalRs;
using FruitBankHybrid.Shared.Pages;
using Microsoft.AspNetCore.Components;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Orders;

namespace FruitBankHybrid.Shared.Components.Grids.GenericAttributes;

public class GridGenericAttributeBase: FruitBankGridBase<GenericAttributeDto>, IGrid
{
    protected const int ContextEntityIdIndex = 0;
    protected const int ContextKeyGroupIndex = 1;
    protected const int ContextStoreIdIndex = 2;

    private bool _isFirstInitializeParameterCore;
    private bool _isFirstInitializeParameters;

    public GridGenericAttributeBase() : base()
    {
        AddMessageTag = SignalRTags.AddGenericAttributeDto;
        UpdateMessageTag = SignalRTags.UpdateGenericAttributeDto;

        //RemoveMessageTag = SignalRTags.;
    }

    protected override async Task OnInitializedAsync()
    {
        if (GetAllMessageTag > 0) return;

        if (IsMasterGrid) GetAllMessageTag = SignalRTags.GetGenericAttributeDtos;
        else
        {
            var hasContextIdParameter = ContextIds is { Length: > 0 };

            if (!hasContextIdParameter)
            {
                ContextIds = new object[3];
                ContextIds[ContextEntityIdIndex] = ParentDataItem!.Id;
                ContextIds[ContextStoreIdIndex] = LoggedInModel.CustomerDto!.RegisteredInStoreId;
            }

            GetAllMessageTag = SignalRTags.GetGenericAttributeDtosByEntityIdAndKeyGroup;
            if (KeyFieldNameToParentId.IsNullOrWhiteSpace()) KeyFieldNameToParentId = nameof(GenericAttribute.EntityId);

            switch (ParentDataItem)
            {
                case IProductDto:
                    if (!hasContextIdParameter) ContextIds![ContextKeyGroupIndex] = nameof(Product);

                    break;
                case IOrderDto:
                    if (!hasContextIdParameter) ContextIds![ContextKeyGroupIndex] = nameof(Order);

                    break;
                case IOrderItemDto:
                    if (!hasContextIdParameter) ContextIds![ContextKeyGroupIndex] = nameof(OrderItem);

                    break;
            }
        }

        await base.OnInitializedAsync();
    }

    protected override async Task OnCustomizeEditModel(GridCustomizeEditModelEventArgs e)
    {
        if (e.IsNew && ParentDataItem != null && ContextIds != null)
        {
            var editModel = (e.EditModel as GenericAttributeDto)!;
            
            editModel.KeyGroup = (string)ContextIds![ContextKeyGroupIndex];
            editModel.StoreId = (int)ContextIds![ContextStoreIdIndex];
            editModel.CreatedOrUpdatedDateUTC = DateTime.UtcNow;

            e.EditModel = editModel;
        }

        await base.OnCustomizeEditModel(e);
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();

        if (!_isFirstInitializeParameters)
        {
            //if (!IsMasterGrid && (ContextIds == null || ContextIds.Length == 0))
            //{
            //    ContextIds = [ParentDataItem!.Id];
            //    GetAllMessageTag = SignalRTags.GetShippingItemsByDocumentId;
            //}

            _isFirstInitializeParameters = false;
        }
    }

    protected override async Task SetParametersAsyncCore(ParameterView parameters)
    {
        await base.SetParametersAsyncCore(parameters);

        if (!_isFirstInitializeParameterCore)
        {
            //if (!IsMasterGrid && (ContextIds == null || ContextIds.Length == 0))
            //{
            //    ContextIds = [ParentDataItem!.Id];
            //    GetAllMessageTag = SignalRTags.GetShippingItemsByDocumentId;
            //}

            //ShowFilterRow = true;
            //ShowGroupPanel = true;
            //AllowSort = false;

            //etc...

            _isFirstInitializeParameterCore = false;
        }
    }
}