using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinRT.BxlComponent.Plugin.BxlMpXamarinSDK;
using WinRT.BxlComponent.Plugin.BxlMpXamarinSDK.Abstractions;
using Windows.Foundation;
using System.Collections.ObjectModel;
using Microsoft.ReactNative.Managed;
using System.IO;

namespace WinRT.BxlComponent.UwpWrapper
{
    [ReactModule]
    public sealed class MPosLookupProxy
    {
        public IAsyncOperation<int> refreshDeivcesList(int interfaceType, int timeout/*second*/)
        {
            MPosDebug.WriteLine("WinRT.BxlComponent.UwpWrapper.refreshDeivcesList");
            return Task.Run<int>(async () => { return await MposLookupImplementation.Instance.refreshDeivcesList(interfaceType, timeout); }).AsAsyncOperation();
        }
        public IList<WinRT.BxlComponent.UwpWrapper.MPosConnectionInformation> getDeviceList(int interfaceType)
        {
            MPosDebug.WriteLine("WinRT.BxlComponent.UwpWrapper.getDeviceList");
            ObservableCollection<Plugin.BxlMpXamarinSDK.Abstractions.MposConnectionInformation> list = null;
            list = MposLookupImplementation.Instance.getDeviceList(interfaceType);
            if(list == null)
                return null;
            var devConnecInfoList = new List<WinRT.BxlComponent.UwpWrapper.MPosConnectionInformation>();
            foreach (var item in list)
            {
                var o = new WinRT.BxlComponent.UwpWrapper.MPosConnectionInformation();
                o.IntefaceType      = (int)item.IntefaceType;
                o.Name              = item.Name;
                o.MacAddress        = item.MacAddress;
                o.IpAddress         = item.IpAddress;
                o.PortNumber        = item.PortNumber;
                o.BluetoohDeviceId  = item.BluetoohDeviceId;
                MPosDebug.WriteLine("WinRT.BxlComponent.UwpWrapper.getDeviceList : name = " + o.Name + " address = " + o.Address);
                devConnecInfoList.Add(o);
            }
            return devConnecInfoList;
        }

        [ReactMethod("getNetworkDevices")]
        public async void GetNetworkDevices(bool wireless, int searchTime /* second */, IReactPromise<string> promise)
        {
            //wireless = true;
            promise.Resolve(await BxlReactNativeSDKHelper.Instance.GetNetworkDevice(wireless, searchTime));
            return;
        }

        [ReactMethod("getBluetoothDevices")]
        public async void GetPairedDevice(IReactPromise<string> promise)
        {
            promise.Resolve(await BxlReactNativeSDKHelper.Instance.GetPairedDevice());
            return; 
        }


    }
    
}
