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>
        /// 获取纵向趋势图
        /// </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_GetFrameRequest
    {
        /// <summary>
        /// 最后1幅数据ID, 当-1, 从数据库最后获取
        /// </summary>
        public long Id;
        /// <summary>
        /// 混合的数据，不能前于 StartTime
        /// </summary>
        public DateTime StartTime;
        /// <summary>
        /// 混合数
        /// </summary>
        public int Mix;
    }
    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_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 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;
    }
}
