using Aitex.Core.RT.Event;
using Aitex.Core.RT.IOCore;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using MECF.Framework.Common.Gem;
using System;
using System.Xml;

namespace Aitex.Core.RT.Device.Devices
{
    public class IoCassette : BaseDevice, IDevice
    {
        private DIAccessor _di = null;

        public DIAccessor SensorDI => _di;

        private R_TRIG _trigArrive = new R_TRIG();

        private R_TRIG _trigLeave = new R_TRIG();

        public bool Value
        {
            get
            {
                if (_di != null)
                    return _di.Value;

                return false;
            }
        }

        public IoCassette(string module, XmlElement node, string ioModule = "")
        {
            var attrModule = node.GetAttribute("module");
            base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
            base.Name = node.GetAttribute("id");
            base.Display = node.GetAttribute("display");
            base.DeviceID = node.GetAttribute("schematicId");

            if (SC.GetStringValue($"System.InchType") == "Inch6")
            {
                _di = ParseDiNode("di6", node, ioModule);
            }
            else
            {
                _di = ParseDiNode("di8", node, ioModule);
            }
        }

        public bool Initialize()
        {
            return true;
        }

        public void Terminate()
        {
        }

        public void Monitor()
        {
            try
            {
                _trigLeave.CLK = !_di.Value;
                if (_trigLeave.Q)
                {
                    if(Module == "CassAL")
                    {
                        OP.DoOperation($"{Module}.DeleteAll", 1, 25);

                        EV.PostInfoLog(Module, "detected no signal DI_WaferCassetLeftInch Sensor,delete CassAL 1-25 Wafer ");
                    }
                    else if (Module == "CassAR")
                    {
                        OP.DoOperation($"{Module}.DeleteAll", 1, 25);

                        EV.PostInfoLog(Module, "detected no signal DI_WaferCassetRightInch Sensor,delete CassAR 1-25 Wafer ");
                    }
                    else if (Module == "CassBL")
                    {
                        OP.DoOperation($"{Module}.DeleteAll", 1, 8);

                        EV.PostInfoLog(Module, "detected no signal DI_TrayCassetInch Sensor,delete CassBL 1-8 Tray ");
                    }

                    GemManager.Instance.Equipment?.TriggerEvent("CarrierLeave", new string[] { "CarrierID" }, new object[] {Module});
                }

                _trigArrive.CLK = _di.Value;
                if (_trigArrive.Q)
                {
                    if (Module == "CassAL")
                    {
                        EV.PostInfoLog(Module, "CassetteAL Arrive");
                    }
                    else if (Module == "CassAR")
                    {
                        EV.PostInfoLog(Module, "CassetteAR Arrive");
                    }
                    else if (Module == "CassBL")
                    {
                        EV.PostInfoLog(Module, "CassetteBL Arrive");
                    }

                    GemManager.Instance.Equipment?.TriggerEvent("CarrierArrive", new string[] { "CarrierID" }, new object[] { Module });
                }
            }
            catch (Exception ex)
            {
                LOG.Write(ex);
            }
        }

        public void Reset()
        {
            _trigArrive.RST = true;
            _trigLeave.RST = true;
        }
    }
}
