/******************************************************************************
 * Config.cs 
 * 
 * <Copyright and License> 
 * 
 * This file is part of GODS, a package for annotating medical images. 
 * 
 * Copyright (C) 2004 
 * Xuedong Chen 
 * Heri Mariwa 
 * Kimmy (Hsiang-ling) Lin 
 * Rui Yu 
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 
 * of the License, or (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 *  
 * For further questions about this file, please send email to 
 * gods@cs.umb.edu or visit us at http://gods.sourceforge.net.  
 ******************************************************************************/

using System;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace GODS
{
	/// <summary>
	/// Config is the GODS configuration class that holds infomation
	/// about patient database. It loads the database info from GodsConfig.xml
	/// file and stores new config info to the XML file.
	/// 
	/// GODS Team 2004
	/// </summary>
	public class Config
	{
		private XmlDocument xmlDoc;
		//default configuration xml file is on same directory
		private string configFileName = GODS.GODSMain.ExecDir + GODS.GODSMain.ConfigFile;
		private bool origShowSaveConfirmDlg;
		private string defaultImage;
		private string imageFolder;
		private ArrayList fileList;

		public Config()
		{
			fileList = new ArrayList(GODS.GODSMain.RECENT_IMAGE_NUM+1);
			LoadConfig();
		}

		public ArrayList RecentImages
		{
			get 
			{
				return fileList;
			}
		}

		/// <summary>
		/// load configuration file info.
		/// </summary>
		public void LoadConfig()
		{
			if(!File.Exists(this.configFileName)){ return;}
			bool inFileList = false;
			XmlTextReader reader = new XmlTextReader(this.configFileName);
			while (reader.Read()) 
			{ 
				switch (reader.NodeType) 
				{
					case XmlNodeType.Element: 
						if (reader.Name.Equals("ShowSaveConfirmDlg"))
						{
							this.defaultImage = reader.ReadString().Trim();
							GODS.GODSMain.DefaultImagePath = GODS.GODSMain.ExecDir + this.defaultImage;
						}
						else if (reader.Name.Equals("DefaultImage"))
						{
							GODS.GODSMain.DefaultImagePath = reader.ReadString().Trim();
							this.defaultImage = GODS.GODSMain.DefaultImagePath;
						}
						else if (reader.Name.Equals("ImageFolder"))
						{
							GODS.GODSMain.ImageDir = reader.ReadString().Trim();
							this.imageFolder = GODS.GODSMain.ImageDir;
						}
						else if (reader.Name.Equals("ImageFiles"))
						{
							inFileList = true;
						}
						else if (inFileList && reader.Name.Equals("ImageFile"))
						{
							if (this.fileList.Count < this.fileList.Capacity)
								this.fileList.Add(reader.ReadString().Trim());
						}
						break;
					case XmlNodeType.EndElement:
						if (inFileList && reader.Name.Equals("ImageFiles"))
						{
							inFileList = false;
						}
						break;
				}
			}
			reader.Close();
		}

		/// <summary>
		/// save to XML configuration file
		/// </summary>
		public void SaveConfig()
		{
			this.xmlDoc = new XmlDocument();
			XmlElement root = xmlDoc.CreateElement("", "Config", "");
			xmlDoc.AppendChild(root);

			XmlElement elem = xmlDoc.CreateElement("", "ShowSaveConfirmDlg", "");
			elem.InnerText = GODS.GODSMain.showSaveConfirmDlg? "true" : "false";
			root.AppendChild(elem);

			elem = xmlDoc.CreateElement("", "DefaultImage", "");
			elem.InnerText = this.defaultImage;
			root.AppendChild(elem);

			elem = xmlDoc.CreateElement("", "ImageFolder", "");
			elem.InnerText = GODS.GODSMain.ImageDir;
			root.AppendChild(elem);

			elem = xmlDoc.CreateElement("", "ImageFiles", "");
			XmlElement elem2;
			foreach (string str in this.fileList)
			{
				elem2 = xmlDoc.CreateElement("", "ImageFile", "");
				elem2.InnerText = str;
				elem.AppendChild(elem2);
			}
			root.AppendChild(elem);

			try
			{
				xmlDoc.Save(this.configFileName);
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message);
			}
		}
	}
}
