using Aitex.Core.Common;
using Aitex.Core.RT.DataCenter;
using Aitex.Core.Util;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.IOCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CyberX8_RT.Devices.EFEM
{
    public class DummyDevice
    {
        #region 常量 
        private const string CASSETE_200_PRESENT = "Cassete200Present";
        private const string CASSETE_150_PRESENT = "Cassete150Present";
        private const string CASSETE_100_PRESENT = "Cassete100Present";
        #endregion

        #region 内部变量
        private bool _cassete200Present = false;
        private bool _cassete150Present = false;
        private bool _cassete100Present = false;
        /// <summary>
        /// 变量是否初始化字典
        /// </summary>
        private Dictionary<string, bool> _variableInitializeDic = new Dictionary<string, bool>();
        /// <summary>
        /// 
        /// </summary>
        private ModuleName _module;
        /// <summary>
        /// 尺寸
        /// </summary>
        private WaferSize _waferSize;
        /// <summary>
        /// 是否存在Cassete
        /// </summary>
        private bool _hasCassete;
        #endregion

        #region 属性
        /// <summary>
        /// 尺寸
        /// </summary>
        public WaferSize WaferSize { get { return _waferSize; } }
        /// <summary>
        /// 是否存在Cassete
        /// </summary>
        public bool HasCassette { get { return _hasCassete; } }
        #endregion
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="moduleName"></param>
        public DummyDevice(ModuleName moduleName)
        {
            _module = moduleName;
            SubscribeValueAction();
            DATA.Subscribe($"{_module}.CassettePlaced", () => _hasCassete, SubscriptionAttribute.FLAG.IgnoreSaveDB);
            DATA.Subscribe($"{_module}.WaferSize", () => WaferSize.ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
        }

        /// <summary>
        /// 订阅变量数值发生变化
        /// </summary>
        private void SubscribeValueAction()
        {
            BeckhoffIoSubscribeUpdateVariable(CASSETE_100_PRESENT);
            BeckhoffIoSubscribeUpdateVariable(CASSETE_150_PRESENT);
            BeckhoffIoSubscribeUpdateVariable(CASSETE_200_PRESENT);
        }

        /// <summary>
        /// 订阅IO变量
        /// </summary>
        /// <param name="variable"></param>
        private void BeckhoffIoSubscribeUpdateVariable(string variable)
        {
            _variableInitializeDic[variable] = false;
            IOModuleManager.Instance.SubscribeModuleVariable(_module.ToString(), variable, UpdateVariableValue);
        }

        /// 更新变量数值
        /// </summary>
        /// <param name="variable"></param>
        /// <param name="value"></param>
        private void UpdateVariableValue(string variable, object value)
        {
            bool cassete = (bool)value;
            if (variable == CASSETE_200_PRESENT)
            {
                _cassete200Present = cassete;
                if (cassete)
                {
                    _waferSize = WaferSize.WS8;
                }
            }
            else if (variable == CASSETE_150_PRESENT)
            {
                _cassete150Present = cassete;
                if (cassete&&!_cassete200Present)
                {
                    _waferSize = WaferSize.WS6;
                }
            }
            else if (variable == CASSETE_100_PRESENT)
            {
                _cassete100Present = cassete;
                if (cassete&&!_cassete200Present&&!_cassete150Present)
                {
                    _waferSize = WaferSize.WS4;
                }
            }
            if (!_cassete100Present && !_cassete150Present && !_cassete200Present)
            {
                _hasCassete = false;
            }
            else
            {
                _hasCassete = true;
            }
        }
    }
}