using System.Globalization;
using Khaemhat.Application.Localization;
using Khaemhat.Core.Items;
using Khaemhat.Web.Ui;
namespace Khaemhat.Web.Items;
///
/// Addresses, tabs and choices of the item screens (SPEC §11.2, mockups 017 and 018): the radar
/// /anagrafiche/articoli, the record of an item under it, and the price grid /anagrafiche/listini.
///
public static class ItemScreens
{
/// The radar of the items.
public const string RadarUrl = "anagrafiche/articoli";
/// A new item.
public const string NewUrl = RadarUrl + "/nuovo";
/// The price grid.
public const string PriceListsUrl = "anagrafiche/listini";
/// The table of the item categories, where /anagrafiche/categorie leads.
public const string CategoriesUrl = "tabelle/categorie-articoli";
/// The tab ids of the record page, in order.
public static class Tabs
{
/// Main data.
public const string Main = "dati";
/// Alternative units.
public const string Units = "um";
/// Alternative codes.
public const string Barcodes = "codici";
/// Prices of the lists and net prices of the customers.
public const string Prices = "listini";
/// Suppliers.
public const string Suppliers = "fornitori";
/// Substitutes.
public const string Substitutes = "sostitutivi";
/// Notes and attachments.
public const string Notes = "note";
/// History.
public const string History = "storico";
}
/// The record of an item.
/// The item.
/// The address, relative to the base.
public static string RecordUrl(long id) => RadarUrl + "/" + id.ToString(CultureInfo.InvariantCulture);
/// The tab that holds a field of the service (Prices.0.Price, Suppliers.1.PartyId).
/// The field.
/// The tab id.
public static string TabOf(string? field)
{
var head = (field ?? string.Empty).Split('.')[0];
return head switch
{
"Units" => Tabs.Units,
"Barcodes" => Tabs.Barcodes,
"Prices" => Tabs.Prices,
"Suppliers" => Tabs.Suppliers,
"Substitutes" => Tabs.Substitutes,
nameof(Item.Notes) => Tabs.Notes,
_ => Tabs.Main,
};
}
/// The choices of an enum, with the texts Items.<group>.<value>.
/// The enum.
/// The texts.
/// The group of texts.
/// The choices.
public static IReadOnlyList> Choices(ITexts texts, string group)
where TEnum : struct, Enum
{
ArgumentNullException.ThrowIfNull(texts);
return [.. Enum.GetValues().Select(v => new KhOption(v.ToString(), texts["Items." + group + "." + v]))];
}
/// The margin on the price as text, such as 45%; none without both values.
/// The price.
/// The cost.
/// The culture of the texts.
/// The text.
public static string? MarginText(decimal? price, decimal? cost, CultureInfo culture) =>
PriceRounding.Margin(price, cost) is { } margin ? margin.ToString("0.#", culture) + "%" : null;
/// Reads a price typed in a cell: empty means no price, otherwise a number not below zero.
/// The text typed.
/// The culture of the texts.
/// The price, or for none.
/// Whether the text is valid.
public static bool TryParsePrice(string? text, CultureInfo culture, out decimal? price)
{
ArgumentNullException.ThrowIfNull(culture);
price = null;
var clean = (text ?? string.Empty).Replace("€", string.Empty, StringComparison.Ordinal).Trim();
if (clean.Length == 0)
{
return true;
}
if (!decimal.TryParse(clean, NumberStyles.Number, culture, out var value) || value < 0)
{
return false;
}
price = Math.Round(value, 4, MidpointRounding.AwayFromZero);
return true;
}
}