using FLY.Thick.FilmCasting.Server.Model;
using FObjBase;
using FObjBase.Reflect;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Thick.FilmCasting.IService
{
    /// <summary>
    /// 基于DB 的 bulkdata
    /// </summary>
    public interface IBulkDbService : INotifyPropertyChanged
    {
        /// <summary>
        /// 最后一条扫描数据Id
        /// </summary>
        long LastScanDataId { get; }
        /// <summary>
        /// 最后一条产品Id
        /// </summary>
        long LastProfileId { get; }
        /// <summary>
        /// 最后一条产品被修改的时间
        /// </summary>
        DateTime LastTimestamp { get; }



        /// <summary>
        /// 开始产生数据时间
        /// </summary>
        DateTime StartTime { get; }

        /// <summary>
        /// 最后产生数据时间
        /// </summary>
        DateTime EndTime { get; }

        /// <summary>
        /// 当前生产，已经结束，已经下辊了
        /// </summary>
        bool IsFinished { get; }

        [Push(typeof(ScanDataIdChangedEventArgs))]
        event EventHandler ScanDataIdChanged;

        /// <summary>
        /// 临时数据改变
        /// </summary>
        [Push(typeof(BulkDbTempFrameChangedEventArgs))]
        event EventHandler TempFrameChanged;

        /// <summary>
        /// 获取临时数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [Call(typeof(BulkDbTempFrameChangedEventArgs))]
        void GetTempFrame(
            AsyncCBHandler asyncDelegate, object asyncContext);

        #region 统计

        /// <summary>
        /// 获取历史profile
        /// </summary>
        /// <param name="request"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [Call(typeof(Pack_GetProfileReponse))]
        void GetProfile(
            Pack_GetProfileRequest request,
            AsyncCBHandler asyncDelegate, object asyncContext);

        /// <summary>
        /// 获取一幅数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [Call(typeof(Pack_GetFrameReponse))]
        void GetFrame(
            Pack_GetFrameRequest request,
            AsyncCBHandler asyncDelegate, object asyncContext);

        /// <summary>
        /// 获取一幅数据 且 对应的Profile
        /// </summary>
        /// <param name="request"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [Call(typeof(Pack_GetFrame2Reponse))]
        void GetFrame2(
            Pack_GetFrameRequest request,
            AsyncCBHandler asyncDelegate, object asyncContext);


        /// <summary>
        /// 获取近期的大量数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [Call(typeof(Pack_GetRecentFramesReponse))]
        void GetRecentFrames(
            Pack_GetRecentFramesRequest request,
            AsyncCBHandler asyncDelegate, object asyncContext);


        /// <summary>
        /// 获取纵向趋势图
        /// </summary>
        /// <param name="request"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [Call(typeof(Pack_GetTrendReponse))]
        void GetTrend(
            Pack_GetTrendRequest request,
            AsyncCBHandler asyncDelegate, object asyncContext);



        #endregion


        /// <summary>
        /// 完成,下辊。。。
        /// </summary>
        void Finish();
    }

    public class Pack_GetRecentFramesRequest
    {
        /// <summary>
        /// 最后1幅数据ID, 当-1, 从数据库最后获取
        /// </summary>
        public long Id;

        /// <summary>
        /// 获取数据集数
        /// </summary>
        public int Count;

        /// <summary>
        /// 混合的数据 时间范围限制
        /// 与最新的图比较， 时间差不能超过 TimeRange
        /// 大于 TimeSpan.Zero 起作用
        /// </summary>
        public DateTime FromTime;
    }
    public class Pack_GetRecentFramesReponse 
    {
        public Pack_GetRecentFramesRequest Request;
        /// <summary>
        /// scanDatas[0] 是最新的数据
        /// </summary>
        public List<Lc_ScanData> scanDatas;
    }
    public class Pack_GetFrameRequest
    {
        /// <summary>
        /// 最后1幅数据ID, 当-1, 从数据库最后获取
        /// </summary>
        public long Id;

        /// <summary>
        /// 混合数
        /// </summary>
        public int Mix;

        /// <summary>
        /// 混合的数据 时间范围限制
        /// 与最新的图比较， 时间差不能超过 TimeRange
        /// 大于 TimeSpan.Zero 起作用
        /// </summary>
        public TimeSpan TimeRange;

        /// <summary>
        /// 混合的数据 均值范围限制
        /// 与最新的图比较， 均值不能超过 AvgRange
        /// 大于 0 起作用
        /// </summary>
        public double AvgRange;
    }
    public class Pack_GetProfileRequest
    {
        public long Id;
    }

    public class Pack_GetProfileReponse
    {
        public Pack_GetProfileRequest Request;
        public Db_Profile profile;
    }

    public class Pack_GetFrameReponse
    {
        public Pack_GetFrameRequest Request;
        public Lc_ScanData scanData;
    }


    public class Pack_GetFrame2Reponse
    {
        public Pack_GetFrameRequest Request;
        public Lc_ScanData scanData;
        public Db_Profile profile;
    }

    public class Pack_GetTrendRequest
    {
        /// <summary>
        /// 最后1幅数据ID, 当-1, 从数据库最后获取
        /// </summary>
        public long Id;

        /// <summary>
        /// 长度
        /// </summary>
        public int Count;

        /// <summary>
        /// 间隔, 只获取 Id % Interval == 0 的数据
        /// </summary>
        public int Interval;

        /// <summary>
        /// 混合数，经过间隔后的数据再混合
        /// </summary>
        public int Mix;

        /// <summary>
        /// 按时间查找数据
        /// </summary>
        public bool IsSearchByTime;

        /// <summary>
        /// 按时间查找数据, 获取的数据对应的时间都比Time小
        /// </summary>
        public DateTime Time;


    }
    public class Pack_GetTrendReponse
    {
        public Pack_GetTrendRequest Request;
        public List<TrendValue> Values;
        public long ScanDataId;
    }


    public class TrendValue
    {
        public long ProfileID { get; set; }
        public long ScanDataID { get; set; }
        public double Sigma { get; set; }
        public double Value { get; set; }
        public DateTime Time { get; set; }
    }

    public class BulkDbTempFrameChangedEventArgs : EventArgs
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime Time;
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime;
        /// <summary>
        /// 开始位置
        /// </summary>
        public int StartIndex;
        /// <summary>
        /// 数据
        /// </summary>
        public double[] D;
    }
    public class ScanDataIdChangedEventArgs : EventArgs
    {
        public long LastScanDataId;
    }
}
