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

using Medivators.Common;
using Medivators.Parser;

namespace TestApp
{
	public partial class CacheMonitor : Form
	{
		public CacheMonitor()
		{
			InitializeComponent();
		}

		private void CacheMonitor_Load(object sender, EventArgs e)
		{
			LoadListBoxes();
		}
		private void cmdStart_Click(object sender, EventArgs e)
		{
			ParseDispatcher.GetInstance().Start();
		}
		private void cmdStop_Click(object sender, EventArgs e)
		{
			ParseDispatcher.GetInstance().Stop();
		}
		private void cmdRefresh_Click(object sender, EventArgs e)
		{
			LoadListBoxes();
			RefreshLabels();
		}
		private void cmdStartCache_Click(object sender, EventArgs e)
		{
			CacheManager.GetInstance().Start();
		}
		private void cmdStopCache_Click(object sender, EventArgs e)
		{
			CacheManager.GetInstance().Stop();
		}
		private void RefreshLabels()
		{
			this.lblCacheIsRunning.Text = CacheManager.GetInstance().IsRunning.ToString();
			this.lblParserIsRunning.Text = ParseDispatcher.GetInstance().IsRunning.ToString();
		}
		private void LoadListBoxes()
		{
			try
			{
				this.SuspendLayout();

				listNewReports.Items.Clear();
				foreach (ICacheItem rpt in UncommittedCycleReportCache.GetInstance().Values)
					listNewReports.Items.Add(rpt.ToString());

				listOldReports.Items.Clear();
				foreach (ICacheItem rpt in CommittedCycleReportCache.GetInstance().Values)
					listOldReports.Items.Add(rpt.ToString());

				listNewRecords.Items.Clear();
				foreach (ICacheItem rpt in UncommittedLogRecordCache.GetInstance().Values)
					listNewRecords.Items.Add(rpt.ToString());

				listOldRecords.Items.Clear();
				foreach (ICacheItem rpt in CommittedLogRecordCache.GetInstance().Values)
					listOldRecords.Items.Add(rpt.ToString());

				this.ResumeLayout();
			}
			catch (Exception ex)
			{
				MsgBox.Show(ex.ToString());
			}
		}

		private void ParseOne_Click(object sender, EventArgs e)
		{
			ParseDispatcher.GetInstance().ParseOneTransmission();
		}

		private void CacheOnce_Click(object sender, EventArgs e)
		{
			CacheManager.GetInstance().ProcessCache();
		}

		private void Parse100_Click(object sender, EventArgs e)
		{
			this.Cursor = Cursors.WaitCursor;
			for (int ctr = 0; ctr < 100; ctr++)
				ParseDispatcher.GetInstance().ParseOneTransmission();
			this.Cursor = Cursors.Default;
		}
	}
}