using Aitex.Core.RT.Device.Devices;
using System.Collections.Generic;

namespace Aitex.Core.RT.Device.PmDevices
{
    /// <summary>
    /// Recipe使用枚举设置模式时，气阀需要对应操作
    /// 当前对象包含阀DI，对应模式下操作气阀设置相应的状态
    /// </summary>
    public class ValveActionData
    {
        /// <summary>
        /// 气阀对应的PM
        /// </summary>
        public string Module { get; private set; }

        /// <summary>
        /// 气阀对应Device中的ID
        /// </summary>
        public string Id { get; private set; }

        /// <summary>
        /// 气阀名称
        /// </summary>
        public string Name { get; private set; }

        private readonly IValve _valve;

        /// <summary>
        /// 存储对应枚举中，需要设置气阀状态
        /// </summary>
        private readonly Dictionary<string, bool> _valveActionDic = new Dictionary<string, bool>();

        public ValveActionData(string module, string id, string name)
        {
            Module = module;
            Id = id;
            Name = name;
            _valve = DEVICE.GetDevice($"{Module}.{Name}") as IValve;
            System.Diagnostics.Debug.Assert(_valve != null,
                $"{Module} {Id} {Name} valve setting not valid : ValveActionData");
        }

        public void AddDictionary(string key, bool value)
        {
            if (_valveActionDic.ContainsKey(key))
                System.Diagnostics.Debug.Assert(false, $"{Module} {Id} {Name} Dictionary contains key:ValveActionData");
            else
                _valveActionDic.Add(key, value);
        }

        public bool SetValve(string module, out string reson)
        {
            if (_valveActionDic.TryGetValue(module, out bool value))
            {
                _valve.TurnValve(value, out reson);
                return true;
            }

            //System.Diagnostics.Debug.Assert(false, $"Valve not configuration:{Name}");
            reson = $"Valve no need to set:{Name}";
            return true;
        }
    }
}