using ProjectManagementSystem.Device.CasunBox;
using ProjectManagementSystem.Common.Config;
using ProjectManagementSystem.Common.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Common.Device
{
    public class BoxFactory
    {
        private static BoxControl m_instanceTcp;
        private static BoxControl m_instanceUdp;
        private static BoxControl m_instanceCom;

        public static BoxControl Init(EnumBoxCommunication enumBoxCommunication)
        {
            string ipAddress = Function.AppSetting.GetString("ListenIp");
            int port = Function.AppSetting.GetInt("ListenPort");

            string comName = Function.AppSetting.GetString("ComPortName");
            int baudRate = 9600;

            switch (enumBoxCommunication)
            {
                case EnumBoxCommunication.Tcp:
                    if (m_instanceTcp == null)
                    {
                        var boxCommunication = new BoxCommunicationTCP(ipAddress, port);
                        m_instanceTcp = new BoxControl(boxCommunication);
                    }
                    return m_instanceTcp;
                case EnumBoxCommunication.Udp:
                    if (m_instanceUdp == null)
                    {
                        var boxCommunication = new BoxCommunicationUDP(ipAddress, port);
                        m_instanceUdp = new BoxControl(boxCommunication);
                    }
                    return m_instanceUdp;
                case EnumBoxCommunication.Com:
                    if (m_instanceCom == null)
                    {
                        var boxCommunication = new BoxCommunicationCom(comName, baudRate);
                        m_instanceCom = new BoxControl(boxCommunication);
                    }
                    return m_instanceCom;
                default:
                    break;
            }

            return null;
        }

        public static BoxControl GetBoxControl(EnumBoxCommunication enumBoxCommunication)
        {
            switch (enumBoxCommunication)
            {
                case EnumBoxCommunication.Tcp:
                    return m_instanceTcp;
                case EnumBoxCommunication.Udp:
                    return m_instanceUdp;
                case EnumBoxCommunication.Com:
                    return m_instanceCom;
                default:
                    break;
            }

            return null;
        }

        public static IEnumerable<InfoBox> GetInfoBox()
        {
            IEnumerable<InfoBox> allInfoBox = null;

            Array values =  Enum.GetValues(typeof(EnumBoxCommunication));
            foreach (EnumBoxCommunication item in values)
            {
                switch (item)
                {
                    case EnumBoxCommunication.Tcp:
                        allInfoBox = m_instanceTcp?.Communication.InfoBoxDict.Values;
                        break;
                    case EnumBoxCommunication.Udp:
                        if(m_instanceUdp != null)
                        {
                            allInfoBox = allInfoBox == null ? m_instanceUdp.Communication.InfoBoxDict.Values
                                : m_instanceUdp.Communication.InfoBoxDict.Values.Concat(allInfoBox);
                        }
                        break;
                    case EnumBoxCommunication.Com:
                        if (m_instanceCom != null)
                        {
                            allInfoBox = allInfoBox == null ? m_instanceCom.Communication.InfoBoxDict.Values
                                : m_instanceCom.Communication.InfoBoxDict.Values.Concat(allInfoBox);
                        }
                        break;
                    default:
                        break;
                }
            }

            return allInfoBox;
        }
    }

    public enum EnumBoxCommunication
    {
        Tcp,
        Udp,
        Com
    }
}

