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

namespace NEIntelligentControl2.Models.PV
{
    /// <summary>
    /// 光伏标签点信息
    /// </summary>
    public class PVTagInfo
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        private string code;
        /// <summary>
        /// UniformCode
        /// </summary>
        public string Code { get => code; set { code = value; Codes = code.Split(','); } }
        /// <summary>
        /// UniformCode们
        /// </summary>
        public string[] Codes { get; set; }
        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; }
        /// <summary>
        /// 是否忽视此标签点
        /// </summary>
        public bool IsIgnore { get; set; }
        /// <summary>
        /// 标签类型
        /// </summary>
        public PVTagType Type { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        private double value;
        public double Value { get { return value; } set { this.value = value; ValueChanged?.Invoke(this.value); } }
        /// <summary>
        /// 值改变
        /// </summary>
        public Action<double> ValueChanged { get; set; }
        /// <summary>
        /// 数据历史值改变
        /// </summary>
        public Action<PVTagInfo, List<TsData>> HistoryValueChanged { get; set; }

        internal void UpdateValue(Dictionary<string, TsData> vs)
        {
            if (vs == null || this.Code == null) return;
            double d = 0;
            foreach (var v in Codes)
            {
                if (!vs.ContainsKey(v)) continue;
                d += vs[v].Value;
            }
            this.Value = d;
        }

        /// <summary>
        /// 更新历史数据
        /// </summary>
        internal void UpdateHistoryData(List<TsData> vs)
        {
            HistoryValueChanged?.Invoke(this, vs);
        }
    }


    /// <summary>
    /// 光伏标签类型
    /// </summary>
    public enum PVTagType
    {
        /// <summary>
        /// 其他（默认）
        /// </summary>
        Other,
        /// <summary>
        /// 温度
        /// </summary>
        Temperature,
        /// <summary>
        /// 功率
        /// </summary>
        Power,
        /// <summary>
        /// 光照强度
        /// </summary>
        Sunshine,
    }
}
