using System.Globalization;
using System.Reflection;
using Khaemhat.Application.Localization;
using Khaemhat.Application.Security;
using Khaemhat.Application.Tables;
using Khaemhat.Core.Entities;
using Khaemhat.Fiscal.Abstractions;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Tables;
/// The choices of the fields of a page: rows of other tables, lists of the fiscal pack, enumerations, fixed values.
public sealed class TableOptions
{
private readonly Dictionary>> _lookups = new(StringComparer.Ordinal);
/// The fiscal pack of the company.
public IFiscalPack? Pack { get; private set; }
/// Reads the pack of the company and the rows every lookup field points to.
/// The tables.
/// The fiscal context.
/// The fields.
/// Who reads.
/// The company.
/// Cancellation.
/// A task.
public async Task LoadAsync(TableService tables, CompanyFiscalContext fiscal, IEnumerable fields, Actor actor, string company,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(fiscal);
ArgumentNullException.ThrowIfNull(fields);
Pack = await fiscal.PackOfAsync(company, cancellationToken).ConfigureAwait(false);
foreach (var field in fields.Where(f => f is { Kind: TableFieldKind.Lookup, Lookup: not null }))
{
var choices = await TableValues.LookupAsync(tables, field.Lookup!, field.ByCode, actor, company, cancellationToken).ConfigureAwait(false);
_lookups[field.Property] = [.. choices.Select(c => new KhOption(c.Value, c.Text))];
}
}
/// The choices of a field for a row.
/// The field.
/// Its property.
/// The row, to keep an obsolete code or leave out the row itself.
/// The texts.
/// The choices.
public IReadOnlyList> For(TableField field, PropertyInfo property, object? row, ITexts texts)
{
ArgumentNullException.ThrowIfNull(field);
ArgumentNullException.ThrowIfNull(property);
ArgumentNullException.ThrowIfNull(texts);
var current = row is null ? null : TableValues.GetText(row, property);
switch (field.Kind)
{
case TableFieldKind.Choice when field.Values is { } values:
return [.. values.Select(v => new KhOption(v, texts[ValueKey(field, v)]))];
case TableFieldKind.Choice:
return [.. TableValues.EnumChoices(property, v => texts[TableScreens.EnumKey(v)]).Select(c => new KhOption(c.Value, c.Text))];
case TableFieldKind.CodeList:
var list = Pack is null ? null : field.CodeList?.Invoke(Pack);
if (list is null)
{
return current is null ? [] : [new(current, current)];
}
return [.. list.Entries.Where(e => !e.Obsolete || e.Code == current).Select(e => new KhOption(e.Code, e.Code + " · " + e.Description))];
default:
var options = _lookups.GetValueOrDefault(field.Property) ?? [];
// A self reference never points to the row itself.
return field.Lookup == row?.GetType() && row is Entity { Id: > 0 } entity
? [.. options.Where(o => o.Value != entity.Id.ToString(CultureInfo.InvariantCulture))]
: options;
}
}
/// The code shown in the grid for the value of a lookup.
/// The field.
/// The stored value.
/// The code.
public string? CodeOf(TableField field, string? value)
{
ArgumentNullException.ThrowIfNull(field);
return value is null ? null : _lookups.GetValueOrDefault(field.Property)?.FirstOrDefault(o => o.Value == value)?.Text.Split(" · ")[0] ?? value;
}
/// The text key of a fixed value: CompanyTables.Values.<Property>.<Value>.
/// The field.
/// The value.
/// The key.
public static string ValueKey(TableField field, string value)
{
ArgumentNullException.ThrowIfNull(field);
return "CompanyTables.Values." + field.Property + "." + value;
}
}