using ServiceStack;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace StartServerWPF.Modules.MseedChart
{
    public class Mseed2asciiApi
    {
        public delegate void LoopCallbackHandler(AsciiDataStruct asciiData);
        [DllImport("mseedC.dll", EntryPoint = "MseedDatas")]
        public extern static int MseedDatas(int a, string[] name);
        //public extern static int MseedDatas(Byte[] bytes, int lenght);
        [DllImport("mseedC.dll", EntryPoint = "bufferMseedData")]
        public extern static int bufferMseedData(int length, Byte[] data);
        [DllImport("mseedC.dll", EntryPoint = "testc")]
        public extern static int testc(int a, int b);
        [DllImport("mseedC.dll")]
        public static extern void MseedDatasCallFun(LoopCallbackHandler callback);
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct AsciiDataStruct
    {
       public string sid;
      //  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
        public string starttime;               //!< Time of first sample
      //   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
        public string endtime;                 //!< Time of last sample
        public double samprate;                  //!< Nominal sample rate (Hz)
        public Int64 samplecnt;                //!< Number of samples in trace coverage
        public UInt64 datasize;                  //!< Size of datasamples buffer in bytes
        public Int64 numsamples;               //!< Number of data samples in datasamples
        public char sampletype;                 //!< Sample type code, see @ref sample-types
        public char samplesize;
        public System.IntPtr datasamples;       //!< Data samples, \a numsamples of type \a sampletype
    }
    public class ASCiiData
    {
        public int Index { set; get; }
        public string sid { set; get; }
        public double samprate { set; get; }

        public double StartOATime { set; get; }
        public Int64 numsamples { set; get; }

        public DateTime FirstSampleTime { set; get; }

        public ConcurrentQueue<ChartSamples> DataOrders { get; set; } = new ConcurrentQueue<ChartSamples>();

        public void AddChartSam(double freque,ChartSamples samples)
        {
            if (DataOrders.Count != 0)
            {
                ChartSamples chartLast = DataOrders.Last();
                var ts = samples.StartTime.Subtract(chartLast.StartTime).Duration();
                // 时间差等于上一次收到数据               
                if (ts.TotalSeconds > 1)
                {
                    System.Diagnostics.Debug.WriteLine($"填充均值数据总数：{ts.TotalSeconds * freque}********{samples.StartTime}, {chartLast.StartTime}时间差：{ts.TotalSeconds}秒, 频率：{freque}");

                    var temp = DataOrders.Last();
                    var Average = temp.DataArray.Average();
                    DataOrders.Enqueue(new ChartSamples
                    {
                        StartTime = samples.StartTime,
                        DataArray = Enumerable.Repeat(Average, (int)(ts.TotalSeconds * freque)).ToArray()
                    });
                }
                else if (ts.TotalSeconds <= -1)
                {
                    var data = DataOrders.Where(d => d.StartTime == samples.StartTime).FirstOrDefault();
                    Debug.WriteLine($"***********总时间：{ts.TotalSeconds}, samples:{samples.StartTime},{samples.DataArray.Length}***{chartLast.StartTime},{chartLast.DataArray.Length}");
                    if (data != null) data = samples;
                    return;
                }
            }       
            DataOrders.Enqueue(samples);
        }
    }

    public class ChartSamples
    {
        public DateTime StartTime { set; get; }

        public double samprate { set; get; }
        public double[] DataArray { set; get; }
    }
}
