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

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

namespace TestApp
{
	public partial class LogTest : Form
	{
		private Log										_log = null;
		private System.Diagnostics.EventLogEntryType	_eventLogType = System.Diagnostics.EventLogEntryType.Information;
		Medivators.Common.Enums.LogEntryType				_logType = LogEntryType.Normal;

		public LogTest()
		{
			InitializeComponent();
		}

		private void LogTest_Load(object sender, EventArgs e)
		{
			this.comboLogType.Items.AddRange(Enum.GetNames(typeof(System.Diagnostics.EventLogEntryType)));
			this.comboCriticality.Items.AddRange(Enum.GetNames(typeof(Medivators.Common.Enums.LogEntryType)));
			this.labelLoggingLevel.Text = TextSettings.GetInstance()[SettingTypes.Application, "LoggingLevel"];
		}

		#region Radiobuttons change
		private void LogType_CheckedChanged(object sender, EventArgs e)
		{
			if (this._log == null)
				this._log = Log.GetInstance();
			if (((Control)sender).Name.Equals(this.radioButton1.Name))
				_log.Provider = new ApplicationLog();
			else
				_log.Provider = new TextLog(this.textPath.Text);
			_log.Provider.Initialize();
			this.groupBox1.Enabled = false;
		}
		#endregion

		#region combos change
		private void comboLogType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this._eventLogType = (System.Diagnostics.EventLogEntryType)Enum.Parse(typeof(System.Diagnostics.EventLogEntryType), (string)this.comboLogType.SelectedItem);
		}

		private void comboCriticality_SelectedIndexChanged(object sender, EventArgs e)
		{
			this._logType = (Medivators.Common.Enums.LogEntryType)Enum.Parse(typeof(Medivators.Common.Enums.LogEntryType), (string)this.comboCriticality.SelectedItem);
		}
		#endregion

		#region buttons click

		// dispose current log provider
		private void DisposeLog_Click(object sender, EventArgs e)
		{
			this._log.Provider.Dispose();
			this.groupBox1.Enabled = true;
		}

		#region Log Buttons click
		private void button1_Click(object sender, EventArgs e)
		{
			this._log.WriteLog(textMessage.Text);
		}

		private void button2_Click(object sender, EventArgs e)
		{
			this._log.WriteLog(textMessage.Text, this._eventLogType);
		}
		#endregion

		private void button4_Click(object sender, EventArgs e)
		{
			this._log.WriteLog(textMessage.Text, _eventLogType, GetData());
		}
		private void button5_Click(object sender, EventArgs e)
		{
			_log.WriteLog(_logType, textMessage.Text);
		}

		private void button6_Click(object sender, EventArgs e)
		{
			_log.WriteLog(_logType, textMessage.Text, _eventLogType);
		}

		private void button7_Click(object sender, EventArgs e)
		{
			_log.WriteLog(_logType, textMessage.Text, _eventLogType, GetData());
		}

		#endregion

		#region private helpers
		private byte[] GetData()
		{
			byte[] data = new byte[6];
			data[0] = Convert.ToByte(byte0.Text);
			data[1] = Convert.ToByte(byte1.Text);
			data[2] = Convert.ToByte(byte2.Text);
			data[3] = Convert.ToByte(byte3.Text);
			data[4] = Convert.ToByte(byte4.Text);
			data[5] = Convert.ToByte(byte5.Text);
			return data;
		}
		#endregion

	}
}