using Aitex.Core.RT.Device;
using Aitex.Core.RT.IOCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MECF.Framework.Simulator.Core
{
    public class SimulatorIoAO_AI : BaseDevice, IDevice
    {
        protected Dictionary<string, string> AO_AI = new Dictionary<string, string>();

        public SimulatorIoAO_AI(string module, XmlElement node, string ioModule) : base(module, node, ioModule)
        {
            InitializeIValve(node);
        }

        /// <summary>
        /// 根据XML配置创建气阀控制对象
        /// </summary>
        /// <param name="node"></param>
        protected virtual void InitializeIValve(XmlElement node)
        {
            XmlNodeList valveNodes = node.GetElementsByTagName("AO_AI");
            foreach (XmlElement xmlElement in valveNodes)
            {
                string aiName = xmlElement.GetAttribute("ai");
                string aoName = xmlElement.GetAttribute("ao");
                if (!AO_AI.ContainsKey(aiName) && !AO_AI.ContainsKey(aoName))
                    AO_AI.Add(aiName, aoName);

                var isDefaultOpen = xmlElement.GetAttribute("aoDefault");
                if (!String.IsNullOrWhiteSpace(isDefaultOpen))
                    IO.AO[$"{Module}.{aoName}"].Value = Convert.ToSingle(isDefaultOpen);
            }
        }

        protected override void HandleMonitor()
        {
            DoValueToDI();
        }

        protected void DoValueToDI()
        {
            foreach (var ai_ao in AO_AI)
            {
                IO.AI[$"{Module}.{ai_ao.Key}"].Value = IO.AO[$"{Module}.{ai_ao.Value}"].Value;
            }
        }

        public bool Initialize()
        {
            return true;
        }

        public void Reset()
        {

        }

        public void Terminate()
        {

        }
    }
}
