using System;
using System.Collections.Generic;
using System.Linq;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using MECF.Framework.Common.Communications;
using MECF.Framework.Common.Device.Bases;
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;

namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.VCE.SiasunVCE
{
    public class SiasunVCE : VCEBase, IConnection
    {
        public string Address { get { return _address; } }
        public override bool IsConnected => _connection.IsConnected;
        public bool Connect()
        {
            return true;
        }

        public bool Disconnect()
        {
            return true;
        }

        public string PortStatus { get; set; } = "Closed";

        private SiasunVCEConnection _connection;
        public SiasunVCEConnection Connection
        {
            get { return _connection; }
        }

        private R_TRIG _trigError = new R_TRIG();

        private R_TRIG _trigCommunicationError = new R_TRIG();
        private R_TRIG _trigRetryConnect = new R_TRIG();

        private PeriodicJob _thread;

        private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
        private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();

        public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();


        private object _locker = new object();

        private bool _enableLog;
        private string _scRoot;
        private string _portName;
        public SiasunVCE(string module, string name, string scRoot) : base(module, name)
        {
            _scRoot = scRoot;
        }

        private void ResetPropertiesAndResponses()
        {
            DoorClosed = null;
            DoorOpened = null;
            IsHalted = null;

            foreach (var ioResponse in IOResponseList)
            {
                ioResponse.ResonseContent = null;
                ioResponse.ResonseRecievedTime = DateTime.Now;
            }
        }

        private string _address = "00";
        public override bool Initialize()
        {
            base.Initialize();

            ResetPropertiesAndResponses();

            if (_connection != null && _connection.IsConnected)
                return true;

            if (string.IsNullOrEmpty(_scRoot))
            {
                _portName = SC.GetStringValue($"{Name}.DeviceAddress");
                _enableLog = SC.GetValue<bool>($"{Name}.EnableLogMessage");
            }
            else
            {
                _portName = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.DeviceAddress");
                _enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
            }

            _connection = new SiasunVCEConnection(_portName);
            _connection.EnableLog(_enableLog);

            if (_connection.Connect())
            {
                PortStatus = "Open";
                EV.PostInfoLog(Module, $"{Module}.{Name} connected");
            }

            _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);

            return true;
        }

        private bool OnTimer()
        {
            try
            {
                //return true;
                //_connection.MonitorTimeout();

                if (!_connection.IsConnected || _connection.IsCommunicationError)
                {
                    lock (_locker)
                    {
                        _lstHandler.Clear();
                    }

                    _trigRetryConnect.CLK = !_connection.IsConnected;
                    if (_trigRetryConnect.Q)
                    {
                        _connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
                        if (!_connection.Connect())
                        {
                            EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
                        }
                        else
                        {
                            //_lstHandler.AddLast(new SiasunVCEQueryPinHandler(this, _deviceAddress));
                            //_lstHandler.AddLast(new SiasunVCESetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
                        }
                    }
                    return true;
                }

                HandlerBase handler = null;
                if (!_connection.IsBusy)
                {
                    lock (_locker)
                    {
                        if (_lstHandler.Count == 0)
                        {
                            foreach (var monitorHandler in _lstMonitorHandler)
                            {
                                _lstHandler.AddLast(monitorHandler);
                            }

                        }

                        if (_lstHandler.Count > 0)
                        {
                            handler = _lstHandler.First.Value;

                            _lstHandler.RemoveFirst();
                        }
                    }

                    if (handler != null)
                    {
                        _connection.Execute(handler);
                    }
                }
            }
            catch (Exception ex)
            {
                LOG.Write(ex);
            }

            return true;
        }

        internal void NoteCommonActionResult(string command)
        {
            if (command == "JD")
            {

            }
            else if (command == "JU")
            {

            }
            else if (command == "LOAD")
            {
                LoadCompleted = true;
            }
            else if (command == "LP")
            {
                LoadPosition = true;
            }
            else if (command == "PI")
            {
                RetractPosition = true;
            }
            else if (command == "PICK")
            {
                Picked = true;
            }
            else if (command == "PLACE")
            {
                Placed = true;
            }
            else if (command == "PO")
            {
                ExtendPosition = true;
            }
            else if (command == "UNLOAD")
            {
                Unloaded = true;
            }
        }

        internal void NoteZAxisPosition(string pos)
        {
            ZAxisPosition = pos;
        }

        internal void NoteHomed(bool homed)
        {
            Homed = homed;
        }

        internal void NoteMoveResult(string position)
        {
            MovedPosition = position;
        }

        public override void Monitor()
        {
            try
            {
                //_connection.EnableLog(_enableLog);


                _trigCommunicationError.CLK = _connection.IsCommunicationError;
                if (_trigCommunicationError.Q)
                {
                    EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
                }
            }
            catch (Exception ex)
            {
                LOG.Write(ex);
            }

        }



        public override void Reset()
        {
            _trigError.RST = true;

            _connection.SetCommunicationError(false, "");
            _trigCommunicationError.RST = true;

            //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");

            _trigRetryConnect.RST = true;

            base.Reset();
        }

        public override bool Home(out string reason)
        {
            return base.Home(out reason);
        }


        #region Command Functions

        public void PerformRawCommand(string commandType)
        {
            lock (_locker)
            {
                _lstHandler.AddLast(new SiasunVCERawCommandHandler(this, commandType, ""));
            }
        }
        public void PerformRawCommand(string commandType, string command)
        {
            lock (_locker)
            {
                _lstHandler.AddLast(new SiasunVCERawCommandHandler(this, commandType, command));
            }
        }

        internal void NoteCurrentCommunicationMode(string data)
        {
            CurrentCommunicationMode = data;
        }

        public void PerformRawCommand(string commandType, string command, string comandArgument)
        {
            lock (_locker)
            {
                _lstHandler.AddLast(new SiasunVCERawCommandHandler(this, commandType, command, comandArgument));
            }
        }



        public void MonitorRawCommand(bool isSelected, string commandType, string command, string comandArgument)
        {
            lock (_locker)
            {
                string msg = comandArgument == null ? $"{command}\r" : $"{command} {comandArgument}\r";//??

                var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(SiasunVCERawCommandHandler) && ((SiasunVCERawCommandHandler)handler).IsSendText(commandType, command, comandArgument));

                if (isSelected)
                {
                    if (!existHandlers.Any())
                        _lstMonitorHandler.AddFirst(new SiasunVCERawCommandHandler(this, commandType, command, comandArgument));
                }
                else
                {
                    if (existHandlers.Any())
                    {
                        _lstMonitorHandler.Remove(existHandlers.First());
                    }
                }
            }
        }

        internal void NoteCurrentACEStatus(string data)
        {
            throw new NotImplementedException();
        }

        internal void NoteCurrentErrorStatus(string data)
        {
            CurrentErrorStatus = data;
        }

        internal void NoteIsLoadPositon(string data)
        {
            InLoadPositon = data.Last() == 'Y';
        }

        public void RequestArmRPositon(bool isSelected, string axisPosition)
        {
            lock (_locker)
            {
                var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestArmRPositonHandler) && handler.SendText.Contains(axisPosition));
                if (isSelected)
                {
                    if (!existHandlers.Any())
                        _lstMonitorHandler.AddFirst(new BrooksACERequestArmRPositonHandler(this, axisPosition));
                }
                else
                {
                    if (existHandlers.Any())
                    {
                        _lstMonitorHandler.Remove(existHandlers.First());
                    }
                }
            }
        }

        internal void NoteCurrentMappingInfo(string data)
        {
            CurrentMappingInfo = data;
        }

        internal void NoteIsPlatformPosition(string data)
        {
            InPlatformPosition = data.Last() == 'Y';
        }

        public void RequestArmZPositon(bool isSelected, string axisPosition)
        {
            lock (_locker)
            {
                var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestArmZPositonHandler) && handler.SendText.Contains(axisPosition));
                if (isSelected)
                {
                    if (!existHandlers.Any())
                        _lstMonitorHandler.AddFirst(new BrooksACERequestArmZPositonHandler(this, axisPosition));
                }
                else
                {
                    if (existHandlers.Any())
                    {
                        _lstMonitorHandler.Remove(existHandlers.First());
                    }
                }
            }
        }

        public void RequestCommunicationMode(bool isSelected)
        {
            lock (_locker)
            {
                var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestCommModeHandler));
                if (isSelected)
                {
                    if (!existHandlers.Any())
                        _lstMonitorHandler.AddFirst(new BrooksACERequestCommModeHandler(this));
                }
                else
                {
                    if (existHandlers.Any())
                    {
                        _lstMonitorHandler.Remove(existHandlers.First());
                    }
                }
            }
        }

        public void RequestErrorStatus(bool isSelected)
        {
            lock (_locker)
            {
                var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestErrorStatusHandler));
                if (isSelected)
                {
                    if (!existHandlers.Any())
                        _lstMonitorHandler.AddFirst(new BrooksACERequestErrorStatusHandler(this));
                }
                else
                {
                    if (existHandlers.Any())
                    {
                        _lstMonitorHandler.Remove(existHandlers.First());
                    }
                }
            }
        }
        public void RequestVCEStatus(bool isSelected)
        {
            lock (_locker)
            {
                var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestVCEStatusHandler));
                if (isSelected)
                {
                    if (!existHandlers.Any())
                        _lstMonitorHandler.AddFirst(new BrooksACERequestVCEStatusHandler(this));
                }
                else
                {
                    if (existHandlers.Any())
                    {
                        _lstMonitorHandler.Remove(existHandlers.First());
                    }
                }
            }
        }

        public void RequestLoadPositon(bool isSelected)
        {
            lock (_locker)
            {
                var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestLoadPositionHandler));
                if (isSelected)
                {
                    if (!existHandlers.Any())
                        _lstMonitorHandler.AddFirst(new BrooksACERequestLoadPositionHandler(this));
                }
                else
                {
                    if (existHandlers.Any())
                    {
                        _lstMonitorHandler.Remove(existHandlers.First());
                    }
                }
            }
        }


        public void RequestMappingInfo(bool isSelected)
        {
            lock (_locker)
            {
                var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestMappingInfoHandler));
                if (isSelected)
                {
                    if (!existHandlers.Any())
                        _lstMonitorHandler.AddFirst(new BrooksACERequestMappingInfoHandler(this));
                }
                else
                {
                    if (existHandlers.Any())
                    {
                        _lstMonitorHandler.Remove(existHandlers.First());
                    }
                }
            }
        }


        public void RequestPlatformPosition(bool isSelected)
        {
            lock (_locker)
            {
                var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestPlatformPositionHandler));
                if (isSelected)
                {
                    if (!existHandlers.Any())
                        _lstMonitorHandler.AddFirst(new BrooksACERequestPlatformPositionHandler(this));
                }
                else
                {
                    if (existHandlers.Any())
                    {
                        _lstMonitorHandler.Remove(existHandlers.First());
                    }
                }
            }
        }

 

        public void CloseDoor()
        {
            lock (_locker)
            {
                _lstHandler.AddLast(new SiasunVCECloseDoorHandler(this));
            }
        }
        public void OpenDoor()
        {
            lock (_locker)
            {
                _lstHandler.AddLast(new SiasunVCEOpenDoorHandler(this));
            }
        }

        public void Load()
        {
            lock (_locker)
            {
                //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "LOAD"));
            }
        }

        public void MoveToLoadPositon()
        {
            lock (_locker)
            {
                _lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "LP"));
            }
        }
        public void MapCassette()
        {
            lock (_locker)
            {
                _lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "MP"));
            }
        }
        public void Move(string axis, string type, string value)
        {
            lock (_locker)
            {
                _lstHandler.AddLast(new SiasunVCEMoveHandler(this, axis, type, value));
            }
        }
        public void RoateRAxisToRetractPosition()
        {
            lock (_locker)
            {
                //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "PI"));
            }
        }
        public void RoateRAxisToExtendPosition()
        {
            lock (_locker)
            {
                //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "PO"));
            }
        }
        public void Pick()
        {
            lock (_locker)
            {
                //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "PICK"));
            }
        }
        public void Place()
        {
            lock (_locker)
            {
                //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "PLACE"));
            }
        }
        public void Unload()
        {
            lock (_locker)
            {
                //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "UNLOAD"));
            }
        }
        public void GotoZ(int slot)
        {
            lock (_locker)
            {
                _lstHandler.AddLast(new SiasunVCEGotoZHandler(this, slot));
            }
        }
        public void Home()
        {
            lock (_locker)
            {
                _lstHandler.AddLast(new SiasunVCEHomeHandler(this));
            }
        }

        public void SetCommunicationMode(string commMode)
        {
            lock (_locker)
            {
                //_lstHandler.AddLast(new SiasunVCESetCommunicationModeHandler(this, commMode));
            }
        }


        #endregion

        #region Properties

        public string DoorClosed { get; private set; }
        public string DoorOpened { get; private set; }
        public string IsHalted { get; private set; }

        public string Error { get; private set; }
        public string ArmRPosition { get; private set; }
        public string CommunicationMode { get; private set; }
        public string ZAxisPosition { get; private set; }
        public bool Homed { get; private set; }
        public bool LoadCompleted { get; private set; }
        public bool LoadPosition { get; private set; }
        public bool RetractPosition { get; private set; }

        public bool Picked { get; private set; }
        public bool Placed { get; private set; }
        public bool ExtendPosition { get; private set; }
        public bool Unloaded { get; private set; }
        public string MovedPosition { get; private set; }
        public string ArmZPosition { get; private set; }
        public string CurrentCommunicationMode { get; private set; }
        public string CurrentErrorStatus { get; private set; }
        public bool InLoadPositon { get; private set; }
        public string CurrentMappingInfo { get; private set; }
        public bool InPlatformPosition { get; private set; }

        #endregion


        #region Note Functions
        private R_TRIG _trigWarningMessage = new R_TRIG();

        public void NoteError(string reason)
        {
            if (reason != null)
            {
                _trigWarningMessage.CLK = true;
                if (_trigWarningMessage.Q)
                {
                    EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
                }
                Error = reason;
            }
            else
            {
                Error = null;
            }
        }

        internal void NoteDoorClosed(bool isClosed)
        {
            DoorClosed = isClosed ? "true" : "false";
            DoorOpened = isClosed ? "false" : "true";
        }

        internal void NoteIsHalted(bool isHalted)
        {
            IsHalted = isHalted ? "true" : "false";
        }

        internal void NoteRawCommandInfo(string commandType, string command, string data)
        {
            var curIOResponse = IOResponseList.Find(res => res.SourceCommandName == command);
            if (curIOResponse != null)
            {
                IOResponseList.Remove(curIOResponse);
            }
            IOResponseList.Add(new IOResponse() { SourceCommandType = commandType, SourceCommand = command, ResonseContent = data, ResonseRecievedTime = DateTime.Now });
        }

        internal void NoteArmRPositon(string armRPosition)
        {
            ArmRPosition = armRPosition;
        }
        internal void NoteArmZPositon(string armPosition)
        {
            ArmZPosition = armPosition;
        }
        internal void NoteCommunicationMode(string mode)
        {
            CommunicationMode = mode;
        }

        #endregion
    }


}
