using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.Collections;
using GcDevicePc.Common;
using GcDevicePc.GCBuffer;
using System.Runtime.InteropServices;

namespace GcDevicePc.ProThread
{
    class ChannelDataSave
    {
        ManualResetEvent datamre = new ManualResetEvent(false);
        Thread t_DataSave;

        private void Monitor_Data()
        {
            double time = 0;
            double value = 0;

            float dtime = 0.0f;
            float dvalue = 0.0f;
         
            while (!datamre.WaitOne(500))
            {
                if ((globaldata.m_hmibuffer.gcinfo.methodstatus == 4 || globaldata.m_hmibuffer.gcinfo.methodstatus == 5) && globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.system_Statue)
                {
                    if (globaldata.m_signalbuffer.Draw_Port.Count > 1)
                    {
                        if (globaldata.startclear || globaldata.stopflag || globaldata.cancelflag)
                        {
                            time = 0;
                            globaldata.startclear = false;
                        }

                        if (globaldata.stopflag)
                        {
                            time = 0;
                        }
                        else
                        {
                            //if (globaldata.starttimer == 0)
                            //{
                            //    globaldata.starttimer = ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port[0]).ctimer;
                            //}

                            for (int i = 0; i < globaldata.m_signalbuffer.Draw_Port.Count; i++)
                            {
                                if (globaldata.starttimer <= ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port[i]).ctimer)
                                {
                                    time += ((((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port[i]).ctimer - globaldata.starttimer) / 1000.0f / 60.0f);
                                    globaldata.starttimer = ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port[i]).ctimer;
                                    value = (((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port[i]).cdata * 1000.0f);

                                    dtime = float.Parse(time.ToString("0.0000"));
                                    dvalue = float.Parse(value.ToString("0.0000"));

                                    if (globaldata.startflag && globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_DataName != null)
                                    {
                                      //  AESfile savefile = new AESfile(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_DataName);
                                      //  savefile.Write_filetw(dtime, dvalue);
                                    }
                                }
                            }
                        }
                    }
                   // globaldata.m_signalbuffer.Draw_Port.Clear();                   
                }

                if (globaldata.cancelflag)
                {
                    if (globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_DataName != null)
                    {
                        if (File.Exists(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_DataName))
                        {
                            File.Delete(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_DataName);
                        }
                    }
                    globaldata.cancelflag = false;
                }

                Thread.Sleep(500);
            }
        }


        public void ChannelDataStart()
        {
            if( datamre != null)
            {
                datamre.Reset();
            }
            t_DataSave = new Thread(Monitor_Data);
            t_DataSave.IsBackground = true;
            t_DataSave.Start();
        }

        public void ChannelDataStop()
        {
            datamre.Set();
        }


    }
}
