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

namespace MECF.Framework.Simulator.Core
{
    public class SimulatorIoDefault : BaseDevice, IDevice
    {
        protected Dictionary<string, object> IO_ValueList = new Dictionary<string, object>();
        private readonly Random _rnd = new Random();

        public SimulatorIoDefault(string module, XmlElement node, string ioModule = "") : base(module, node, ioModule)
        {
            InitializeIValve(node);
        }

        /// <summary>
        /// 根据XML配置创建IO对象初始默认值
        /// </summary>
        /// <param name="node"></param>
        protected virtual void InitializeIValve(XmlElement node)
        {
            XmlNodeList valveNodes = node.GetElementsByTagName("IODefault");
            foreach (XmlElement xmlElement in valveNodes)
            {
                string type = xmlElement.GetAttribute("type");
                string ioName = xmlElement.GetAttribute("ioName");
                string value = xmlElement.GetAttribute("default");
                switch (type)
                {
                    case "DI":
                        IO.DI[$"{Module}.{ioName}"].Value = Convert.ToBoolean(value);
                        break;

                    case "DO":
                        IO.DO[$"{Module}.{ioName}"].Value = Convert.ToBoolean(value);
                        break;

                    case "AI":
                        IO.AI[$"{Module}.{ioName}"].Value = Convert.ToSingle(value);
                        break;

                    case "AO":
                        IO.AO[$"{Module}.{ioName}"].Value = Convert.ToSingle(value);
                        break;
                }
            }
        }

        public bool Initialize()
        {
            return true;
        }

        public void Reset()
        {

        }

        public void Terminate()
        {

        }
    }
}
