@using System.Collections.ObjectModel
@using AyCode.Blazor.Components.Components.Grids
@using AyCode.Core.Helpers
@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

@inject IEnumerable<IAcLogWriterClientBase> LogWriters
@inject FruitBankSignalRClient FruitBankSignalRClient

<MgGridWithInfoPanel ShowInfoPanel="@IsMasterGrid">
	<GridContent>
		<GridStockTakingItemBase @ref="Grid" AutoSaveLayoutName="GridStockTakingItem" SignalRClient="FruitBankSignalRClient" Logger="_logger"
		                         CssClass="@GridCss" ValidationEnabled="false" OnGridFocusedRowChanged="Grid_FocusedRowChanged">
								 @* CustomizeGroupValueDisplayText="Grid_CustomizeGroupValueDisplayText"> *@
			<Columns>
				<DxGridDataColumn FieldName="@nameof(StockTakingItem.Id)" SortIndex="0" SortOrder="GridColumnSortOrder.Descending" ReadOnly="true" />

				<DxGridDataColumn FieldName="@nameof(StockTakingItem.StockTakingId)" TextAlignment="GridTextAlignment.Left" Caption="Leltár Id"/>
				<DxGridDataColumn FieldName="@($"{nameof(StockTakingItem.StockTaking)}.{nameof(StockTaking.StartDateTime)}")" Visible="true" DisplayFormat="g" Caption="Leltár időpontja" />
				<DxGridDataColumn FieldName="@($"{nameof(StockTakingItem.Product)}.{nameof(ProductDto.Name)}")" Caption="ProductName" />
				<DxGridDataColumn FieldName="@nameof(StockTakingItem.OriginalStockQuantity)" />
				<DxGridDataColumn FieldName="@nameof(StockTakingItem.InProcessOrdersQuantity)" />
				<DxGridDataColumn FieldName="@nameof(StockTakingItem.TotalOriginalQuantity)" />
				<DxGridDataColumn FieldName="@nameof(StockTakingItem.MeasuredStockQuantity)" />

				<DxGridDataColumn FieldName="@nameof(StockTakingItem.OriginalNetWeight)" />
				<DxGridDataColumn FieldName="@nameof(StockTakingItem.MeasuredNetWeight)" />

				<DxGridDataColumn FieldName="@nameof(StockTakingItem.QuantityDiff)" />
				<DxGridDataColumn FieldName="@nameof(StockTakingItem.NetWeightDiff)" />

				<DxGridDataColumn FieldName="@nameof(StockTakingItem.IsMeasurable)" />
				<DxGridDataColumn FieldName="@nameof(StockTakingItem.IsMeasured)" />
				<DxGridDataColumn FieldName="@nameof(StockTakingItem.IsInvalid)" />

				<DxGridDataColumn FieldName="@nameof(StockTakingItem.Created)" ReadOnly="true" Visible="false" DisplayFormat="g" />
				<DxGridDataColumn FieldName="@nameof(StockTakingItem.Modified)" ReadOnly="true" DisplayFormat="g" />
				<DxGridCommandColumn Visible="!IsMasterGrid" Width="120"></DxGridCommandColumn>
			</Columns>
			<ToolbarTemplate>
				@if (IsMasterGrid)
				{
					<MgGridToolbarTemplate Grid="Grid" OnReloadDataClick="() => ReloadDataFromDb(true)" EnableDelete="false" EnableEdit="false" EnableNew="false" />
				}
			</ToolbarTemplate>
		</GridStockTakingItemBase>
	</GridContent>
</MgGridWithInfoPanel>

@code {

	//[Inject] public required ObjectLock ObjectLock { get; set; }
	[Inject] public required DatabaseClient Database { get; set; }

	[Parameter] public bool IsMasterGrid { get; set; } = false;
	[Parameter] public AcObservableCollection<Partner>? Partners { get; set; }
	[Parameter] public AcObservableCollection<Shipping>? Shippings { get; set; }

	const string ExportFileName = "ExportResult";
	string GridSearchText = "";
	bool EditItemsEnabled { get; set; }
	int FocusedRowVisibleIndex { get; set; }
	public GridStockTakingItemBase Grid { get; set; }
	string GridCss => !IsMasterGrid ? "hide-toolbar" : string.Empty;

	private int _activeTabIndex;
	private LoggerClient<GridStockTakingItem> _logger;

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

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

		if (Grid == null) return;

		//using (await ObjectLock.GetSemaphore<StockTakingItem>().UseWaitAsync())
		//if (forceReload) await Grid.ReloadDataSourceAsync();

		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;
	}

	// static void Grid_CustomizeGroupValueDisplayText(GridCustomizeGroupValueDisplayTextEventArgs e)
	// {
	// 	if (e.FieldName != nameof(StockTakingItem.StockTakingId)) return;

	// 	if (e.GetRowValue("StockTaking.StartDateTime") is DateTime startDateTime)
	// 	{
	// 		e.DisplayText = startDateTime.ToString("g");
	// 	}
	// }

}

