using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Medivators.SerialDevices
{
	public partial class dlgDSD_201Simulation : Form
	{

		private DSD201Simulation _device = null;
		private DateTime _lastRead = DateTime.Now;
		private TimeSpan _showInterval = new TimeSpan(0, 0, 0, 0, 500);

		private delegate void ReadingLightDelegate (bool On);

		public dlgDSD_201Simulation()
		{
			InitializeComponent();
		}
		public dlgDSD_201Simulation(DSD201Simulation Device)
		{
			InitializeComponent();
			_device = Device;
			_device.RecordReceived += new RecordReceivedEventHandler(device_RecordReceived);
			tmrDisplay.Tick += new EventHandler(tmrDisplay_Tick);
			txtReadInterval.Text = _device.RecordReadInterval.ToString();
		}

		private void txtFilePath_TextChanged(object sender, EventArgs e)
		{
			_device.SimulationFilePath = this.txtFilePath.Text;
		}
		private void cmdBrowseCSV_Click(object sender, EventArgs e)
		{
			if (this.txtFilePath.Text != String.Empty && this.txtFilePath.Text.Length > 0)
				this.dlgOpenFile.FileName = this.txtFilePath.Text;
			if (this.dlgOpenFile.ShowDialog() == DialogResult.OK && this.dlgOpenFile.FileName != String.Empty)
				this.txtFilePath.Text = this.dlgOpenFile.FileName;
		}
		private void txtReadInterval_TextChanged(object sender, EventArgs e)
		{
			try
			{
				this._device.RecordReadInterval = Convert.ToInt32(this.txtReadInterval.Text);
			}
			catch
			{
			}
		}
		private void device_RecordReceived(object sender, RecordReceivedEventArgs e)
		{
			ReadingLightDelegate d = ReadingDisplay;
			object[] parms = new object[1] { true };
			this.lblReading.Invoke(d, parms);
			this._lastRead = DateTime.Now;
		}
		private void tmrDisplay_Tick(object sender, EventArgs e)
		{
			if (_lastRead.Add(_showInterval) <= DateTime.Now)
			{
				ReadingLightDelegate d = ReadingDisplay;
				object[] parms = new object[1] {false};
				this.lblReading.Invoke(d, parms);
			}
		}

		private void cmdGo_Click(object sender, EventArgs e)
		{
			bool go = false;
			if (this.txtFilePath.Text != String.Empty && this.txtFilePath.Text.Length > 0)
				if (this.txtReadInterval.Text != String.Empty && this.txtReadInterval.Text.Length > 0)
					go = true;

			if (!go)
				MessageBox.Show("Comm test file path and read interval are both required");
			else
			{
				//	 get first record immediately
				_device.Timer_Elapsed(null, null);

				// start timer to get remaining records
				_device.StartTimer(this.radioRollFile.Checked);
				this.cmdGo.Enabled = false;
				this.cmdStop.Enabled = true;
			}
		}
		private void cmdStop_Click(object sender, EventArgs e)
		{
			_device.StopTimer();
			this.cmdGo.Enabled = true;
			this.cmdStop.Enabled = false;
		}
		private void ReadingDisplay(bool On)
		{
			if (this.lblReading.Visible != On)
				this.lblReading.Visible = On;
		}

		private void cmdGetOne_Click(object sender, EventArgs e)
		{
			//	 get one record immediately but do not start timer
			_device.Timer_Elapsed(null, null);
		}

		private void get100_Click(object sender, EventArgs e)
		{
			this.Cursor = Cursors.WaitCursor;
			for ( int ctr = 0; ctr < 100; ctr ++ )
				_device.Timer_Elapsed(null, null);
			this.Cursor = Cursors.Default;
		}
	}
}