using System;
using System.Linq;
using WinRT.BxlComponent.Plugin.BxlMpXamarinSDK.Abstractions;
using System.Collections.Generic;

namespace WinRT.BxlComponent.Plugin.BxlMpXamarinSDK
{
    internal interface IMPosConnectionBuilder
    {
        IMPosConnection Build(MPosInterfaceType connectionType, string connectionInfo, int deviceId, int commandMode);
    }
    /// <summary>
    /// Implementation for Feature
    /// </summary>
    internal class MPosConnectionBuilderImplementation //: IMPosConnectionBuilder
    {
        internal class MPosDeviceOpenCloseInfo
        {
            //public bool IsOpen { get; set; } = false;
            public int DeviceId { get; set; } = -1;
        }

        static IMPosConnection _connection = null;
        static IMPosConnection _bGateConnection = null;
        private static List<MPosDeviceOpenCloseInfo> _DeviceList = new List<MPosDeviceOpenCloseInfo>();
        public static void AddDevice(int id)
        {
            _DeviceList.RemoveAll(x => x.DeviceId == id);
            _DeviceList.Add(new MPosDeviceOpenCloseInfo() { DeviceId = id});
            MPosDebug.WriteLine("ConnectionBuilderImplementation AddDevice : _DeviceList.Count = " + _DeviceList.Count);
        }
        public static void RemoveDevice(int id)
        {
            _DeviceList.RemoveAll(x => x.DeviceId == id);
            if(_DeviceList.Count <= 0){
                MPosDebug.WriteLine("ConnectionBuilderImplementation RemoveDevice : _DeviceList.Count = " + _DeviceList.Count);
                _bGateConnection = null;
            }
        }
        public static int GetDeviceCount()
        {
            MPosDebug.WriteLine("ConnectionBuilderImplementation GetDeviceCount : _DeviceList.Count = " + _DeviceList.Count);
            return _DeviceList.Count;
        }
        public static IMPosConnection Build(MPosInterfaceType connectionType, string connectionInfo, int commandMode)
        {
            MPosDebug.WriteLine("ConnectionBuilderImplementation Build : START");

            bool isMember = Enum.GetValues(typeof(MPosInterfaceType)).Cast<int>().Any(x => x == (int)connectionType);

            if (isMember != true){
                MPosDebug.WriteLine("ConnectionBuilderImplementation Build : NOT MPosInterfaceType Member");
                MPosDebug.WriteLine("ConnectionBuilderImplementation Build : END");
                return null;
            }

            IMPosConnection connection = null;

            switch (connectionType)
            {
                case MPosInterfaceType.MPOS_INTERFACE_WIFI:
                case MPosInterfaceType.MPOS_INTERFACE_ETHERNET:
                case MPosInterfaceType.MPOS_INTERFACE_BLUETOOTH:
                    switch ((MPosCommandMode)commandMode)
                    {
                        case MPosCommandMode.MPOS_COMMAND_MODE_DIRECT:
                            if(_connection == null) {
                                if (!string.IsNullOrEmpty(connectionInfo)) {
                                    connection = new MPosConnectionImplementation(connectionType, connectionInfo, commandMode);
                                    _connection = connection;
                                }
                            }
                            else if (_connection._ConnectionInfo == connectionInfo)
                            {
                                MPosDebug.WriteLine("ConnectionBuilderImplementation Build : _byPassConnection");
                                connection = _connection;
                            }
                            break;
                        case MPosCommandMode.MPOS_COMMAND_MODE_BGATE:
                            if (_bGateConnection == null){
                                if (!string.IsNullOrEmpty(connectionInfo))
                                    _bGateConnection = new MPosConnectionImplementation(connectionType, connectionInfo, commandMode);
                            }
                            MPosDebug.WriteLine("ConnectionBuilderImplementation Build : _bGateConnection");
                            connection = _bGateConnection;
                            break;
                    }
                    break;
            }

            MPosDebug.WriteLine("ConnectionBuilderImplementation Build : END");
            return connection;
        }
    }
}
