namespace Inspectron.Epson.Templates.Configuration;

public class PrinterProfileRegistry
{
    private readonly Dictionary<byte, PrinterProfile> _profilesById = new();
    private readonly Dictionary<string, PrinterProfile> _profilesByName = new();

    public PrinterProfileRegistry()
    {
        // Register built-in profiles
        RegisterBuiltInProfiles();
    }

    private void RegisterBuiltInProfiles()
    {
        // TM-T30III (default thermal printer)
        var tmT30III = new PrinterProfile(
            Id: "tm-t30iii",
            Name: "TM-T30III",
            LineWidth: 48,
            BigLineWidth: 24,
            SupportsRed: false);
        Register(0x01, tmT30III);

        // TM-U220II (impact printer with red support)
        var tmU220II = new PrinterProfile(
            Id: "tm-u220ii",
            Name: "TM-U220II",
            LineWidth: 33,
            BigLineWidth: 20,
            SupportsRed: true);
        Register(0x0D, tmU220II);
        Register(0x13, tmU220II);
    }

    public void Register(byte printerId, PrinterProfile profile)
    {
        _profilesById[printerId] = profile;
        _profilesByName[profile.Id] = profile;
    }

    public PrinterProfile GetProfile(byte printerId)
    {
        return _profilesById.TryGetValue(printerId, out var profile)
            ? profile
            : PrinterProfile.Default;
    }

    public PrinterProfile GetProfile(string profileId)
    {
        return _profilesByName.TryGetValue(profileId, out var profile)
            ? profile
            : PrinterProfile.Default;
    }

    public IEnumerable<PrinterProfile> GetAllProfiles()
    {
        return _profilesByName.Values.Distinct();
    }

    public bool TryGetProfile(byte printerId, out PrinterProfile? profile)
    {
        return _profilesById.TryGetValue(printerId, out profile);
    }

    public bool TryGetProfile(string profileId, out PrinterProfile? profile)
    {
        return _profilesByName.TryGetValue(profileId, out profile);
    }
}
