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

namespace SyncMany
{
	[XmlRoot("SyncFile")]
	public class SyncFile
	{
		private CompareItems _Items=new CompareItems();
		[XmlElement("CompareItem")]
		public CompareItems Items
		{
			get { return _Items; }
			set { _Items = value; }
		}
	}
	public partial class CompareItems : List<CompareItem>
	{
	}
	public class CompareItem
	{
		private string _Source;
		[XmlAttribute("Source")]
		public string Source
		{
			get { return _Source; }
			set { _Source = value; }
		}
		private string _Destination;
		[XmlAttribute("Destination")]
		public string DestinationItem
		{
			get { return _Destination; }
			set { _Destination = value; }
		}
		public CompareItem()
		{
		}
		public CompareItem(string source, string destination)
		{
			_Source = source;
			_Destination = destination;
		}
		private FileInfo _SourceFile;
		[XmlIgnore]
		public FileInfo SourceFile
		{
			get 
			{
				if (_SourceFile == null)
					_SourceFile = new FileInfo(_Source);
				return _SourceFile; 
			}
			set { _SourceFile = value; }
		}
		private FileInfo _DestinationFile;
		[XmlIgnore]
		public FileInfo DestinationFile
		{
			get 
			{
				if (_DestinationFile == null)
					_DestinationFile = new FileInfo(_Destination);
				return _DestinationFile; 
			}
			set { _DestinationFile = value; }
		}
		public bool IsDifferent
		{
			get
			{
				string srcText = ReadFile(SourceFile);
				string dstText = ReadFile(DestinationFile);
				return srcText != dstText;
			}
		}
		private string ReadFile(FileInfo myFile)
		{
			string retval;
			using (StreamReader sr = myFile.OpenText())
			{
				retval = sr.ReadToEnd();
				sr.Close();
			}
			return retval;
		}
		public void CopySourceToDestination()
		{
			_DestinationFile.IsReadOnly = false;
			_SourceFile.CopyTo(_Destination,true);
		}
		public void CopyDestinationToSource()
		{
			_SourceFile.IsReadOnly = false;
			_DestinationFile.CopyTo(Source,true);
		}
	}
}
