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

namespace CRVM.Utility
{

    public class VibData
    {

        public readonly int speedChannelMax = 5;
        public readonly int SamplePoint = 1024;
        public readonly int SampleFre = 5120;
        public int dataFrameNum = 0;//文件的数据帧长度
        private List<List<float>> _originDataList;
        public List<List<float>> originDataList
        {
            get { return _originDataList; }
        }

        private List<List<double>> _originSpeedList;
        public List<List<double>> originSpeedList
        {
            get { return _originSpeedList; }
        }

        private List<double> _originDistanceList;
        public List<double> originDistanceList
        {
            get { return _originDistanceList; }
        }

        public int _signalLengthOrigin;
        public int signalLengthOrigin
        {
            get { return _signalLengthOrigin; }
        }

        private Dictionary<string, int> indColmun;


        public bool readOrigData(string path)
        {
            try
            {
                Stream myStream = new FileStream(path, FileMode.Open);
                _signalLengthOrigin = 0;
                BinaryReader MyReader = new BinaryReader(myStream, Encoding.UTF8);
                string tempStr = MyReader.ReadString();
                if (tempStr == "原始数据")
                {
                    //读取头
                    tempStr = MyReader.ReadString();
                    indColmun = new Dictionary<string, int>();
                    int channelCount = 0;
                    while (tempStr != "/r/n")
                    {
                        indColmun.Add(tempStr, channelCount);
                        tempStr = MyReader.ReadString();
                        channelCount += 1;
                    }
                   int SampleFre = Convert.ToInt32(MyReader.ReadSingle());
                   int SamplePoint = Convert.ToInt32(MyReader.ReadSingle());
                    //建立数据结构
                    _originDataList = new List<List<float>>();
                    for (int i = 0; i < channelCount; i++)
                    {
                        //定义数据长度，避免自动扩充时溢出
                        List<float> listm = new List<float>();// 30 * 60 * SysParam.Instance.reSampleFre);
                        _originDataList.Add(listm);
                    }
                    _originSpeedList = new List<List<double>>();//存放速度和轧制距离 
                    for (int i = 0; i < speedChannelMax; i++)
                    {
                        List<double> listm = new List<double>();
                        _originSpeedList.Add(listm);
                    }
                    _originDistanceList = new List<double>();

                    //开始读取数据
                    float signaldata = 0;
                    signaldata = MyReader.ReadSingle();
                    dataFrameNum = 0;
                    try
                    {
                        while (signaldata == Convert.ToSingle("1111"))
                        {
                            for (int i = 0; i < SamplePoint; i++)
                            {
                                for (int k = 0; k < channelCount; k++)
                                {
                                    _originDataList[k].Add(MyReader.ReadSingle());
                                }
                            }
                            for (int i = 0; i < speedChannelMax; i++)
                            {
                                _originSpeedList[i].Add(MyReader.ReadSingle());
                            }
                            _originDistanceList.Add(MyReader.ReadSingle());
                            signaldata = MyReader.ReadSingle();
                            dataFrameNum++;
                            _signalLengthOrigin = dataFrameNum * SamplePoint;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("readOrigData" + ex.Message);
                        for (int k = 0; k < channelCount; k++)////去掉读取不正确的那帧不完整的数据，将前面正确读取的部分保留
                        {
                            for (int i = dataFrameNum * SamplePoint; i < _originDataList[k].Count; i++)
                            {
                                _originDataList[k].RemoveAt(i);
                            }
                        }
                        for (int i = 0; i < speedChannelMax; i++)
                        {
                            for (int j = dataFrameNum; j < _originSpeedList[i].Count; j++)
                            {
                                _originSpeedList[i].RemoveAt(j);
                            }
                        }
                        for (int i = dataFrameNum; i < _originDistanceList.Count; i++)
                        {
                            _originDistanceList.RemoveAt(i);
                        }
                    }

                    _signalLengthOrigin = _originDataList[0].Count;
                    MyReader.Close();
                    myStream.Close();
                    return true;

                }
                MyReader.Close();
                myStream.Close();
                return false;

            }
            catch (Exception)
            {
                return false;
            }
        }
    }

    public class OnlineData
    {
        public double[,] vibData { set; get; }
        public double[] speed { get; set; }

        public double length { get; set; }

        public bool isCuted { get; set; }

        public OnlineData()
        {
            vibData = new double[8, 1024];
            speed = new double[6];
            length = 0;
            isCuted = false;
        }
    }

    static public class VibSimulation
    {
        static public readonly string path = AppDomain.CurrentDomain.BaseDirectory + "SimulationData";
        static public List<VibData> _fileOriginData { set; get; }
        //static public List<int> _fileFrameNum { set; get; }

        static public OnlineData _onlineData = new OnlineData();

        static private List<OnlineData> alldata = new List<OnlineData>();

        static int allDataCount = 0;
        static int frameNum = 0;
        static VibSimulation()
        {
            string[] originFiles = Directory.GetFiles(path, "*.dat", SearchOption.AllDirectories);
            int fileNum = originFiles.Length;
            int factFileNum = 0;
         
     
            _fileOriginData = new List<VibData>();
            for (int i = 0; i < fileNum; i++)
            {
                VibData temp = new VibData();
                if (temp.readOrigData(originFiles[i]))
                {
                    _fileOriginData.Add(temp);
                    //_fileFrameNum.Add(temp._signalLengthOrigin);
                    factFileNum++;

                }
            }
            if (factFileNum == 0)
            {
                _fileOriginData = null;
            }

            AssemblyData();//数据组装




        }

        static public void UpdateOnlineData(bool isDebug=false)
        {
            if (allDataCount != 0)
            {
                frameNum = frameNum % allDataCount;
                _onlineData = alldata[frameNum];
                if(!isDebug)
                frameNum++;
            }
        
        }
        static void AssemblyData()
        {
            if (_fileOriginData != null)
            {
                for (int i = 0; i < _fileOriginData.Count; i++)
                {//文件
                    VibData vdatai = _fileOriginData[i];
                    int dataFrameNum = vdatai._signalLengthOrigin / vdatai.SamplePoint;
                    allDataCount += dataFrameNum;
                    for (int j = 0; j < dataFrameNum; j++)
                    {
                        OnlineData onlineData = new OnlineData();
                        //通道
                        for (int ch = 0; ch < 8; ch++)
                        {
                            //振动数据 数据帧 
                            for (int k = 0; k < vdatai.SamplePoint; k++)
                            {
                                //振动
                                onlineData.vibData[ch, k] = vdatai.originDataList[ch][j * vdatai.SamplePoint + k];
                                //速度
                                if (ch < 5)
                                {
                                    onlineData.speed[ch] = vdatai.originSpeedList[ch][j];
                                }
                                if (ch == 5)
                                {
                                    onlineData.speed[ch] = onlineData.speed[ch - 1]+3;
                                }

                            }
                        }
                        //距离
                        onlineData.length = vdatai.originDistanceList[j];
                        //剪切信号
                        onlineData.isCuted = j > (dataFrameNum - 5) ? true : false;
                        alldata.Add(onlineData);
                    }
                }
            }
        }




    }
}
