using Microsoft.AspNetCore.Components;
using Schaad.Accounting.Models;
namespace Schaad.Accounting.UI.Components.Pages.Controls;
///
/// Filter above the charts on the Strom and Wasser pages: period toggle (bill, calendar year,
/// half-year), year range and the note shown when bills had to be split over several periods.
///
public partial class BillChartToolbar : ComponentBase
{
private static readonly BillPeriodKind[] PeriodKinds =
[BillPeriodKind.Bill, BillPeriodKind.Year, BillPeriodKind.HalfYear];
[Parameter]
public BillChartFilter Filter { get; set; } = new();
[Parameter]
public EventCallback FilterChanged { get; set; }
/// The years any bill touches (see ).
[Parameter]
public IReadOnlyList Years { get; set; } = [];
[Parameter]
public bool IsSplit { get; set; }
private (int From, int To) Range => Filter.RangeIn(Years);
/// Suffix for chart titles, e.g. "pro Halbjahr".
public static string PerPeriod(BillPeriodKind kind) => kind switch
{
BillPeriodKind.Year => "pro Jahr",
BillPeriodKind.HalfYear => "pro Halbjahr",
_ => "pro Abrechnung"
};
private static string PeriodKindName(BillPeriodKind kind) => kind switch
{
BillPeriodKind.Year => "Kalenderjahr",
BillPeriodKind.HalfYear => "Halbjahr",
_ => "Abrechnung"
};
// Picking a "from" after the current "to" (or the other way round) moves the other end along.
private Task OnFromChanged(int year)
=> year == Range.From ? Task.CompletedTask : ChangeAsync(Filter with { FromYear = year, ToYear = Math.Max(Range.To, year) });
private Task OnToChanged(int year)
=> year == Range.To ? Task.CompletedTask : ChangeAsync(Filter with { FromYear = Math.Min(Range.From, year), ToYear = year });
private Task ChangeAsync(BillChartFilter filter) => FilterChanged.InvokeAsync(filter);
}