@using System.Collections.ObjectModel
@using AyCode.Blazor.Components.Components.Grids
@using AyCode.Core.Helpers
@using AyCode.Core.Interfaces
@using AyCode.Core.Loggers
@using AyCode.Utils.Extensions
@using FruitBank.Common.Dtos
@using FruitBank.Common.Entities
@using FruitBankHybrid.Shared.Components.Grids.Shippings
@using FruitBankHybrid.Shared.Databases
@using FruitBankHybrid.Shared.Services.Loggers
@using FruitBankHybrid.Shared.Services.SignalRs
@using Nop.Core.Domain.Common

@inject IEnumerable<IAcLogWriterClientBase> LogWriters
@inject FruitBankSignalRClient FruitBankSignalRClient

<MgGridWithInfoPanel ShowInfoPanel="@IsMasterGrid">
	<GridContent>
		<GridGenericAttributeBase @ref="Grid" DataSource="GenericAttributes" AutoSaveLayoutName="GridGenericAttribute" SignalRClient="FruitBankSignalRClient" Logger="_logger"
								  ParentDataItem="@ParentDataItem" CssClass="@GridCss" ValidationEnabled="false" OnGridFocusedRowChanged="Grid_FocusedRowChanged">
			<Columns>
				<DxGridDataColumn FieldName="@nameof(GenericAttribute.Id)" SortIndex="0" SortOrder="GridColumnSortOrder.Descending" ReadOnly="true" />

				<DxGridDataColumn FieldName="@nameof(GenericAttribute.KeyGroup)" ReadOnly="true" />
				<DxGridDataColumn FieldName="@nameof(GenericAttribute.Key)" />
				<DxGridDataColumn FieldName="@nameof(GenericAttribute.Value)" />
				<DxGridDataColumn FieldName="@nameof(GenericAttribute.EntityId)" ReadOnly="true" />
				<DxGridDataColumn FieldName="@nameof(GenericAttribute.StoreId)" ReadOnly="true" />
				<DxGridDataColumn FieldName="@nameof(GenericAttribute.CreatedOrUpdatedDateUTC)" ReadOnly="true" />

				<DxGridCommandColumn Visible="!IsMasterGrid" Width="120"></DxGridCommandColumn>
			</Columns>
			<ToolbarTemplate>
				@if (IsMasterGrid)
				{
					<MgGridToolbarTemplate Grid="Grid" OnReloadDataClick="() => ReloadDataFromDb(true)" />
				}
			</ToolbarTemplate>
		</GridGenericAttributeBase>
	</GridContent>
</MgGridWithInfoPanel>

@code {
	[Inject] public required DatabaseClient Database { get; set; }

	[Parameter] public AcObservableCollection<GenericAttributeDto>? GenericAttributes { get; set; }

	//[Parameter] public object[]? ContextIds { get; set; }
	[Parameter] public IId<int>? ParentDataItem { get; set; }
	public bool IsMasterGrid => ParentDataItem == null;

	const string ExportFileName = "ExportResult";
	string GridSearchText = "";
	bool EditItemsEnabled { get; set; }
	int FocusedRowVisibleIndex { get; set; }

	public GridGenericAttributeBase Grid { get; set; }
	string GridCss => !IsMasterGrid ? "hide-toolbar" : string.Empty;

	private LoggerClient<GridGenericAttribute> _logger;

	protected override async Task OnInitializedAsync()
	{
		_logger = new LoggerClient<GridGenericAttribute>(LogWriters.ToArray());
		await ReloadDataFromDb(false);
	}

	private async Task ReloadDataFromDb(bool forceReload = false)
	{
		if (!IsMasterGrid) return;

		if (forceReload) Grid?.Reload();
	}

	async Task Grid_FocusedRowChanged(GridFocusedRowChangedEventArgs args)
	{
		if (Grid == null) return;

		if (Grid.IsEditing() && !Grid.IsEditingNewRow())
			await Grid.SaveChangesAsync();

		FocusedRowVisibleIndex = args.VisibleIndex;
		EditItemsEnabled = true;
	}
}

