using CommunityToolkit.Mvvm.Messaging;
using SteUp.Shared.Core.BarcodeReader.Contracts;
using SteUp.Shared.Core.Messages.Scanner;

namespace SteUp.Shared.Core.BarcodeReader;

public class BarcodeManager(
    IBarcodeReaderService scanner,
    IMessenger messenger) : IBarcodeManager
{
    public void Init()
    {
        if (!scanner.IsRightAdapter())
        {
            Console.WriteLine("Dispositivo non compatibile con lo scanner Honeywell.");
            return;
        }

        scanner.Register(
            onScanSuccessful: dto =>
            {
                messenger.Send(new NewScannerMessage(dto.StringValue));
            },
            onScanFailed: ex =>
            {
                messenger.Send(new ErrorScannerMessage(ex.Message));
            }
        );

        scanner.Init(() => { scanner.ChangeSettings([("TRIGGER_SCAN_MODE", "ONE_SHOT")]); });
    }
}