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;
using VEPROMS.CSLA.Library;
using DevComponents.DotNetBar;

namespace VEPROMS
{
	public partial class frmRODbProperties : Form
	{
		private ProgressBarItem _ProgressBar = null;
		public ProgressBarItem ProgressBar
		{
			get { return _ProgressBar; }
			set { _ProgressBar = value; }
		}
		private void DoProgressBarRefresh(int value, int max, string text)
		{
			if (ProgressBar == null) return;
			ProgressBar.Value = value;
			ProgressBar.Maximum = max;
			ProgressBar.Text = text;
			Application.DoEvents();
		}
		private string InitialProgressBarMessage
		{
			set
			{
				if (ProgressBar == null) return;
				ProgressBar.Value = 100;
				ProgressBar.Maximum = 100;
				ProgressBar.Text = value;
				Application.DoEvents();
			}
		}
		private string FinalProgressBarMessage
		{
			set
			{
				if (ProgressBar == null) return;
				ProgressBar.Value = 100;
				ProgressBar.Maximum = 100;
				ProgressBar.Text = value;
				Application.DoEvents();
			}
		}
		private Point _ParentLocation;
		public Point ParentLocation
		{
			get { return _ParentLocation; }
			set { _ParentLocation = value; }
		}
		private RODbInfo _roDbInfo;
		private string _origROName;
		private string _origFolderPath;
		private DocVersion _docVersion;
		public frmRODbProperties(DocVersion docVersion, RODbInfo roDbInfo)
		{
			_roDbInfo = roDbInfo;
			_docVersion = docVersion;
			InitializeComponent();
			_origROName = (_roDbInfo == null) ? null : _roDbInfo.ROName;
			_origFolderPath = (_roDbInfo == null) ? null : _roDbInfo.FolderPath;
			// Disable the OK button when initialized.  Enable it if the user makes changes
			ppBtnOk.Enabled = false;
		}

		private void ppBtnFldrDlg_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog dlgROFolder = new FolderBrowserDialog();
			// Initialize the starting location in the Browser window
			dlgROFolder.SelectedPath = ppTxtPath.Text;
			if (dlgROFolder.ShowDialog() == DialogResult.OK)
			{
				ppTxtPath.Text = dlgROFolder.SelectedPath;
			}
		}

		private void ppBtnOk_Click(object sender, EventArgs e)
		{
			// check for modify.  If the path to this rodb already exists, we've got to use that
			// as the assocation. if it's a new path, i.e. a modify of the rodb record, just save current data. 
			// Then close & return.
			if (_origROName !=null && _origFolderPath !=null && (_origROName != ppRTxtName.Text || _origFolderPath != ppTxtPath.Text))
			{
				if (!Directory.Exists(ppTxtPath.Text))
				{
					MessageBox.Show(ppTxtPath.Text, "Invalid Directory for RO Database");
					return;
				}
				if (!File.Exists(ppTxtPath.Text + @"\ro.fst"))
				{
					MessageBox.Show(ppTxtPath.Text, "Ro.Fst Doesn't Exist");
					return;
				}
				RODb roDb = RODb.GetJustRoDb(_roDbInfo.RODbID);
				if (_origROName != ppRTxtName.Text) roDb.ROName = ppRTxtName.Text;
				if (_origFolderPath != ppTxtPath.Text)
				{
					// see if an rodb already exists with this pathname.
					RODbInfoList roDbList = RODbInfoList.Get();
					foreach (RODbInfo rdi in roDbList)
					{
						if (ppTxtPath.Text.ToUpper() == rdi.FolderPath.ToUpper())
						{
							// if this has a previously defined path and a different name, do not save it.
							if(ppRTxtName.Text.ToUpper() != rdi.ROName.ToUpper())
							{
								MessageBox.Show(string.Format("This path has been used for \"{0}\" Database",rdi.ROName), "Previously Used RO Path", MessageBoxButtons.OK, MessageBoxIcon.Warning);
								return;
							}
							// get the most recent rofst data for this rodb:
							ROFstInfoList roFstList = ROFstInfoList.GetByRODbID(rdi.RODbID);
							DateTime dts = new DateTime(1900, 1, 1);
							int rofstinfoid = -1;
							foreach (ROFstInfo rfi in roFstList)
							{
								if (rfi.DTS > dts)
								{
									dts = rfi.DTS;
									rofstinfoid = rfi.ROFstID;
								}
							}
							_docVersion.DocVersionAssociations[0].MyROFst = ROFst.Get(rofstinfoid);
							_docVersion.Save().Dispose();
							DialogResult = DialogResult.OK;
							Close();
							return;
						}
					}
					roDb.FolderPath = ppTxtPath.Text;
				}
				try
				{
					roDb.Save().Dispose();
					DialogResult = DialogResult.OK;
					Close();
					return;
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message);
					return;
				}
			}

			// this is creating a new rodb.  A new rofst record will be created as well & the association set
			// check for unique name/path first
			RODbInfoList allrodbinfo = RODbInfoList.Get();
			foreach (RODbInfo rdi in allrodbinfo)
			{
				if (ppRTxtName.Text == rdi.ROName)
				{
					MessageBox.Show("Create unique Description.", "Description is not unique");
					return;
				}
				if (ppTxtPath.Text.ToUpper() == rdi.FolderPath.ToUpper())
				{
					MessageBox.Show("Specify unique Folder Path or cancel and select an existing RO Database from drop-down.", "Folder Path is not unique" );
					return;
				}
			}
			if (ppRTxtName.Text == null || ppRTxtName.Text == "")
			{
				MessageBox.Show("You must enter a Description.");
				return;
			}
			if (ppTxtPath.Text == null || ppTxtPath.Text == "")
			{
				MessageBox.Show("You must enter a Path.");
				return;
			}

			// check for an ro.fst existing in the selected path.  If it is valid, create an rodb, rofst 
			// and an association to its docversion.
			string rofstPath = ppTxtPath.Text + @"\ro.fst";
			if (!File.Exists(rofstPath))
			{
				MessageBox.Show("No existing ro.fst in path " + ppTxtPath.Text + ". Check for invalid path");
				return;
			}
			RODb newroDb = RODb.MakeRODb(ppRTxtName.Text, ppTxtPath.Text, "cstring", null);
			RODbInfo newrdi = RODbInfo.Get(newroDb.RODbID);
			newroDb.Save().Dispose();
			ROFst rofst = ROFstInfo.AddRoFst(newrdi, _docVersion, DoProgressBarRefresh);
			DialogResult = DialogResult.OK;
			Close();
		} 
		private void frmRODbProperties_Load(object sender, EventArgs e)
		{
			Location = ParentLocation;
			ppRTxtName.Text = (_roDbInfo == null) ? null : _roDbInfo.ROName;
			ppTxtPath.Text = (_roDbInfo == null) ? null : _roDbInfo.FolderPath;
			RODbConfig cfg = (_roDbInfo == null) ? new RODbConfig() : new RODbConfig(_roDbInfo);
			// Note that the Graphic Extension data is shown in a non-editable text box here because
			// the data is either retrieved from the roapp.ini - and if there can only be editted from 
			// the file system. Otherwise it is derived from the top node - and changed from folder
			// properties on the top node.  And lastly, it is the code default.
			ppTxtExt.Text = cfg.GetDefaultGraphicExtension();
			ppLblGraphicFileExtLoc.Text = (cfg == null) ? null : cfg.GetDefaultGraphicExtensionLocation();
			ppBtnOk.Enabled = false;
			if (_roDbInfo == null)
			{
				string newFolderPath = null;
				FolderBrowserDialog dlgROFolder = new FolderBrowserDialog();
				dlgROFolder.RootFolder = Environment.SpecialFolder.MyComputer;
				dlgROFolder.SelectedPath = ppTxtPath.Text;
				if (dlgROFolder.ShowDialog() == DialogResult.OK)
				{
					newFolderPath = dlgROFolder.SelectedPath;
					RODbInfoList allRODBs = RODbInfoList.Get();
					foreach(RODbInfo rodb in allRODBs)
					{
						if (newFolderPath.ToUpper() == rodb.FolderPath.ToUpper())
						{
							ppRTxtName.Text = rodb.ROName;
							break;
						}
					}
					ppBtnOk.Enabled = true;
				}
				ppTxtPath.Text = newFolderPath;
			}
			// Initialize the enabled status for the OK button to false.
			//ppBtnOk.Enabled = false;
			ppTxtPath.TextChanged += ppTxtPath_TextChanged;
		}
		void ppTxtPath_TextChanged(object sender, EventArgs e)
		{
			//The OK button should only be enabled if the path has changed
			ppBtnOk.Enabled = _origFolderPath != ppTxtPath.Text;
		}
	}
}