using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HC_APTBS.Services;
namespace HC_APTBS.ViewModels.Dialogs
{
///
/// ViewModel for the WReport dialog.
///
///
/// Lets the operator fill in client and company information before generating
/// the PDF report. The client list is backed by the
/// and persisted when the dialog is accepted.
///
///
public sealed partial class ReportViewModel : ObservableObject
{
// ── Services ──────────────────────────────────────────────────────────────
private readonly IConfigurationService _config;
// ── Constructor ───────────────────────────────────────────────────────────
/// Initialises and populates the dialog from configuration.
public ReportViewModel(IConfigurationService configService)
{
_config = configService;
CompanyName = _config.Settings.CompanyName;
CompanyInfo = _config.Settings.CompanyInfo;
foreach (var name in _config.Clients.Keys)
ClientNames.Add(name);
}
// ── Company ───────────────────────────────────────────────────────────────
/// Company name shown in the report header.
[ObservableProperty] private string _companyName = string.Empty;
/// Company address / contact info shown in the report header.
[ObservableProperty] private string _companyInfo = string.Empty;
// ── Client ────────────────────────────────────────────────────────────────
/// List of known client names for the autocomplete combo box.
public ObservableCollection ClientNames { get; } = new();
/// Currently selected or typed client name.
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SaveClientCommand))]
[NotifyCanExecuteChangedFor(nameof(DeleteClientCommand))]
private string _selectedClientName = string.Empty;
/// Free-text notes for the selected client.
[ObservableProperty] private string _clientInfo = string.Empty;
/// Operator name.
[ObservableProperty] private string _operatorName = string.Empty;
/// Free-text observations appended to the report.
[ObservableProperty] private string _observations = string.Empty;
// ── Dialog result ─────────────────────────────────────────────────────────
/// True after the user clicks Accept; false if they cancel.
public bool Accepted { get; private set; }
// ── Commands ──────────────────────────────────────────────────────────────
/// Saves the current client entry, closes the dialog with Accepted=true.
[RelayCommand]
private void Accept()
{
SaveCurrentClient();
_config.Settings.CompanyName = CompanyName;
_config.Settings.CompanyInfo = CompanyInfo;
_config.SaveClients();
_config.SaveSettings();
Accepted = true;
RequestClose?.Invoke();
}
/// Closes the dialog without saving.
[RelayCommand]
private void Cancel()
{
Accepted = false;
RequestClose?.Invoke();
}
/// Persists the current client entry and refreshes the client list.
[RelayCommand(CanExecute = nameof(HasClientName))]
private void SaveClient()
{
SaveCurrentClient();
RefreshClientList();
}
/// Deletes the currently selected client from the list.
[RelayCommand(CanExecute = nameof(HasClientName))]
private void DeleteClient()
{
string name = SelectedClientName.Trim();
if (string.IsNullOrEmpty(name)) return;
if (_config.Clients.ContainsKey(name))
_config.Clients.Remove(name);
SelectedClientName = string.Empty;
ClientInfo = string.Empty;
RefreshClientList();
}
///
/// Called when the user picks a client name from the combo box.
/// Loads the associated info text.
///
public void SelectClient(string name)
{
SelectedClientName = name ?? string.Empty;
if (!string.IsNullOrEmpty(name) && _config.Clients.TryGetValue(name, out var info))
ClientInfo = info;
}
// ── Events ────────────────────────────────────────────────────────────────
/// Raised when the dialog should close itself.
public event System.Action? RequestClose;
// ── Helpers ───────────────────────────────────────────────────────────────
private bool HasClientName() => !string.IsNullOrWhiteSpace(SelectedClientName);
private void SaveCurrentClient()
{
string name = SelectedClientName.Trim();
if (string.IsNullOrEmpty(name)) return;
if (_config.Clients.ContainsKey(name))
_config.Clients[name] = ClientInfo;
else
_config.Clients.Add(name, ClientInfo);
}
private void RefreshClientList()
{
ClientNames.Clear();
foreach (var name in _config.Clients.Keys)
ClientNames.Add(name);
}
}
}