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



namespace WindowsFormsApplication1
{
    class FileTransactions
    {
        //-------------------------------------------------------------------------------------------------
        public static void ToFile(

            byte ChanelPresent, int datalen, int wrPtr, int postTrigLen, string path, int deci_factor,

            int ampli1, int ampli2, int ampli3,

            int ampli_offset1, int ampli_offset2, int ampli_offset3,

            char coupling_CH1, char coupling_CH2, char coupling_CH3,

            byte[] data1, byte[] data2, byte[] data3, byte[] data4)
        {
            byte data_offset = 0x45; //offset from file begining to begin of data // it is the first walid data point
            string lines;
            DateTime actualTime = DateTime.Now;

            path += actualTime.Year + "." +
                actualTime.Month.ToString("00") + "." +
                actualTime.Day.ToString("00") + "_" +
                actualTime.Hour.ToString("00") + "-" +
                actualTime.Minute.ToString("00") + "-" +
                actualTime.Second.ToString("00") + "--"+
                actualTime.Millisecond.ToString("00") + ".lgf";

            FileStream fs = new FileStream(path, FileMode.Create);
            BinaryWriter w = new BinaryWriter(fs);


            lines = "TMW OSC LOG";

            lines += actualTime.Year + ".";
            lines += (actualTime.Month).ToString("00") + ".";
            lines += (actualTime.Day).ToString("00") + " ";

            lines += (actualTime.Hour).ToString("00") + ":";
            lines += (actualTime.Minute).ToString("00") + ":";
            lines += (actualTime.Second).ToString("00");


            w.Write(lines);
            w.Write(ChanelPresent);
            w.Write(deci_factor);
            w.Write(datalen);
            w.Write(postTrigLen);
            w.Write(data_offset);
            w.Write(ampli1);
            w.Write(ampli2);
            w.Write(ampli3);
            w.Write(ampli_offset1);
            w.Write(ampli_offset2);
            w.Write(ampli_offset3);
            w.Write(coupling_CH1);
            w.Write(coupling_CH2);
            w.Write(coupling_CH3);

            if ((ChanelPresent & 0x03) > 0)
            {
                w.Write(data1, wrPtr, datalen - wrPtr);
                w.Write(data1, 0, wrPtr);
            }

            if ((ChanelPresent & 0x0C) > 0)
            {
                w.Write(data2, wrPtr, datalen - wrPtr);
                w.Write(data2, 0, wrPtr);
            }

            if ((ChanelPresent & 0x30) > 0)
            {
                w.Write(data3, wrPtr, datalen - wrPtr);
                w.Write(data3, 0, wrPtr);
            }

            if ((ChanelPresent & 0xC0) > 0)
            {
                w.Write(data4, wrPtr, datalen - wrPtr);
                w.Write(data4, 0, wrPtr);
            }
            // 4x byte array

            w.Close();
            fs.Close();
        }
        //-------------------------------------------------------------------------------------------------
    }
}
