using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.IOCore;
using System.Threading.Tasks;
using System.Xml;
using Aitex.Core.RT.OperationCenter;

namespace Aitex.Core.RT.Device.Devices
{
    public class IoPlcProcessAbort : BaseDevice, IDevice
    {

        

        protected readonly DIAccessor _diAbortProcessRun;

        protected readonly DOAccessor _doAbortProcessInterrupt;

        protected readonly DOAccessor _doAbortProcessBuzzerOFF;

        public IoPlcProcessAbort(string module, XmlElement node, string ioModule = "") : base(module, node, ioModule)
        {

            _diAbortProcessRun = ParseDiNode("diAbortProcessRun", node, ioModule);

            _doAbortProcessInterrupt = ParseDoNode("doAbortProcessInterrupt", node, ioModule);

            _doAbortProcessBuzzerOFF = ParseDoNode("doAbortProcessBuzzerOFF", node, ioModule);

        }



        public bool Initialize()
        {

            DATA.Subscribe($"{Module}.AbortProcessRun", () => _diAbortProcessRun.Value);

            OP.Subscribe($"{Module}.AbortProcessInterrupt", (function, args) =>
            {
                _doAbortProcessInterrupt?.SetValue(true, out string reason);
                Task.Delay(2000).ContinueWith(a =>
                {
                    _doAbortProcessInterrupt?.SetValue(false, out string reason);
                });
                return true;
            });

            OP.Subscribe($"{Module}.AbortProcessBuzzerOFF", (function, args) =>
            {
                _doAbortProcessBuzzerOFF?.SetValue(true, out string reason);
                Task.Delay(2000).ContinueWith(a =>
                {
                    _doAbortProcessBuzzerOFF?.SetValue(false, out string reason);
                });
                return true;
            });
            return true;
        }

        public void Terminate()
        {
            
        }

        public void Reset()
        {
            
        }
    }
}
