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

namespace FruitBankHybrid.Shared.Components.Grids.ShippingItems;

public class GridShippingItemBase : FruitBankGridBase<ShippingItem>, IGrid
{
    private bool _isFirstInitializeParameterCore;
    private bool _isFirstInitializeParameters;

    public GridShippingItemBase () : base()
    {
        AddMessageTag = SignalRTags.AddShippingItem;
        UpdateMessageTag = SignalRTags.UpdateShippingItem;

        //RemoveMessageTag = SignalRTags.;
    }

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

        if (IsMasterGrid) GetAllMessageTag = SignalRTags.GetShippingItems;
        else
        {
            if (ContextIds == null || ContextIds.Length == 0) ContextIds = [ParentDataItem!.Id];

            switch (ParentDataItem)
            {
                case IShippingDocument:
                    GetAllMessageTag = SignalRTags.GetShippingItemsByDocumentId;
                    if (KeyFieldNameToParentId.IsNullOrWhiteSpace()) KeyFieldNameToParentId = nameof(ShippingItem.ShippingDocumentId);

                    break;
                case IShipping:
                    AddMessageTag = 0;//SignalRTags.None;
                    GetAllMessageTag = SignalRTags.GetShippingItemsByShippingId;
                    
                    break;
                case IPartner:
                    AddMessageTag = 0;//SignalRTags.None;
                    GetAllMessageTag = SignalRTags.GetShippingItemsByPartnerId;

                    break;
            }
        }

        await base.OnInitializedAsync();
    }

    //protected override Task OnCustomizeEditModel(GridCustomizeEditModelEventArgs e)
    //{
    //    return 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;
        }
    }
}