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

namespace ErinClock
{
	public partial class MainForm : Form
	{
		private Point MousePointer;
		ErinClock.Properties.Settings st = new ErinClock.Properties.Settings();
		public MainForm()
		{
			InitializeComponent();
			st.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(st_PropertyChanged);
		}

		void st_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			switch (e.PropertyName)
			{
				case "Interval":
					ErinTimeGetter.Stop();
					ErinTimeGetter.Interval = ((ErinClock.Properties.Settings)sender).Interval;
					ErinTimeGetter.Start();
					break;
			}
		}

		private void ErinTimeGetter_Tick(object sender, EventArgs e)
		{
			DateTime ErinTimeNow = 
				DateTime.FromBinary(DateTime.UtcNow.ToBinary() * 40).AddHours(st.TimeRag);

			StringBuilder sb = new StringBuilder();

			ErinTimeNow = ErinTimeNow.AddYears(-2003);
			ErinTimeNow = ErinTimeNow.AddMonths(-12);
			ErinTimeNow = ErinTimeNow.AddDays(-18);
			sb.Append(ErinTimeNow);
			ErinTime.Text = sb.ToString();

			sb = null;
			GC.Collect();
		}

		private void MainForm_MouseDown(object sender, MouseEventArgs e)
		{
			if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
			{
				MousePointer = new Point(-e.X, -e.Y);
			}
		}

		private void MainForm_MouseMove(object sender, MouseEventArgs e)
		{
			if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
			{
				this.SetDesktopLocation(
					this.Left + MousePointer.X + e.X,
					this.Top + MousePointer.Y + e.Y);
			}
		}

		private void SettingItem_Click(object sender, EventArgs e)
		{
			ConfigForm cf = new ConfigForm();
			cf.Interval.Value = st.Interval;
			cf.TimeRag.Value = st.TimeRag;
			if (cf.ShowDialog(this) == DialogResult.OK)
			{
				st.TimeRag = (int)cf.TimeRag.Value;
				st.Interval = (int)cf.Interval.Value;
				st.Save();
			}
			if (cf.Created)
			{
				cf.Dispose();
				GC.Collect();
			}
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			ErinTimeGetter.Interval = st.Interval;
		}

		private void ResetItem_Click(object sender, EventArgs e)
		{
			st.Reset();
			st.Save();
		}

		private void ExitItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

	}
}