using AyCode.Blazor.Components.Components.Grids;
using AyCode.Core.Interfaces;
using DevExpress.Blazor;
using FruitBank.Common.Models;
using FruitBankHybrid.Shared.Services.Loggers;
using FruitBankHybrid.Shared.Services.SignalRs;
using Microsoft.AspNetCore.Components;

namespace FruitBankHybrid.Shared.Components.Grids;

public class FruitBankGridBase<TDataItem> : MgGridBase<SignalRDataSourceObservable<TDataItem>, TDataItem, int, LoggerClient> where TDataItem : class, IId<int>
{
    [Inject] public required LoggedInModel LoggedInModel { get; set; }

    private bool _isFirstInitializeParameterCore;
    private bool _isFirstInitializeParameters;
    public bool PreRendered { get; set; }

    /// <summary>
    /// Override to provide the logged-in user's ID for layout storage
    /// </summary>
    protected override int GetLayoutUserId() => LoggedInModel.CustomerDto?.Id ?? 0;

    protected void OnCustomizeElement(GridCustomizeElementEventArgs e)
    {
        if (IsMasterGrid && e.ElementType == GridElementType.DataRow && e.VisibleIndex % 2 == 1 && !e.Grid.IsRowSelected(e.VisibleIndex) && !e.Grid.IsRowFocused(e.VisibleIndex))
        {
            e.CssClass = " alt-item";
        }

        if (e.ElementType == GridElementType.DataRow && !LoggedInModel.IsAdministrator)
        {
            e.CssClass = "hideDetailButton";
        }

        if (e.ElementType == GridElementType.HeaderCell)
        {
            e.Style = "background-color: #E6E6E6;";
        }
    }

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

        if (!_isFirstInitializeParameterCore)
        {
            KeyFieldName = "Id";
            CustomizeElement += OnCustomizeElement;
            _isFirstInitializeParameterCore = true;
        }
    }

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

        if (!_isFirstInitializeParameters)
        {
            SizeMode = DevExpress.Blazor.SizeMode.Small;
            ShowGroupPanel = IsMasterGrid;
            ShowSearchBox = IsMasterGrid;
            ShowFilterRow = IsMasterGrid;

            FilterMenuButtonDisplayMode = (IsMasterGrid ? GridFilterMenuButtonDisplayMode.Never : GridFilterMenuButtonDisplayMode.Always);

            DetailRowDisplayMode = IsMasterGrid ? GridDetailRowDisplayMode.Auto : GridDetailRowDisplayMode.Never;
            DetailExpandButtonDisplayMode = IsMasterGrid ? GridDetailExpandButtonDisplayMode.Auto : GridDetailExpandButtonDisplayMode.Never;

            TextWrapEnabled = false;
            AllowSelectRowByClick = true;
            HighlightRowOnHover = true;
            AutoCollapseDetailRow = true;
            AutoExpandAllGroupRows = false;

            PagerVisible = true;//IsMasterGrid;
            PageSize = IsMasterGrid ? (SizeMode == DevExpress.Blazor.SizeMode.Small ? 20 : 15) : 10;

            AllowColumnReorder = true;
            AllowGroup = IsMasterGrid;
            AllowSort = true;

            EditMode = GridEditMode.EditRow;
            FocusedRowEnabled = true;
            ColumnResizeMode = GridColumnResizeMode.NextColumn;
            PageSizeSelectorVisible = true;

            _isFirstInitializeParameters = true;
        }
    }

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
    }
}