using System.Globalization;
using Khaemhat.Infrastructure.Files;
namespace Khaemhat.Application.Ledger;
/// A column of a ledger print.
/// The heading.
/// The width in points.
/// Aligned on the right (amounts).
public sealed record ReportColumn(string Title, double Width, bool Right = false);
///
/// The prints of the ledger (VAT registers, general journal, ledgers, trial balance, issue #65 and #67): A4 landscape,
/// the company and the title on every page, the headings repeated, the pages numbered from a first page in the form
/// year/page as the final prints of the books want (art. 2215-bis c.c., numbering by year).
///
internal static class LedgerReport
{
private const double Margin = 28;
private const double LineHeight = 11;
private static readonly PdfColor Rule = PdfColor.Gray(180);
/// Writes a print.
/// The name, VAT number and address of the company.
/// The title, such as Registro IVA vendite.
/// The period and the kind of print.
/// The columns.
/// The rows; a row whose first cell starts with ** is a bold total.
/// The year of the page numbers.
/// The number of the first page.
/// Cancellation.
/// The file and the number of pages.
public static async Task<(byte[] File, int Pages)> WriteAsync(
string company, string title, string subtitle, IReadOnlyList columns, IReadOnlyList> rows, int year, int firstPage,
CancellationToken cancellationToken)
{
using var buffer = new MemoryStream();
int pages;
await using (var pdf = new PdfWriter(buffer, new PdfDocumentInfo { Title = title }, leaveOpen: true))
{
var width = PdfWriter.A4Height;
var height = PdfWriter.A4Width;
var scale = Math.Min(1d, (width - (2 * Margin)) / Math.Max(1d, columns.Sum(c => c.Width)));
PdfPage? page = null;
var y = 0d;
var number = firstPage;
async Task NewPageAsync()
{
page = await pdf.AddPageAsync(width, height, cancellationToken).ConfigureAwait(false);
y = height - Margin;
page.DrawText(company, Margin, y, PdfFont.HelveticaBold, 9);
page.DrawTextRight(year.ToString(CultureInfo.InvariantCulture) + "/" + number.ToString(CultureInfo.InvariantCulture), width - Margin, y, PdfFont.Helvetica, 9);
y -= 14;
page.DrawText(title, Margin, y, PdfFont.HelveticaBold, 12);
y -= 12;
page.DrawText(subtitle, Margin, y, PdfFont.Helvetica, 8.5);
y -= 16;
var x = Margin;
foreach (var column in columns)
{
var w = column.Width * scale;
if (column.Right)
{
page.DrawTextRight(column.Title, x + w - 2, y, PdfFont.HelveticaBold, 7.5);
}
else
{
page.DrawText(column.Title, x + 2, y, PdfFont.HelveticaBold, 7.5);
}
x += w;
}
page.DrawLine(Margin, y - 3, width - Margin, y - 3, 0.6, Rule);
y -= LineHeight + 2;
number++;
}
await NewPageAsync().ConfigureAwait(false);
foreach (var row in rows)
{
if (y < Margin + LineHeight)
{
await NewPageAsync().ConfigureAwait(false);
}
var bold = row.Count > 0 && row[0] is { } first && first.StartsWith("**", StringComparison.Ordinal);
var font = bold ? PdfFont.HelveticaBold : PdfFont.Helvetica;
var x = Margin;
for (var i = 0; i < columns.Count; i++)
{
var w = columns[i].Width * scale;
var text = i < row.Count ? row[i] : null;
if (i == 0 && bold)
{
text = text![2..];
}
text = PdfTextLayout.Fit(text, font, 7.5, w - 4);
if (columns[i].Right)
{
page!.DrawTextRight(text, x + w - 2, y, font, 7.5);
}
else
{
page!.DrawText(text, x + 2, y, font, 7.5);
}
x += w;
}
if (bold)
{
page!.DrawLine(Margin, y + LineHeight - 2, width - Margin, y + LineHeight - 2, 0.4, Rule);
}
y -= LineHeight;
}
pages = pdf.PageCount;
await pdf.CloseAsync(cancellationToken).ConfigureAwait(false);
}
return (buffer.ToArray(), pages);
}
/// An amount as the prints show it, with the Italian separators.
/// The amount.
/// The text; empty for zero.
public static string Money(decimal amount) => amount == 0m ? string.Empty : amount.ToString("#,##0.00", Italian);
/// A date as the prints show it.
/// The date.
/// The text.
public static string Date(DateOnly? date) => date?.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) ?? string.Empty;
private static readonly CultureInfo Italian = CultureInfo.GetCultureInfo("it-IT");
}