using Microsoft.ReactNative.Managed;
using WinRT.BxlComponent.Plugin.BxlMpXamarinSDK;
using WinRT.BxlComponent.Plugin.BxlMpXamarinSDK.Abstractions;
using System;
using Windows.UI.Core;
using System.Runtime.InteropServices.WindowsRuntime;
using Plugin.BxlMAUISDK.PlatformImplementations;

namespace ReactNativeRnBixolonBgateLibs.UWPProxy
{
    [ReactModule]
    public sealed partial class MPosControllerNFCProxy
    {
        private MPosControllerNFCImplementation _device;
        private static int _uniqueDeviceId = 256;

        //[ReactEvent("PrinterDataEvent")]
        //public Action<byte[]> dataEvent { get; set; }

        [ReactEvent("NFCDataEvent")]
        public Action<int, byte[]> dataEvent { get; set; }

        public MPosControllerNFCProxy()
        {
            _device = new MPosControllerNFCImplementation((int)MPosDeviceType.MPOS_DEVICE_NFC, _uniqueDeviceId++);
        }
       
        [ReactMethod("selectInterface")]
        public void selectInterface(string[] array, IReactPromise<int> promise)
        {
            promise.Resolve(_device.selectInterface(
                Convert.ToInt32(array[0]),
                array[1])
                );
            return;
        }

        [ReactMethod("openService")]
        public async void openService(uint[] array, IReactPromise<int> promise)
        {
            _device.dataEvent2 += dataEvent;
            promise.Resolve(await _device.openService(
                array[0],
                array[1]
                ));
            return;
        }
        [ReactMethod("closeService")]
        public async void closeService(uint closeTimeout/*second*/, IReactPromise<int> promise)
        {
            _device.dataEvent2 -= dataEvent;
            promise.Resolve(await _device.closeService(closeTimeout));
            return;
        }
        [ReactMethod("selectCommandMode")]
        public void selectCommandMode(int commandMode, IReactPromise<int> promise)
        {
            promise.Resolve(_device.selectCommandMode(commandMode));
            return;
        }
        [ReactMethod("isOpen")]
        public void isOpen(IReactPromise<int> promise)
        {
            promise.Resolve(Convert.ToInt32(_device.isOpen()));
            return;
        }
        [ReactMethod("getDeviceId")]
        public void getDeviceId(IReactPromise<int> promise)
        {
            promise.Resolve(Convert.ToInt32(_device.DeviceID));
            return;
        }
        [ReactMethod("directIO")]
        public async void directIO([ReadOnlyArray()] byte[] data, IReactPromise<int> promise)
        {
            promise.Resolve(await _device.directIO(data));
            return;
        }
    }
}
