using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using DescriptiveEnum;
namespace VEPROMS.CSLA.Library
{
	[Serializable]
	[TypeConverter(typeof(ExpandableObjectConverter))]
	public class RODbConfig : ConfigDynamicTypeDescriptor, INotifyPropertyChanged
	{
		#region DynamicTypeDescriptor
		internal override bool IsReadOnly
		{
			get { return _RODb == null; }
		}
		#endregion
		#region XML
		private XMLProperties _Xp;
		private XMLProperties Xp
		{
			get { return _Xp; }
		}
		#endregion
		#region Constructors
		private RODb _RODb;
		public RODbConfig(RODb roDb)
		{
			_RODb = roDb;
			string xml = roDb.Config;
			if (xml == string.Empty) xml = "";
			_Xp = new XMLProperties(xml);
		}
		private RODbInfo _RODbInfo;
		public RODbConfig(RODbInfo roDbInfo)
		{
			_RODbInfo = roDbInfo;
			string xml = roDbInfo.Config;
			if (xml == string.Empty) xml = "";
			_Xp = new XMLProperties(xml);
		}
		public RODbConfig(string xml)
		{
			if (xml == string.Empty) xml = "";
			_Xp = new XMLProperties(xml);
		}
		public RODbConfig()
		{
			string xml = "";
			_Xp = new XMLProperties(xml);
		}
		internal string GetValue(string group, string item)
		{
			return _Xp[group, item];
		}
		#endregion
		#region Local Properties
		[Category("General")]
		[DisplayName("ROName")]
		[Description("ROName")]
		public string ROName
		{
			get { return (_RODb != null ? _RODb.ROName : _RODbInfo.ROName); }
			set { if (_RODb != null) _RODb.ROName = value; }
		}
		[Category("General")]
		[DisplayName("FolderPath")]
		[Description("FolderPath")]
		public string FolderPath
		{
			get { return (_RODb != null ? _RODb.FolderPath : _RODbInfo != null?_RODbInfo.FolderPath:null); }
			set { if (_RODb != null) _RODb.FolderPath = value; }
		}
        public RODb MyRODb
        { get { return _RODb; } }
		#endregion
		#region ToString
		public override string ToString()
		{
			string s = _Xp.ToString();
			if (s == "" || s == "") return string.Empty;
			return s;
		}
		#endregion
		#region RODefaults
		[Category("Referenced Objects")]
		[DisplayName("Graphic File Extension")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Default File Extension")]
		public string Graphics_defaultext
		{
			get
			{
				// For this data:
				//  look in roapp.ini in the FolderPath directory;
				//  look in top folder's config
				//  set to Volian default, i.e. "TIF".
				string s = GetRoAppIniValue("ROAPP", "Extention");
				if (s == null || s == string.Empty)
					s = TopFolderConfigValue("Graphics", "defaultext");
				if (s == null || s == string.Empty)
					return s = "TIF";
				return s;
			}
		}
		private string GetRoAppIniValue(string p, string p_2)
		{
			if (FolderPath == null) return null;
			string inipath = FolderPath + @"\roapp.ini";
			if (!File.Exists(inipath)) return null;
			StreamReader myReader = new StreamReader(inipath);
			string sLine;
			int indx = -1;
			while ((sLine = myReader.ReadLine()) != null)
			{
				if (sLine.Length > 0 && sLine.Substring(0, 1) != ";")
				{
					if ((indx = sLine.ToLower().IndexOf(p_2.ToLower())) >= 0)
					{
						indx = sLine.IndexOf("=", indx + 9);
						return sLine.Substring(indx + 1, sLine.Length - indx - 1).Trim();
					}
				}
			}
			myReader.Close();
			return null;
		}
		private string TopFolderConfigValue(string p, string p_2)
		{
			FolderInfo fi = FolderInfo.GetTop();
			return fi.FolderConfig.Graphics_defaultext;
		}
		public string GetDefaultGraphicExtension()
		{
			if (_RODb != null)
				return Graphics_defaultext;
			if (_RODbInfo != null)
				return Graphics_defaultext;
			else
				return "TIF"; // this is the Volian Default
		}
		public string GetDefaultGraphicExtensionLocation()
		{
			string s = GetRoAppIniValue("ROAPP", "Extention");
			if (s != null && s != string.Empty) return "Default Extension found in roapp.ini";
			s = TopFolderConfigValue("Graphics", "defaultext");
			if (s != null && s != string.Empty) return "Default Extension defined in veproms properties";
			return "Used program default";
		}
		#endregion
	}
}