using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Misc;

namespace FLY.FeedbackRenZiJia.Server
{
    /// <summary>
    /// 记录加热变化
    /// 给加热 附加 时间 与 加热序号
    /// 只有2个对外接口, SetHeats, GetHeats 
    /// </summary>
    public class HeatChanged : INotifyPropertyChanged
    {
        private int channelcnt = 44;
        /// <summary>
        /// 总是有第1个记录
        /// </summary>
        public RList<ITEM> mItem;
        public class ITEM
        {
            public DateTime Time;//修改的时间
            public int No;//加热序号!!! 
            public int[] Heats;//加热

        }

        public DateTime Last { get; set; } = DateTime.MinValue;


        /// <summary>
        /// 根据加热,判断是否改变了
        /// </summary>
        /// <param name="heats"></param>
        public bool Add(int[] heats)
        {
            if ((mItem.Count() == 0) || (!IsSame(mItem.Last().Heats, heats)))
            {
                return Add(heats, DateTime.Now);
            }
            else
            {
                return false;
            }
        }
        public bool Add(int[] heats, DateTime time)
        {
            ITEM item = new ITEM();
            item.Heats = heats.Clone() as int[];
            item.Time = time;
            mItem.RAdd(item);
            item.No = mItem.GetLastNo();
            Last = item.Time;
            return true;
        }
        /// <summary>
        /// 判断两个数组是否完全一样
        /// </summary>
        /// <param name="heats1"></param>
        /// <param name="heats2"></param>
        /// <returns></returns>
        bool IsSame(int[] heats1, int[] heats2)
        {
            int channelcnt = heats1.Count();
            for (int i = 0; i < channelcnt; i++)
            {
                if (heats1[i] != heats2[i])
                    return false;
            }
            return true;
        }
        /// <summary>
        /// 获取时间点前的加热策略,  
        /// </summary>
        /// <param name="dt">时间点</param>
        /// <returns></returns>
        public ITEM Get(DateTime dt)
        {
            for (int i = 0; i < mItem.Count(); i++)
            {
                int idx = mItem.Count() - i - 1;
                if (mItem[idx].Time < dt)
                {
                    return mItem[idx];
                }
            }
            return mItem.First();
        }
        public ITEM Get()
        {
            return mItem.Last();
        }
        public void Init(int channelcnt)
        {
            if (this.channelcnt != channelcnt)
            {
                this.channelcnt = channelcnt;
                mItem.Clear();
                //放入第1幅数据,空数据!!!!
                Add(new int[channelcnt], DateTime.Now);
            }
        }
        public HeatChanged()
        {
            mItem = new RList<ITEM>(30);
            Add(new int[channelcnt], DateTime.Now);
        }
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}