using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Sync
{
	class FileCompare
	{
		private FileInfo _Developement;
		private FileInfo _SourceSafe;
		private bool _ToProcess = false;
		public bool ToProcess
		{
			get { return _ToProcess; }
			set { _ToProcess = value; }
		}
		public string FileName
		{
			get { return _Developement.FullName; }
		}
		public Nullable<DateTime> DevModified
		{
			get
			{
				if (_Developement.Exists)
					return _Developement.LastWriteTime;
				return null;
			}
		}
		public Nullable<DateTime> SSModified
		{
			get 
			{
				if(_SourceSafe.Exists)
					return _SourceSafe.LastWriteTime;
				return null;
			}
		}
		private bool _Different;
		public bool Different
		{
			get { return _Different; }
		}
		public string SSFileName
		{
			get { return _SourceSafe.FullName; }
		}
		private bool _ReadOnly;
		public bool ReadOnly
		{
			get { return _ReadOnly; }
		}
		private bool _Merge = false;
		public bool Merge
		{
			get { return _Merge; }
			set { _Merge = value; }
		}
		public FileCompare(FileInfo development, FileInfo sourceSafe)
		{
			_Developement = development;
			_SourceSafe = sourceSafe;
			_Different = DifferentContents(development, sourceSafe);
			_ToProcess = _Different;
			_ReadOnly = (development.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
		}
		public static bool DifferentContents(FileInfo development, FileInfo sourceSafe)
		{
			string strDev =ReadFile(development);
			string strSS = ReadFile(sourceSafe);
			return strDev != strSS;
		}
		private static string ReadFile(FileInfo fi)
		{
			if (fi.Exists == false) return "";
			TextReader tr = fi.OpenText();
			string retval = tr.ReadToEnd();
			tr.Close();
			return retval;
		}
		public void MoveToDevelopment()
		{
			if (_Developement.Exists)
			{
				_Developement.Attributes &= (_Developement.Attributes ^ FileAttributes.ReadOnly);// Turn-Off ReadOnly Attribute
				_Developement.Attributes &= (_Developement.Attributes ^ FileAttributes.Hidden);// Turn-Off Hidden Attribute
			}
			BuildFolder(_Developement.Directory);
			if(_SourceSafe.Exists)_SourceSafe.CopyTo(_Developement.FullName , true);
		}
		public void MoveDevToMailBox(string devFolder, string mailboxFolder)
		{
			if (ToProcess)
			{
				FileInfo fi1 = new FileInfo(_Developement.FullName.Replace(devFolder, mailboxFolder));
				BuildFolder(fi1.Directory);
				if (fi1.Exists) fi1.IsReadOnly = false;
				if(_Developement.Exists)_Developement.CopyTo(fi1.FullName,true);
			}
		}
		public void MoveMailBoxToDev(string devFolder, string mailboxFolder)
		{
			if (ToProcess)
			{
				FileInfo fi1 = new FileInfo(_Developement.FullName.Replace(devFolder, mailboxFolder));
				BuildFolder(_Developement.Directory);
				if (fi1.Exists) fi1.IsReadOnly = false;
				if (_Developement.Exists) fi1.CopyTo(_Developement.FullName, true);
			}
		}
		public void MoveSSToMailBox(string SSFolder, string mailboxFolder)
		{
			if (ToProcess)
			{
				FileInfo fi1 = new FileInfo(_SourceSafe.FullName.Replace(SSFolder, mailboxFolder));
				BuildFolder(fi1.Directory);
				if (fi1.Exists) fi1.IsReadOnly = false;
				if (_SourceSafe.Exists) _SourceSafe.CopyTo(fi1.FullName, true);
			}
		}
		public void MoveMailBoxToSS(string SSFolder, string mailboxFolder)
		{
			if (ToProcess)
			{
				FileInfo fi1 = new FileInfo(_SourceSafe.FullName.Replace(SSFolder, mailboxFolder));
				BuildFolder(fi1.Directory);
				if (fi1.Exists) fi1.IsReadOnly = false;
				if (_SourceSafe.Exists) fi1.CopyTo(_SourceSafe.FullName, true);
			}
		}
		private static void BuildFolder(DirectoryInfo directoryInfo)
		{
			if (!directoryInfo.Exists)
			{
				BuildFolder(directoryInfo.Parent);
				directoryInfo.Create();
			}
		}
	}
}
