using MECF.Framework.Common.CommonData;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace MECF.Framework.Common.Aitex.Core.RT.EMS
{
    /// <summary>
    /// 保养触发器相关信息;
    /// 此信息RT端保存于本地;
    /// 初始化时UI需要请求一次初始值
    /// </summary>
    [Serializable]
    [DataContract]
    public class MaintainerInfo: NotifiableItem, ICloneable
    {
        #region Properties

        /// <summary>
        /// 触发器类型
        /// </summary>
        [DataMember]
        public string Type { get; set; }

        public string ShortType => Type.Replace("Maintainer", "");

        /// <summary>
        /// 保养触发器名称(保证唯一性)
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// 保养触发器模组(保证唯一性)
        /// </summary>
        [DataMember]
        public string Module { get; set; }

        /// <summary>
        /// 触发器简称
        /// </summary>
        public string DisplayName => ShortType + ":" + Name + (Module == "" ? "" : "-" + Module);

        /// <summary>
        /// Config页面被选中
        /// </summary>
        public bool IsSelected { get; set; } = false;
        
        /// <summary>
        /// 保养触发器序号(保证唯一性)
        /// </summary>
        [DataMember]
        public int Index { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        [DataMember]
        public string Description { get; set; }

        /// <summary>
        /// 是否使能
        /// </summary>
        [DataMember]
        public bool Enable { get; set; }

        /// <summary>
        /// 是否超时
        /// </summary>
        [DataMember]
        public bool IsTrigger { get; set; }
        
        /// <summary>
        /// 是否计时中
        /// </summary>
        [DataMember]
        public bool IsProcessing { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        [DataMember]
        public DateTime StartDate { get; set; }

        /// <summary>
        /// 记录值
        /// </summary>
        [DataMember]
        public double Value { get; set; }

        /// <summary>
        /// 阈值
        /// </summary>
        [DataMember]
        public double Threshold { get; set; }

        /// <summary>
        /// 触发时间
        /// </summary>
        [DataMember]
        public DateTime? TrigDate { get; set; }

        /// <summary>
        /// 超过触发点的时间
        /// </summary>
        [DataMember]
        public double TimeExceed { get; set; }

        /// <summary>
        /// 时间单位
        /// </summary>
        [DataMember]
        public TimeUnit TimeDisplayUnit { get; set; }

        /// <summary>
        /// 剩余阈值量
        /// </summary>
        [DataMember]
        public double ValueElapsed { get; set; }

        /// <summary>
        /// 超出阈值量
        /// </summary>
        [DataMember]
        public double ValueExceed { get; set; }

        /// <summary>
        /// 周期内待保养项
        /// </summary>
        [DataMember]
        public double UnMaintainedCount { get; set; }

        /// <summary>
        /// 待保养项总数
        /// </summary>
        [DataMember]
        public double TotalCount { get; set; }

        /// <summary>
        /// 保养项信息集合
        /// </summary>
        [DataMember]
        public List<MaintainerItemInfo> MaintainerItemInfos { get; set; } = new();

        /// <summary>
        /// UI端重更新保养项旗标(保养配置变动时)
        /// </summary>
        [DataMember] 
        public DateTime ReShapeItemHandShake { get; set; } = DateTime.Now;

        #endregion

        #region Constructors
        public MaintainerInfo()
        {
            
        }
        public MaintainerInfo(List<MaintainerItemInfo> items, string name, string module, int index, string description, bool enabled, DateTime startdate, double threshold, TimeUnit timedisplayunit)
        {
            Name = name;
            Module = module;
            Index = index;
            Description = description;
            Enable = enabled;
            TimeDisplayUnit = timedisplayunit;
            Threshold = threshold;
            StartDate = startdate;
            MaintainerItemInfos = items;

        }

        public MaintainerInfo(List<MaintainerItemInfo> items, MaintainerInfo info, DateTime startdate, double threshold)
        {
            Name = info.Name;
            Module = info.Module;
            Index = info.Index;
            Description = info.Description;
            Enable = info.Enable;
            TimeDisplayUnit = info.TimeDisplayUnit;
            Threshold = info.Threshold;
            StartDate = info.StartDate;
            MaintainerItemInfos = items;
            TimeExceed = info.TimeExceed;
            ValueExceed = info.ValueExceed;
            ValueElapsed = info.ValueElapsed;
        }

        #endregion

        #region Function
        public bool EqualsTypeNameModule(string type, string name, string module)
        {
            return Type == type && Name == name && Module == module;
        }

        public void Update(MaintainerInfo newinfo)
        {
            Name = newinfo.Name;
            Type = newinfo.Type;
            Module = newinfo.Module;
            Index = newinfo.Index;
            Description = newinfo.Description;
            Enable = newinfo.Enable;
            StartDate = newinfo.StartDate;
            Threshold = newinfo.Threshold;
            IsTrigger = newinfo.IsTrigger;
            TrigDate = newinfo.TrigDate;
            TimeDisplayUnit = newinfo.TimeDisplayUnit;
            Value = newinfo.Value;
            TimeExceed = newinfo.TimeExceed;
            ValueElapsed = newinfo.ValueElapsed;
            ValueExceed = newinfo.ValueExceed;
            UnMaintainedCount = newinfo.UnMaintainedCount;
            TotalCount = newinfo.TotalCount;
            IsProcessing = newinfo.IsProcessing;

            if (ReShapeItemHandShake == newinfo.ReShapeItemHandShake)
            {
                foreach (var item in MaintainerItemInfos)
                {
                    var item2 = newinfo.MaintainerItemInfos.Find(i => i.UID == item.UID);
                    if (item2 != null)
                    {
                        item.Update(item2);
                    }
                }
            }
            else
            {
                ReShapeItemHandShake = newinfo.ReShapeItemHandShake;
                MaintainerItemInfos = newinfo.MaintainerItemInfos;
            }

            InvokePropertyChanged();
        }

        public object Clone()
        {
            return new MaintainerInfo()
            {
                Name = this.Name,
                Type = this.Type,
                Module = this.Module,
                Index = this.Index,
                Description = this.Description,
                Enable = this.Enable,
                StartDate = this.StartDate,
                Threshold = this.Threshold,
                TrigDate = this.TrigDate,
                TimeDisplayUnit = this.TimeDisplayUnit
            };
        }
        #endregion


    }
}
