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

using Medivators.Comm;
using Medivators.Common;
using Medivators.Common.Enums;
using Medivators.Common.Logging;
using Medivators.Common.Settings;
using Medivators.SerialDevices;
using Medivators.Parser;


namespace TestApp
{
	public partial class frmTest : Form
	{

		private SerialDeviceCollection _devices = null;

		public frmTest()
		{
			InitializeComponent();
			//	initialize logging and error handling
			ApplicationLog aLog = new ApplicationLog();
			aLog.Initialize();
			TextLog tLog = new TextLog(Constants.MedivatorsProgramFiles + "\\" + "logs\\" + Utilities.GetAppName() + ".log");
			tLog.Initialize();

			Log.GetInstance().Provider = aLog;
			//	initialize error handling
			ErrorHandler.GetInstance().Initialize(aLog, tLog);
			// initialize serial devices
			_devices = new SerialDeviceCollection(aLog);
		}

		private void frmTest_Load(object sender, EventArgs e)
		{
			this.textBox1.Text = TextSettings.GetInstance()[SettingTypes.Application,"CommitInterval"];
			this.textBox2.Text = TextSettings.GetInstance()[SettingTypes.Application,"ParseInterval"];
			this.textBox3.Text = TextSettings.GetInstance()[SettingTypes.Application,"DataSource"];
			this.textBox4.Text = TextSettings.GetInstance()[SettingTypes.Application,"ActiveCommPorts"];
		}

		private void button1_Click(object sender, EventArgs e)
		{
			string tempSet = string.Empty;
			for (int i = 1; i <= 8; i++)
			{
				CommPortSettings set;				
				set.BaudRate = Constants.DefaultBaudRate;
				set.DataBit= Constants.DefaultDataBit;
				set.HandShake = Constants.DefaultHandShake;
				set.Parity = Constants.DefaultParity;
				set.StopBit = Constants.DefaultStopBit;
				tempSet += set.ToString();
			}
			MsgBox.Show(tempSet);
		}

		private void buttonLogTest_Click(object sender, EventArgs e)
		{
			TestApp.LogTest dlg = new TestApp.LogTest();
			dlg.Show();
		}

		private void buttonCommPort_Click(object sender, EventArgs e)
		{
			using (CommPort port = new CommPort())
			{
				port.ShowControlDialog();
			}
		}

		private void buttonFakePort_Click(object sender, EventArgs e)
		{
			using (CommPortSimulation port = new CommPortSimulation())
			{
				port.ShowControlDialog();
			}
		}

		private void cmdDSD201Sim_Click(object sender, EventArgs e)
		{
			//	init comm devices
			_devices.Clear();
			DSD201Simulation sim = new DSD201Simulation();
			sim.Log = Log.GetInstance();
			_devices.Add(sim);
		}

		private void cmdLoad_Click(object sender, EventArgs e)
		{
			_devices.SerialDeviceType = SupportedSerialDevices.DSD201;
			_devices.LoadDevices(true);
		}

		private void cmdCacheMonitor_Click(object sender, EventArgs e)
		{
			CacheMonitor dlg = new CacheMonitor();
			dlg.Show();
		}

		private void cmdDBTest_Click(object sender, EventArgs e)
		{
			DBTest dlg = new DBTest();
			dlg.Show();
		}

		private void cmdVirtualService_Click(object sender, EventArgs e)
		{
			VirtualService frmVS = new VirtualService();
			frmVS.Show();
		}
	}
}