namespace Inspectron.Epson;

/// <summary>
/// Discovery service that returns preconfigured printers instead of performing network discovery
/// </summary>
public class PreconfiguredDiscoveryService : IDiscoveryService
{
    private readonly List<DiscoveredPrinter> _printers = new();

    public void AddPrinter(string ipAddress, string? modelName = null, int port = 9100)
    {
        _printers.Add(new DiscoveredPrinter
        {
            IPAddress = ipAddress,
            ModelName = modelName ?? "Unknown",
            Port = port,
            DiscoveredAt = DateTime.UtcNow
        });
    }

    public void RemovePrinter(string ipAddress)
    {
        _printers.RemoveAll(p => p.IPAddress == ipAddress);
    }

    public void ClearPrinters()
    {
        _printers.Clear();
    }

    public Task<List<DiscoveredPrinter>> DiscoverPrintersAsync(
        TimeSpan? timeout = null,
        Action<DiscoveredPrinter>? onPrinterDiscovered = null)
    {
        var results = _printers.Select(p => new DiscoveredPrinter
        {
            IPAddress = p.IPAddress,
            ModelName = p.ModelName,
            Port = p.Port,
            MACAddress = p.MACAddress,
            ServiceUrl = p.ServiceUrl,
            ServiceType = p.ServiceType,
            Lifetime = p.Lifetime,
            RawResponse = p.RawResponse,
            DiscoveredAt = DateTime.UtcNow
        }).ToList();

        if (onPrinterDiscovered != null)
        {
            foreach (var printer in results)
                onPrinterDiscovered(printer);
        }

        return Task.FromResult(results);
    }
}
