using System;
using System.IO;
using System.Text;

using Medivators.Common;
using Medivators.Common.Enums;

namespace Medivators.SerialDevices
{
	class SerialDeviceCaptureFile
	{
		private string _folderName = string.Empty;
		private string _fileName = string.Empty;
		private bool _appendData = false;

		public SerialDeviceCaptureFile (string CaptureFileFolder, string CaptureFileName)
		{
			_fileName = CaptureFileName;
			_folderName = CaptureFileFolder;
			_appendData = false;
		}
		public void Capture (SerialDeviceTransmission TX)
		{
			try
			{
				if (_folderName.Equals(string.Empty) || _fileName.Equals(string.Empty))
					throw new ArgumentException("Must provide Serial Device Capture file name and folder name");
				else
				{
					//	on first write, overwrite. on additional writes, append
					string path = Utilities.PathSlash(_folderName) + _fileName;
					FileStream file = null;
					if ( _appendData )
						file = new FileStream(path, FileMode.Append);
					else
						file = new FileStream(path, FileMode.Create);
					file.Write(TX.Data,0,TX.Data.Length);
					file.WriteByte(10);
					file.WriteByte(13);
					file.Flush();
					file.Close();
					_appendData = true;
				}		
			}
			catch (Exception ex)
			{
				ErrorHandler.GetInstance().LogException("SerialDeviceCaptureFile.Capture reports error: ", ex);
			}
		}
	}
}
