using Aitex.Core.RT.Event;
using System;

namespace MECF.Framework.Common.Aitex.Core.RT.EMS
{
    /// <summary>
    /// 监控系统时间，超时触发保养
    /// </summary>
    public class PresetMaintainer: TimeMaintainer
    {
        #region Variables


        #endregion

        #region Constructors
        public PresetMaintainer()
        {

        }

        public PresetMaintainer(MaintainerItemCollection item,
            string name,
            string module,
            int index,
            string description,
            bool enabled,
        DateTime startdate,
        double threshold,
            TimeUnit timeunit) : base(item,name, module,index ,description, enabled, startdate, threshold, timeunit)
        {
            _info.Type = nameof(PresetMaintainer);
            _getProcessing = ()=> true;
            CalculateEndDate();
        }

        public PresetMaintainer(MaintainerItemCollection item,
            MaintainerInfo info,
            DateTime startdate,
            double threshold) : base(item, info, startdate, threshold)
        {
            _info.Type = nameof(PresetMaintainer);
            _getProcessing = () => true;
            CalculateEndDate();
        }

        #endregion

        #region Methods

        public override void Monitor()
        {
            ////①确认未使能;没有维护项也不进行计算
            if (!_info.Enable || _itemCollection.Count == 0)
            {
                _isTimeOut = false;
                _timeExceed = 0;
                _valueElapsed = 0;
                _value = 0;
            }
            else
            {
                ////②计算时间
                DateTime now = DateTime.Now;
                _itemCollection.CheckAllItemMaintained(_startDate);

                if (_unMaintainedCount == 0)
                {
                    Reset();
                }
                else
                {
                    if (now.CompareTo((DateTime)_trigDate) > 0)
                    {
                        _isTimeOut = true;
                        _timeExceed = DateTimeHelper.DateTimeSubtraction((DateTime)_trigDate, now, TimeUnit.Seconds);
                        _valueExceed = _timeExceed;
                        _valueElapsed = 0;
                    }
                    else
                    {
                        _isTimeOut = false;
                        _timeExceed = 0;
                        _valueExceed = _timeExceed;
                        _valueElapsed = DateTimeHelper.DateTimeSubtraction(now, (DateTime)_trigDate, TimeUnit.Seconds);
                    }
                }
                _value = DateTimeHelper.DateTimeSubtraction(_startDate, now, TimeUnit.Seconds);
            }
            ////④确认超时上升沿
            base.Monitor();
        }

        public override bool Reset()
        {
            if (!_info.Enable)
            {
                EV.PostWarningLog("Maintenance", $"Plan {_info.Name} is disable,can not be reset");
                return false;
            }
            if (!CanReset())
            {
                return false;
            }

            _isTimeOut = false;
            DateTime now = DateTime.Now;
            _startDate = now;
            CalculateEndDate();

            _value = 0;
            _timeExceed = 0;
            _valueExceed = _timeExceed;
            _valueElapsed = DateTimeHelper.DateTimeSubtraction(now, (DateTime)_trigDate,TimeUnit.Seconds);
            _itemCollection.CheckAllItemMaintained(_startDate);

            return base.Reset();
        }

        protected void CalculateEndDate()
        {
            _trigDate = _startDate.AddSeconds(_threshold);
        }

        public override bool SetThreshold(double threshold, params object[] args)
        {
            if (!base.SetThreshold(threshold,args))
            {
                return false;
            }
            CalculateEndDate();
            return true;
        }

        public override bool SetStartDate(DateTime t)
        {
            if (!base.SetStartDate(t))
            {
                return false;
            }
            CalculateEndDate();
            return true;
        }

        #endregion
    }
}
