using Aitex.Core.Common.DeviceData;
using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.IOCore;
using System;
using System.Xml;

using Aitex.Core.Util;
using Aitex.Core.RT.Log;

namespace Aitex.Core.RT.Device.Devices
{
    class IoWaterFlow : BaseDevice, IDevice
    {
        private AIAccessor _aiFeedBack = null;
        private DIAccessor _diFlowSW = null;

        private bool _isFloatAioType;
        private R_TRIG _trigTextOut = new R_TRIG();

        private string _warningText;
        private string _alarmText;
        private string _infoText;

        public AITDeviceData DeviceData
        {
            get 
            {
                AITDeviceData data = new AITDeviceData()
                {
                    Module = Module,
                    DeviceName = Name,
                    DisplayName = Display,
                    DeviceSchematicId = DeviceID,
                    UniqueName = UniqueName,
                    
                };
                data.AttrValue["FeedBack"] = FeedBack;
                data.AttrValue["FlowSW"] = FlowSW;
                return data;
            }
        }
        public float FeedBack
        {
            get
            {
                if (_aiFeedBack != null)
                    return _isFloatAioType ? _aiFeedBack.Value : _aiFeedBack.Value;

                return 0;
            }
        }
        public bool FlowSW
        {
            get
            {
                if (_diFlowSW != null)
                    return _diFlowSW.Value;

                return false;
            }
        }
        public IoWaterFlow(string module, XmlElement node, string ioModule = "") : base (module, node, ioModule)
        {
            _isFloatAioType = !string.IsNullOrEmpty(node.GetAttribute("aioType")) && (node.GetAttribute("aioType") == "float");

            _aiFeedBack = ParseAiNode("aiFeedback", node, ioModule);
            _diFlowSW = ParseDiNode("diFlowSW", node, ioModule);

            _infoText = "";
            _warningText = "";
            _alarmText = "";
            //if (base.Name == "TMPump2FlowTemp")
            //{
            //    _alarmText = "Alarm9  TMPump2 Water Low Flow -U6  [DI-19]";
            //}
        }
        public bool Initialize()
        {
            DATA.Subscribe($"{Module}.{Display}.DeviceData", () => DeviceData);
            DATA.Subscribe($"{Module}.{Display}.FeedBack", () => FeedBack);
            DATA.Subscribe($"{Module}.{Display}.FlowSW", () => FlowSW);
            return false;
        }

        protected override void HandleMonitor()
        {
            try
            {
                //if (base.Name == "TMPump2FlowTemp")//DI-19
                //{
                //    _trigTextOut.CLK = _diFlowSW.Value;

                //    if (_trigTextOut.Q)
                //    {
                //        if (!string.IsNullOrEmpty(_warningText.Trim()))
                //        {
                //            EV.PostWarningLog(Module, _warningText);
                //        }
                //        else if (!string.IsNullOrEmpty(_alarmText.Trim()))
                //        {
                //            EV.PostAlarmLog(Module, _alarmText);
                //        }
                //        else if (!string.IsNullOrEmpty(_infoText.Trim()))
                //        {
                //            EV.PostInfoLog(Module, _infoText);
                //        }
                //    }
                //}
              
            }
            catch (Exception ex)
            {
                LOG.Write(ex);

            }
        }

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

        public void Terminate()
        {
            
        }
    }
}
