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

namespace SyncMany
{
	public partial class frmSyncMany : Form
	{
		public string MyStatus
		{
			get { return tsslStatus.Text; }
			set { tsslStatus.Text = value; Application.DoEvents(); }
		}
		private string _MyFileName;
		public string MyFileName
		{
			get { return _MyFileName; }
			set { _MyFileName = value; ofd.FileName = value; sfd.FileName = value; }
		}
		SyncFile _MySyncFile = new SyncFile();
		public SyncFile MySyncFile
		{
			get { return _MySyncFile; }
			set 
			{ 
				_MySyncFile = value;
				ResetOriginalSync();
				dgv.DataSource = value.Items; 
				dgv.AutoResizeColumns(); 
			}
		}
		private void ResetOriginalSync()
		{
			_OriginalSync = ObjectSerializer<SyncFile>.StringSerialize(MySyncFile);
		}
		private string _SourceFolder;
		public string SourceFolder
		{
			get { return _SourceFolder; }
			set { _SourceFolder = value; }
		}
		private string _DestinationFolder;
		public string DestinationFolder
		{
			get { return _DestinationFolder; }
			set { _DestinationFolder = value; }
		}
		public frmSyncMany()
		{
			InitializeComponent();
			compareItemsBindingSource.DataSource = MySyncFile.Items;
		}
		public string _OriginalSync = "";
		public bool IsDirty
		{
			get
			{
				string tmp = ObjectSerializer<SyncFile>.StringSerialize(MySyncFile);
				return tmp != _OriginalSync;
			}
		}
		private void openToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (ofd.ShowDialog() == DialogResult.OK)
			{
				MySyncFile = ObjectSerializer<SyncFile>.ReadFile(MyFileName);
			}
		}
		private void saveToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (MyFileName == null)
				saveAsToolStripMenuItem_Click(sender, e);
			else
			{
				ObjectSerializer<SyncFile>.WriteFile(MySyncFile, MyFileName);
				ResetOriginalSync();
			}
		}
		private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (sfd.ShowDialog() == DialogResult.OK)
			{
				ObjectSerializer<SyncFile>.WriteFile(MySyncFile, MyFileName);
				ResetOriginalSync();
			}
		}
		private void ofd_FileOk(object sender, CancelEventArgs e)
		{
			MyFileName = ofd.FileName;
		}
		private void sfd_FileOk(object sender, CancelEventArgs e)
		{
			MyFileName = sfd.FileName;
		}
		private void addToolStripMenuItem_Click(object sender, EventArgs e)
		{
			frmAddCompareItem myCompareItem = new frmAddCompareItem(SourceFolder,DestinationFolder);
			if (myCompareItem.ShowDialog() == DialogResult.OK)
			{
				SourceFolder = myCompareItem.SourceFolder;
				DestinationFolder = myCompareItem.DestinationFolder;
				MySyncFile.Items.Add(new CompareItem(myCompareItem.SourceName, myCompareItem.DestinationName));
				//compareItemsBindingSource.Add(new CompareItem(myCompareItem.SourceName, myCompareItem.DestinationName));
				compareItemsBindingSource.DataSource = null;
				compareItemsBindingSource.DataSource = MySyncFile.Items;
				dgv.DataSource = null;
				dgv.DataSource = compareItemsBindingSource;
				dgv.Refresh();
			}
		}
		private void compareToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			foreach (DataGridViewRow row in dgv.SelectedRows)
			{
				CompareItem item = row.DataBoundItem as CompareItem;
				if (item != null)
				{
					System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"C:\Program Files\IDM Computer Solutions\UltraCompare\UC.exe",
						string.Format(@" -t ""{0}"" ""{1}""", item.Source, item.DestinationItem));
					System.Diagnostics.Process prc = System.Diagnostics.Process.Start(psi);
				}
			}
		}
		private void frmSyncMany_Load(object sender, EventArgs e)
		{
			LoadSettings();
			this.Move +=new EventHandler(frmSyncMany_Move);
			this.Resize +=new EventHandler(frmSyncMany_Resize);
			FileInfo myInfo = new FileInfo(MyFileName);
			if (myInfo.Exists)
				MySyncFile = ObjectSerializer<SyncFile>.ReadFile(MyFileName);
			else
				MySyncFile = new SyncFile();
		}
		private void copySourceToDestinationToolStripMenuItem_Click(object sender, EventArgs e)
		{
			foreach (DataGridViewRow row in dgv.SelectedRows)
			{
				CompareItem item = row.DataBoundItem as CompareItem;
				if (item != null)
				{
					item.CopySourceToDestination();
				}
				dgv.Refresh();
			}
		}
		private void copyDestinationToSourceToolStripMenuItem_Click(object sender, EventArgs e)
		{
			foreach (DataGridViewRow row in dgv.SelectedRows)
			{
				CompareItem item = row.DataBoundItem as CompareItem;
				if (item != null)
				{
					item.CopyDestinationToSource();
				}
			}
			dgv.Refresh();
		}
		private void frmSyncMany_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (IsDirty)
			{
				DialogResult result = MessageBox.Show("Do you want to Save changes?","Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
				switch (result)
				{
					case DialogResult.Cancel:
						// Don't close
						e.Cancel = true;
						return;
						break;
					case DialogResult.No:
						// Don't do anything
						break;
					case DialogResult.Yes:
						saveToolStripMenuItem_Click(this, e);
						break;
				}
			}
			SaveSettings();
		}
		private string GetPropertyString(string propertyName)
		{
			object prop = Properties.Settings.Default[propertyName];
			return prop == null ? "" : prop.ToString();
		}
		private void LoadSettings()
		{
			string myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
			if (GetPropertyString("Location") != "") 
				this.Location = Properties.Settings.Default.Location;
			if ((Properties.Settings.Default["Size"] ?? "") != "")
				this.Size = Properties.Settings.Default.Size;
			this.WindowState = Properties.Settings.Default.WindowState;
			string test = GetPropertyString("FileName");
			if (GetPropertyString("FileName") != "")
				MyFileName = Properties.Settings.Default.FileName;
			else
				MyFileName = myDocuments + @"\SyncList.XML";
			SetupOpenFileDialog(MyFileName);
			if (GetPropertyString("SourceFolder") != "")
				SourceFolder = Properties.Settings.Default.SourceFolder;
			else
				SourceFolder = myDocuments;
			if (GetPropertyString("DestinationFolder") != "")
				DestinationFolder = Properties.Settings.Default.DestinationFolder;
			else
				DestinationFolder = myDocuments;
		}
		private string SetupOpenFileDialog(string fileName)
		{
			FileInfo fi = new FileInfo(fileName);
			sfd.InitialDirectory = ofd.InitialDirectory = fi.Directory.FullName;
			sfd.FileName = ofd.FileName = fi.Name;
			return ofd.InitialDirectory;
		}
		private void SaveSettings()
		{
			Properties.Settings.Default.WindowState = this.WindowState;
			Properties.Settings.Default.FileName = MyFileName;
			Properties.Settings.Default.SourceFolder = SourceFolder;
			Properties.Settings.Default.DestinationFolder = DestinationFolder;
			Properties.Settings.Default.Save();
		}
		private void frmSyncMany_Move(object sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Normal)
			{
				Properties.Settings.Default.Location = this.Location;
				Properties.Settings.Default.Size = this.Size;
			}
		}
		private void frmSyncMany_Resize(object sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Normal)
			{
				Properties.Settings.Default.Location = this.Location;
				Properties.Settings.Default.Size = this.Size;
			}
		}
		private void removeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			int index = dgv.SelectedRows[0].Index;
			dgv.Rows[index].Selected = false;
			MySyncFile.Items.RemoveAt(index);
			compareItemsBindingSource.DataSource = MySyncFile.Items;
			dgv.DataSource = null;
			dgv.DataSource = compareItemsBindingSource;
			dgv.Refresh();
		}
		private void dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex >= 0)
			{
				dgv.Rows[e.RowIndex].Selected = true;
				dgv.CurrentCell = dgv[0, e.RowIndex];
			}
		}
		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

	}
}