using NEIntelligentControl2.Models.Datas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NEIntelligentControl2.Models.AGC
{
    /// <summary>
    /// AGC数据标签
    /// </summary>
    public class TagInfo
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        private string tag;
        /// <summary>
        /// 标签
        /// </summary>
        public string Tag { get => tag; set { tag = value; Tags = tag?.Split(','); } }
        /// <summary>
        /// 倍率
        /// </summary>
        public float Multiplier { get; set; } = 1;
        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; }
        /// <summary>
        /// 类型
        /// </summary>
        public TagType Type { get; set; }
        /// <summary>
        /// 标签们
        /// </summary>
        public string[] Tags { get; set; }
        /// <summary>
        /// 时间戳
        /// </summary>
        public long Ts { get; set; }

        /// <summary>
        /// 值改变
        /// </summary>
        public Action<double> ValueChanged { get; internal set; }
        /// <summary>
        /// 值
        /// </summary>
        private double value;
        public double Value { get { return value; } set { this.value = value * Multiplier; ValueChanged?.Invoke(this.value); } }
        /// <summary>
        /// 数据历史值改变
        /// </summary>
        public Action<TagInfo, List<TsData>> HistoryValueChanged { get; set; }
        /// <summary>
        /// 更新数据
        /// </summary>
        internal void UpdateData(Dictionary<string, TsData> vs)
        {
            if (vs == null || this.Tags == null) return;
            double d = 0;
            foreach (var v in this.Tags)
            {
                if (!vs.ContainsKey(v)) continue;
                var td = vs[v];
                d += td.Value;
                Ts = td.Ts;
            }
            this.Value = d;
        }
        /// <summary>
        /// 更新历史数据
        /// </summary>
        internal void UpdateHistoryData(List<TsData> dts)
        {
            HistoryValueChanged?.Invoke(this, dts);
        }
    }
}
