using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Com.FirmLib.UI
{
    /// <summary>
    /// 监视参数
    /// </summary>
    public class WatchParameter
    {
        public WatchParameter()
        { }
        public WatchParameter(string parameterId)
        {
            this.Id = parameterId;
        }
        /// <summary>
        /// 关联父类
        /// </summary>
        public WatchParameters Parent { get; internal set; }
        /// 参数Id
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// 更新时间
        /// </summary>
        public string UpdateTime { get; set; }

        /// <summary>
        /// 时间是否有效【如果时间有效则使用相关参数】
        /// </summary>
        public bool ValidTime { get { return !string.IsNullOrEmpty(this.UpdateTime); } }
    }
    /// <summary>
    /// 监视参数集合
    /// </summary>
    public class WatchParameters  : KeyedCollection<string, WatchParameter>
    {
       
        
        #region 重写方法
        protected override string GetKeyForItem(WatchParameter item)
        {
            return item.Id;
        }
        protected override void ClearItems()
        {
            foreach (var item in this)
            {
                if (item != null)
                {
                    item.Parent = null;
                }

            }
            base.ClearItems();
        }
        protected override void InsertItem(int index, WatchParameter item)
        {
            base.InsertItem(index, item);
            item.Parent = this;
        }
        protected override void RemoveItem(int index)
        {
            if (this[index] != null)
            {
                this[index].Parent = null;
            }
            base.RemoveItem(index);
        }
        protected override void SetItem(int index, WatchParameter item)
        {

            base.SetItem(index, item);
            item.Parent = this;
        }

        public void AddRange(IEnumerable<WatchParameter> items)
        {
            foreach (var item in items)
            {
                this.CheckAdd(item);
            }
        }
        #endregion
        /// <summary>
        /// 是否是没有变化
        /// </summary>
        public bool IsEmpty { get { return this.Count == 0; } }
        /// <summary>
        /// 根据参数id获取修改时间字符串
        /// </summary>
        /// <param name="parameterId"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public bool SetParameterTime(string parameterId,string time)
        {
            var paramter = this.FirstOrDefault(p => p.Id == parameterId);
            if (paramter == null)
                return false;
            paramter.UpdateTime = time;
            return true;
        }
        public void SetAllParameterTime(string time)
        {
            foreach (var item in this)
            {
                item.UpdateTime = time;
            }
        }
        /// <summary>
        /// 合并参数
        /// </summary>
        /// <param name="parameters"></param>
        public void Merge(WatchParameters parameters)
        {
            foreach (var item in parameters)
            {
                this.CheckAdd(item);
            }
        }
        /// <summary>
        /// 获取相关参数
        /// </summary>
        /// <param name="parameterId"></param>
        /// <returns></returns>
        public WatchParameter GetParameter(string parameterId)
        {
            if (this.Contains(parameterId))
            {
                return this[parameterId];
            }
            return null;
        }

        public void AddAndUpdate(WatchParameter parameter)
        {
             if (this.Contains(GetKeyForItem(parameter)))
            {
                var existItem = this.Items.FirstOrDefault(w => GetKeyForItem(w) == GetKeyForItem(parameter));
                var index = this.IndexOf(existItem);
                this[index] = parameter;
            }
            else
            {
                this.Add(parameter);
            }
        }
        /// <summary>
        /// 检测并添加【如果没有则加入，并返回true,如果存在则返回false】
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public bool CheckAdd(WatchParameter parameter)
        {
            if (this.Contains(GetKeyForItem(parameter)))
            {
                return false;
            }
            else
            {
                this.Add(parameter);
                return true;
            }
        }
    }
}
