using Android.OS;
using Com.Honeywell.Aidc;
using SteUp.Shared.Core.BarcodeReader.Dto;

namespace SteUp.Maui.Core.System;

public partial class HoneywellScannerService
{
    private AidcManager? _aidcManager;
    private BarcodeReader? _barcodeReader;

    private static readonly List<string> CompatibleModels = ["EDA50", "EDA51", "EDA52", "CT60", "CT40", "CT40XP", "CT70-X1"];

    public partial bool IsRightAdapter()
    {
        return CompatibleModels.Contains(Build.Model ?? string.Empty);
    }

    public partial string GetAdapterName() => "Honeywell";

    public partial void Init(Action onDeviceReady)
    {
        if (!IsRightAdapter())
            throw new Exception($"Adapter non trovato per il modello: {Build.Model}");

        AidcManager.Create(
            Android.App.Application.Context,
            new AidcCreatedCallback(this, onDeviceReady)
        );
    }

    public partial void Deinit()
    {
        _barcodeReader?.Close();
        _barcodeReader = null;

        _aidcManager?.Close();
        _aidcManager = null;
    }

    public partial void OnKeyEvent(object keyEvent)
    {
        // Honeywell gestisce il trigger hardware internamente, non serve implementazione
    }

    public partial void ChangeSettings(List<(string Key, object Value)> settings)
    {
        if (_barcodeReader == null) return;

        var properties = GetDefaultProperties();

        foreach (var (key, value) in settings)
        {
            switch (key)
            {
                case "TRIGGER_SCAN_MODE":
                    var scanMode = (string)value switch
                    {
                        "ONE_SHOT"                     => BarcodeReader.TriggerScanModeOneshot,
                        "CONTINUOUS"                   => BarcodeReader.TriggerScanModeContinuous,
                        "READ_ON_SECOND_TRIGGER_PRESS" => BarcodeReader.TriggerScanModeReadOnSecondTriggerPress,
                        "READ_ON_RELEASE"              => BarcodeReader.TriggerScanModeReadOnRelease,
                        _                              => BarcodeReader.TriggerScanModeOneshot
                    };
                    properties[BarcodeReader.PropertyTriggerScanMode] = scanMode!;
                    break;

                case "TRIGGER_SCAN_DELAY":
                    properties[BarcodeReader.PropertyTriggerScanDelay] = int.Parse((string)value);
                    break;
            }
        }

        _barcodeReader.SetProperties(properties);

        try { _barcodeReader.Claim(); }
        catch (ScannerUnavailableException ex) { OnScanFailed?.Invoke(ex); }
    }

    private void RegisterListenersInternal()
    {
        _barcodeReader?.AddBarcodeListener(new BarcodeEventListener(this));
    }

    private void DispatchEvent(BarcodeReadEvent e)
    {
        var dto = new BarcodeScanDto
        {
            StringValue = e.BarcodeData,
            Type        = e.CodeId,
            Name        = e.CodeId
        };

        OnScanSuccessful?.Invoke(dto);
    }

    private static Dictionary<string, Java.Lang.Object> GetDefaultProperties() => new()
    {
        { BarcodeReader.PropertyEan8Enabled,                    true },
        { BarcodeReader.PropertyEan13Enabled,                   true },
        { BarcodeReader.PropertyCode39Enabled,                  true },
        { BarcodeReader.PropertyCode128Enabled,                 true },
        { BarcodeReader.PropertyGs1128Enabled,                  true },
        { BarcodeReader.PropertyUpcAEnable,                     true },
        { BarcodeReader.PropertyEan8CheckDigitTransmitEnabled,  true },
        { BarcodeReader.PropertyEan13CheckDigitTransmitEnabled, true },
        { BarcodeReader.PropertyUpcACheckDigitTransmitEnabled,  true }
    };

    private class AidcCreatedCallback(HoneywellScannerService service, Action onDeviceReady)
        : Java.Lang.Object, AidcManager.ICreatedCallback
    {
        public void OnCreated(AidcManager? manager)
        {
            service._aidcManager = manager;

            try
            {
                if (manager == null) throw new Exception("AidcManager null");
                
                service._barcodeReader = manager.CreateBarcodeReader();
                service.RegisterListenersInternal();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                
                service.OnScanFailed?.Invoke(ex);
            }

            onDeviceReady.Invoke();
        }
    }

    private class BarcodeEventListener(HoneywellScannerService service)
        : Java.Lang.Object, BarcodeReader.IBarcodeListener
    {
        public void OnBarcodeEvent(BarcodeReadEvent? e)   => service.DispatchEvent(e!);
        public void OnFailureEvent(BarcodeFailureEvent? e) =>
            service.OnScanFailed?.Invoke(new Exception($"Scan failure: {e}"));
    }
}