using System;

namespace MECF.Framework.Common.Aitex.Core.RT.EMS
{
    public class TimeMaintainer : Maintainer
    {
        #region Variables

        #endregion

        #region Properties

        #endregion

        #region Constructors
        public TimeMaintainer()
        {

        }

        public TimeMaintainer(MaintainerItemCollection item, string name, string module, int index, string description, bool enabled, DateTime startdate, double threshold, TimeUnit timedisplayunit)
            :base(item ,name, module, index, description, enabled, startdate, threshold, timedisplayunit)
        {
            _threshold = DateTimeHelper.Transform2Seconds(threshold,timedisplayunit);
        }

        public TimeMaintainer(MaintainerItemCollection item, MaintainerInfo info, DateTime startdate, double threshold)
            : base(item, info, startdate, threshold)
        {

        }

        #endregion

        #region Methods

        /// <summary>
        /// 计算剩余时间和超时时间，不同类算法不同，需要重载
        /// </summary>
        public virtual void Monitor()
        {
            base.Monitor();
            UpdateInfo();
        }

        public override void UpdateInfo()
        {
            _info.Value = DateTimeHelper.SecondsTransform(_value, _info.TimeDisplayUnit);
            _info.Threshold = DateTimeHelper.SecondsTransform(_threshold, _info.TimeDisplayUnit);
            _info.ValueExceed = DateTimeHelper.SecondsTransform(_valueExceed, _info.TimeDisplayUnit);
            _info.ValueElapsed = DateTimeHelper.SecondsTransform(_valueElapsed, _info.TimeDisplayUnit);

            base.UpdateInfo();
        }

        public virtual bool Reset()
        {
            UpdateInfo();
            return base.Reset();
        }

        /// <summary>
        /// 阈值设置
        /// </summary>
        /// <param name="threshold"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public override bool SetThreshold(double threshold, params object[] args)
        {
            if (!base.SetThreshold(threshold))
            {
                return false;
            }
            if (args.Length != 1)
            {
                return false;
            }
            if (args[0] is TimeUnit t)
            {
                _threshold = DateTimeHelper.Transform2Seconds(threshold, t);
                _info.TimeDisplayUnit = t;
                return true;
            }

            return false;
        }

        /// <summary>
        /// 设置开始时间
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public override bool SetStartDate(DateTime t)
        {
            if (!base.SetStartDate(t))
            {
                return false;
            }
            _startDate = t;
            return true;
        }
        #endregion
    }
}
