using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Hanna.Controls;
using Medivators.Common;
using Medivators.Common.Enums;
using Medivators.Common.Settings;

namespace Medivators.Reporting
{
	public partial class frmBase : Form
	{
		private bool _mouseDown = false;
		private Point _mouseDownPt = new Point(0, 0);
		protected bool _dirty = false;

		public frmBase()
		{
			InitializeComponent();
		}
		public virtual new void Dispose()
		{
			SaveState();
			base.Dispose();
		}

		public virtual Enums.Forms FormType 
		{ 
			get { return Enums.Forms.None;}
		}
		
		#region drawing custom controlbox buttons

		private void btnMin_Load(object sender, EventArgs e)
		{
			this.btnMin.IconShape.Icon = IconShapes.Down;
			this.btnMin.IconShape.Height = 10;
			this.btnMin.IconShape.Top += 6;
		}
		private void btnMax_Load(object sender, EventArgs e)
		{
			this.btnMax.IconShape.Icon = IconShapes.Maximize;
		}

		#endregion

		#region resizing the form

		private void frmBase_Resize(object sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Maximized)
				this.btnMax.IconShape.Icon = IconShapes.Restore;
			else
				this.btnMax.IconShape.Icon = IconShapes.Maximize;
		}
		private void btnMax_Click(object sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Maximized)
				this.WindowState = FormWindowState.Normal;
			else
				this.WindowState = FormWindowState.Maximized;
		}
		private void btnMin_Click(object sender, EventArgs e)
		{
			this.WindowState = FormWindowState.Minimized;
		}
		private void menu_DoubleClick(object sender, EventArgs e)
		{
			this.btnMax_Click(null, null);
		}

		#endregion

		#region moving the form

		private void menu_MouseDown(object sender, MouseEventArgs e)
		{
			_mouseDown = true;
			_mouseDownPt = e.Location;
		}
		private void menu_MouseMove(object sender, MouseEventArgs e)
		{
			if (_mouseDown)
			{
				this.Left += (e.X - _mouseDownPt.X);
				this.Top += (e.Y - _mouseDownPt.Y);
			}
		}
		private void menu_MouseUp(object sender, MouseEventArgs e)
		{
			_mouseDown = false;
		}
		
		#endregion

		#region form events

		protected virtual void form_Load(object sender, EventArgs e)
		{
			LoadState();
		}
		protected virtual void form_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (!CheckForDirty())
			{
				SaveState();
				App.CloseForm(this.FormType);
			}
			else
				e.Cancel = true;
		}
		protected virtual void form_KeyUp(object sender, KeyEventArgs e)
		{
			// provides hotkey access to the menu commands
			if (e.Alt)
				switch (e.KeyCode)
				{
					case Keys.A:
						this.btnAdmin.Focus();
						this.btnAdmin_Click(null, null);
						e.Handled = true;
						break;
					case Keys.C:
						this.btnCycles.Focus();
						this.btnCycles_Click(null, null);
						e.Handled = true;
						break;
					case Keys.L:
						this.btnLogs.Focus();
						this.btnLogs_Click(null, null);
						e.Handled = true;
						break;
				}			
		}
		private void btnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		protected virtual bool CheckForDirty()
		{
			bool cancel = false;
			if (_dirty)
				switch (MsgBox.Show("Save Changes before closing?",
									"Settings Changed",
									MessageBoxButtons.YesNoCancel,
									MessageBoxIcon.Question))
				{
					case DialogResult.Yes:
						cancel = !Save();
						break;
					case DialogResult.Cancel:
						cancel = true;
						break;
				}
			return cancel;
		}
		protected virtual bool Save()
		{
			_dirty = false;
			return true;
		}
		#endregion

		#region menu command buttons

		#region *	managing visual cues
		private void command_MouseDown(object sender, MouseEventArgs e)
		{
			((RoundedRectangle)sender).FillStyle = Shape.FillStyles.Sunken;
		}
		private void command_MouseUp(object sender, MouseEventArgs e)
		{
			((RoundedRectangle)sender).FillStyle = Shape.FillStyles.Raised;
		}
		private void command_MouseLeave(object sender, EventArgs e)
		{
			((RoundedRectangle)sender).FillStyle = Shape.FillStyles.Raised;
		}
		private void command_Enter(object sender, EventArgs e)
		{
			if (sender.Equals(btnAdmin))
				textAdmin.BackColor = Color.Lime;
			else if (sender.Equals(btnCycles))
				textCycles.BackColor = Color.Lime;
			else if (sender.Equals(btnLogs))
				textLogs.BackColor = Color.Lime;
		}		
		private void command_Leave(object sender, EventArgs e)
		{
			if (sender.Equals(btnAdmin))
				textAdmin.BackColor = Color.White;
			else if (sender.Equals(btnCycles))
				textCycles.BackColor = Color.White;
			else if (sender.Equals(btnLogs))
				textLogs.BackColor = Color.White;
		}
		#endregion

		#region *	menu commands
		
		private void btnLogs_Click(object sender, EventArgs e)
		{
			App.OpenForm(Enums.Forms.Logs);
		}
		private void btnCycles_Click(object sender, EventArgs e)
		{
			App.OpenForm(Enums.Forms.Cycles);
		}
		private void btnAdmin_Click(object sender, EventArgs e)
		{
			App.OpenForm(Enums.Forms.Admin);
		}

		#endregion

		#endregion

		#region form settings

		protected virtual void SaveState()
		{
			if (this.WindowState == FormWindowState.Normal)
			{
				TextSettings.GetInstance()[SettingTypes.User, this.Name, "Left"] = this.Left.ToString();
				TextSettings.GetInstance()[SettingTypes.User, this.Name, "Top"] = this.Top.ToString();
				TextSettings.GetInstance()[SettingTypes.User, this.Name, "Height"] = this.Height.ToString();
				TextSettings.GetInstance()[SettingTypes.User, this.Name, "Width"] = this.Width.ToString();
				TextSettings.GetInstance()[SettingTypes.User, this.Name, "WindowState"] = this.WindowState.ToString();
			}
			else if (this.WindowState == FormWindowState.Maximized)
				TextSettings.GetInstance()[SettingTypes.User, this.Name, "WindowState"] = this.WindowState.ToString();
		}
	
		protected virtual void LoadState()
		{
			string wrk = string.Empty;

			wrk = TextSettings.GetInstance()[SettingTypes.User, this.Name, "Left"];
			if (!wrk.Equals(string.Empty))
				this.Left = Convert.ToInt32(wrk);

			wrk = TextSettings.GetInstance()[SettingTypes.User, this.Name, "Width"];
			if (!wrk.Equals(string.Empty))
				this.Width = Convert.ToInt32(wrk);

			wrk = TextSettings.GetInstance()[SettingTypes.User, this.Name, "Top"];
			if (!wrk.Equals(string.Empty))
				this.Top = Convert.ToInt32(wrk);

			wrk = TextSettings.GetInstance()[SettingTypes.User, this.Name, "Height"];
			if (!wrk.Equals(string.Empty))
				this.Height = Convert.ToInt32(wrk);

			wrk = TextSettings.GetInstance()[SettingTypes.User, this.Name, "WindowState"];
			if (!wrk.Equals(string.Empty))
				this.WindowState = (FormWindowState)Enum.Parse(typeof(FormWindowState), wrk);
		}

		#endregion
	}
}