using FObjBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/// <summary>
/// 公共类型
/// </summary>
namespace FLY.Thick.BulkDataModule
{
    /// <summary>
    /// 一幅数据是否有效
    /// </summary>
    public enum FR_DATA_TYPE
    {
        /// <summary>
        /// 无效
        /// </summary>
        INVALID = 0,
        /// <summary>
        /// 有效
        /// </summary>
        VALID = 1
    }

    /// <summary>
    /// 
    /// </summary>
    public class FR_DATA : IPack2
    {
        public FR_DATA_TYPE type;
        public int bookmark;// bookmark ID
        public DateTime timemark;
        public int[] m_data;
        public override string ToString()
        {
            return bookmark.ToString();
        }
        public FR_DATA(int bm, int[] data, FR_DATA_TYPE type)
        {
            bookmark = bm;
            timemark = DateTime.Now;
            m_data = (int[])data.Clone();
            this.type = type;
        }
        public FR_DATA()
        {

        }

        public bool TryParse(byte[] value, int index, out int cnt)
        {
            cnt = 4 + 8 + 4;
            if (index + cnt > value.Length)
                return false;

            int idx = index;
            bookmark = BitConverter.ToInt32(value, idx);
            idx += 4;

            try
            {
                timemark = new DateTime(BitConverter.ToInt64(value, idx));
            }
            catch
            {
                timemark = DateTime.MinValue;
            }
            idx += 8;

            type = (FR_DATA_TYPE)BitConverter.ToInt32(value, idx);
            idx += 4;


            //data
            cnt += 4;
            if (index + cnt > value.Length)
                return false;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            cnt += len * 4;
            if (index + cnt > value.Length)
                return false;
            if (len > 0)
            {
                m_data = new int[len];
                for (int i = 0; i < len; i++)
                {
                    m_data[i] = BitConverter.ToInt32(value, idx);
                    idx += 4;
                }
            }

            return true;
        }

        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            buf.AddRange(BitConverter.GetBytes(bookmark));
            buf.AddRange(BitConverter.GetBytes(timemark.Ticks));
            buf.AddRange(BitConverter.GetBytes((int)type));
            if (m_data == null)
            {
                buf.AddRange(BitConverter.GetBytes(0));
            }
            else
            {
                buf.AddRange(BitConverter.GetBytes(m_data.Length));
                for (int i = 0; i < m_data.Length; i++)
                {
                    buf.AddRange(BitConverter.GetBytes(m_data[i]));
                }
            }

            return buf.ToArray();
        }

        public bool TryParse(byte[] value)
        {
            int cnt;
            return TryParse(value, 0, out cnt);
        }
    }
    /// <summary>
    /// 它是 struct, 值类型,所以不用 Clone!!!!!
    /// </summary>
    public struct TrendDataCell 
    {
        /// <summary>
        /// 时间
        /// </summary>
        public DateTime Time;
        /// <summary>
        /// 平均值
        /// </summary>
        public int Avg;
        /// <summary>
        /// sigma
        /// </summary>
        public int Sigma;
        /// <summary>
        /// 类型
        /// </summary>
        public FR_DATA_TYPE Type;
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Time.ToShortTimeString() + " " + Avg.ToString();
        }
        #region IPack 成员
        /// <summary>
        /// 输出20个字节
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            buf.AddRange(BitConverter.GetBytes(Time.Ticks));
            buf.AddRange(BitConverter.GetBytes(Avg));
            buf.AddRange(BitConverter.GetBytes(Sigma));
            buf.AddRange(BitConverter.GetBytes((int)Type));
            return buf.ToArray();
        }

        /// <summary>
        /// 使用20个字节转换
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryParse(byte[] value, int startIndex )
        {
            int count = 8 + 4 * 3;
            if (value.Length < count)
                return false;

            int idx = startIndex;
            Time = new DateTime(BitConverter.ToInt64(value, idx));
            idx += 8;

            Avg = BitConverter.ToInt32(value, idx);
            idx += 4;
            Sigma = BitConverter.ToInt32(value, idx);
            idx += 4;
            Type = (FR_DATA_TYPE)BitConverter.ToInt32(value, idx);
            idx += 4;
            
            return true;
        }

        public TrendDataCell Clone() 
        {
            return new TrendDataCell()
            {
                Time = Time,
                Avg = Avg,
                Sigma = Sigma,
                Type = Type
            };
        }
        #endregion
    }
    /// <summary>
    /// 获取模式
    /// </summary>
    public enum PushMode 
    {
        /// <summary>
        /// 获取并且推送
        /// </summary>
        GetAndPush,
        /// <summary>
        /// 只获取
        /// </summary>
        Get
    }


    /// <summary>
    /// 它是 struct, 值类型,所以不用 Clone!!!!!
    /// </summary>
    public struct FrameDataCell
    {
        /// <summary>
        /// 平均值
        /// </summary>
        public int Avg;
        /// <summary>
        /// sigma
        /// </summary>
        public int Sigma;
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Avg.ToString();
        }
        #region IPack 成员
        /// <summary>
        /// 输出20个字节
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            buf.AddRange(BitConverter.GetBytes(Avg));
            buf.AddRange(BitConverter.GetBytes(Sigma));
            return buf.ToArray();
        }

        /// <summary>
        /// 使用8个字节转换
        /// </summary>
        /// <param name="value"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public bool TryParse(byte[] value, int startIndex)
        {
            int count = 4 * 2;
            if (value.Length < count)
                return false;

            int idx = startIndex;
            
            Avg = BitConverter.ToInt32(value, idx);
            idx += 4;
            Sigma = BitConverter.ToInt32(value, idx);
            idx += 4;

            return true;
        }
        /// <summary>
        /// 复制
        /// </summary>
        /// <returns></returns>
        public FrameDataCell Clone()
        {
            return new FrameDataCell()
            {
                Avg = Avg,
                Sigma = Sigma,
            };
        }
        #endregion
    }
}