using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using DescriptiveEnum;
using System.Xml;
namespace VEPROMS.CSLA.Library
{
	[Serializable]
	[TypeConverter(typeof(ExpandableObjectConverter))]
	public class ProcedureConfig : ConfigDynamicTypeDescriptor, INotifyPropertyChanged, IItemConfig
	{
		#region DynamicTypeDescriptor
		internal override bool IsReadOnly
		{
			get { return _Procedure == null; }
		}
		#endregion
		#region XML
		private XMLProperties _Xp;
		private XMLProperties Xp
		{
			get { return _Xp; }
		}
		#endregion
		#region Constructors
		//PROPGRID: Hide ParentLookup
		[Browsable(false)]
		public bool ParentLookup
		{
			get { return _Xp.ParentLookup; }
			set { _Xp.ParentLookup = value; }
		}
		//PROPGRID: Needed to comment out [NonSerialized] in order to hide AncestorLookup from property grid
		//[NonSerialized]
		//private bool _AncestorLookup;
		////PROPGRID: Hide AncestorLookup
		//[Browsable(false)]
		//public bool AncestorLookup
		//{
		//  get { return _AncestorLookup; }
		//  set { _AncestorLookup = value; }
		//}
		private Procedure _Procedure;
		private ProcedureInfo _ProcedureInfo;
		public ProcedureConfig(Procedure procedure)
		{
			_Procedure = procedure;
			string xml = procedure.MyContent.Config;
      if (xml == string.Empty)
          xml = "";
			_Xp = new XMLProperties(xml);
			// Correct Slaves nodes for Parent Child
			ValidateSlaves(_Xp.XmlContents, _Procedure.MyProcedureInfo.MyDocVersion.MultiUnitCount);
			if (procedure.MyProcedureInfo.ActiveParent != null) _Xp.LookInAncestor += new XMLPropertiesEvent(Xp_LookInAncestorFolder);
		}
		/// 
		/// Fix Slaves Nodes for Parent Child
		/// 
		/// At some point it may be better to use a more forrmal structure to indentify which nodes are required and
		/// which are optional.
		/// 
		/// XmlDocument
		/// Number of slave nodes supported
		private static void ValidateSlaves(XmlDocument xd, int unitCount)
		{
			if (unitCount < 1) return; // No Slave nodes necessary
			XmlNode xn = xd.DocumentElement.SelectSingleNode("Slaves");
			if (xn == null)// Missing required Slaves node - Add it.
			{
				XmlElement xe = xd.CreateElement("Slaves");
				xn = xd.DocumentElement.AppendChild(xe);
			}
			for (int i = 1; i <= unitCount; i++)
			{
				XmlNode xnc = xn.SelectSingleNode(string.Format("Slave[@index='{0}']", i));
				if (xnc == null) // Missing Required Slave Node (index=i) - Add it.
				{
					XmlElement xec = xd.CreateElement("Slave");
					xnc = xn.AppendChild(xec);
					XmlAttribute xa = xd.CreateAttribute("index");
					xa.Value = i.ToString();
					xnc.Attributes.Append(xa);
				}
			}
		}
		private string Xp_LookInAncestorFolder(object sender, XMLPropertiesArgs args)
		{
			if (args.AncestorLookup || ParentLookup)
			{
				string retval;
				// There may be levels of procedures, i.e. procedures within a procedure.
				ProcedureInfo proc = _Procedure != null ? _Procedure.MyProcedureInfo : _ProcedureInfo;
				while (proc.ActiveParent.IsProcedure)
				{
					proc = (ProcedureInfo)proc.ActiveParent;
					retval = proc.ProcedureConfig.GetValue(args.Group, args.Item);
					if (retval != string.Empty) return retval;
				}
				DocVersionInfo docVersion = proc.ActiveParent as DocVersionInfo;
				if (docVersion == null) return string.Empty;
				retval = docVersion.DocVersionConfig.GetValue(args.Group, args.Item);
				if (retval != string.Empty) return retval;
				for (FolderInfo folder = docVersion.MyFolder; folder != null; folder = folder.MyParent)
				{
					retval = folder.FolderConfig.GetValue(args.Group, args.Item);
					if (retval != string.Empty) return retval;
				}
			}
			return string.Empty;
		}
		//private string Xp_LookInAncestorFolderInfo(object sender, XMLPropertiesArgs args)
		//{
		//  if (args.AncestorLookup || ParentLookup)
		//  {
		//    DocVersionInfo docVersion = (DocVersionInfo)_Procedure.ActiveParent;
		//    string retval = docVersion.DocVersionConfig.GetValue(args.Group, args.Item);
		//    if (retval != string.Empty) return retval;
		//    for (FolderInfo folder = docVersion.MyFolder; folder != null; folder = folder.MyParent)
		//    {
		//      retval = folder.FolderConfig.GetValue(args.Group, args.Item);
		//      if (retval != string.Empty) return retval;
		//    }
		//  }
		//  return string.Empty;
		//}
		public ProcedureConfig(ProcedureInfo procedureInfo)
		{
			_ProcedureInfo = procedureInfo;
			string xml = procedureInfo.MyContent.Config;
      if (xml == string.Empty)
          xml = "";
      _Xp = new XMLProperties(xml);
			// Fix Slaves nodes for Parent Child
			ValidateSlaves(_Xp.XmlContents, _ProcedureInfo.MyDocVersion.MultiUnitCount);
			_Xp.AncestorLookup = true;
			if (procedureInfo.ActiveParent != null) _Xp.LookInAncestor += new XMLPropertiesEvent(Xp_LookInAncestorFolder);
		}
		private int _SelectedSlave = 0;
		//[Browsable(false)]
		public int SelectedSlave
		{
			get { return _SelectedSlave; }
			set { _SelectedSlave = value; }
		}
		public ProcedureConfig(string xml)
		{
			if (xml == string.Empty) xml = "";
			_Xp = new XMLProperties(xml);
		}
		//public ProcedureConfig()
		//{
		//  _Xp = new XMLProperties();
		//}
		public string GetValue(string group, string item)
		{
			return _Xp[group, item];
		}
		public void SetValue(string group, string item, string newvalue)
		{
			_Xp[group, item] = newvalue;
		}
		public int GetItemId()
		{
			if (_Procedure != null) return _Procedure.ItemID;
			if (_ProcedureInfo != null) return _ProcedureInfo.ItemID;
			return -1;
		}
		#endregion
		#region Local Properties
		[Category("General")]
		[DisplayName("Number")]
		[Description("Number")]
		public string Number
		{
			get { return (_Procedure != null ? _Procedure.MyContent.Number : _ProcedureInfo.MyContent.Number); }
			set { if (_Procedure != null) _Procedure.MyContent.Number = value; }
		}
		[Category("General")]
		[DisplayName("Title")]
		[Description("Title")]
		public string Title
		{
			get { return (_Procedure != null ? _Procedure.MyContent.Text : _ProcedureInfo.MyContent.Text); }
			set { if (_Procedure != null) _Procedure.MyContent.Text = value; }
		}
		[Category("Identification")]
		//PROPGRID: Hide Old Sequence
		[Browsable(false)]
		[DisplayName("Old Sequence")]
		[Description("Old Sequence")]
		public string OldSequence
		{
			get { return (_Procedure != null ? _Procedure.MyContent.MyZContent.OldStepSequence : (_ProcedureInfo.MyContent.MyZContent == null ? null : _ProcedureInfo.MyContent.MyZContent.OldStepSequence)); }
			set { if (_Procedure != null) _Procedure.MyContent.MyZContent.OldStepSequence = value; }
		}
		[Category("Identification")]
		//PROPGRID: Hide Dirty
		[Browsable(false)]
		[DisplayName("Dirty")]
		[Description("Dirty")]
		public bool Dirty
		{
			get { return (_Procedure != null ? _Procedure.IsDirty : false); }
		}
		[Category("Format Settings")]
		[DisplayName("Format")]
		[Description("Format")]
		[TypeConverter(typeof(FormatList))]
		public string FormatSelection
		{
			get
			{
				if (_Procedure != null && _Procedure.MyContent.MyFormat != null) return _Procedure.MyContent.MyFormat.FullName; //.PlantFormat.FormatData.Name;
				if (_ProcedureInfo != null && _ProcedureInfo.MyContent.MyFormat != null) return _ProcedureInfo.MyContent.MyFormat.FullName;//.PlantFormat.FormatData.Name;
				return null;
			}
			set
			{
				if (_Procedure != null)
				{
					_Procedure.MyContent.MyFormat = FormatList.ToFormat(value); // Can only be set if _DocVersion is set
					//_Procedure.ActiveFormat = null;
				}
			}
		}
		[Category("Format Settings")]
		[DisplayName("Default Format")]
		[Description("Default Format")]
		[TypeConverter(typeof(FormatList))]
		public string DefaultFormatSelection
		{
			get
			{
				if (_Procedure != null && _Procedure.MyProcedureInfo.ActiveParent != null && _Procedure.MyProcedureInfo.ActiveParent.ActiveFormat != null) return _Procedure.MyProcedureInfo.ActiveParent.ActiveFormat.FullName;
				if (_ProcedureInfo != null && _ProcedureInfo.MyParent != null && _ProcedureInfo.MyParent.ActiveFormat != null) return _ProcedureInfo.MyParent.ActiveFormat.FullName;
				return null;
			}
		}
		public FormatInfo DefaultFormat
		{
			get
			{
				if (_Procedure != null && _Procedure.MyProcedureInfo.ActiveParent != null && _Procedure.MyProcedureInfo.ActiveParent.ActiveFormat != null) return FormatInfo.Get(_Procedure.MyProcedureInfo.ActiveParent.ActiveFormat.FormatID);
				if (_ProcedureInfo != null && _ProcedureInfo.MyParent != null && _ProcedureInfo.MyParent.ActiveFormat != null) return _ProcedureInfo.MyParent.ActiveFormat;
				return null;
			}
		}
		public Procedure MyProcedure
		{ get { return _Procedure; } }
		private bool _CreatingNew = false;
		public bool CreatingNew
		{
			get { return _CreatingNew; }
			set { _CreatingNew = value; }
		}
		#endregion
		#region ToString
		public override string ToString()
		{
			string s = _Xp.ToString();
			if (s == "" || s == "") return string.Empty;
			return s;
		}
		#endregion
		#region DelProcReason
		[Category("General")]
		[DisplayName("DelProcReason")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Delete Procedure Reason")]
		public string General_DelProcReason  // C2020-038: reason was added when procedure was deleted
		{
			get
			{
				return _Xp["General", "DelProcReason"];
			}
			set
			{
				_Xp["General", "DelProcReason"] = value;
			}
		}
		#endregion
		#region FormatCategory
		//[TypeConverter(typeof(EnumDescConverter))]
		//public enum FormatColumns : int
		//{
		//    Default = 0,
		//    [Description("Single Column")]
		//    OneColumn,
		//    [Description("Dual Column")]
		//    TwoColumn,
		//    [Description("Triple Column")]
		//    ThreeColumn,
		//    [Description("Quad Column")]
		//    FourColumns
		//}
		[Category("General")]
		[DisplayName("Default Column Mode")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Column Layout")]
		public FormatColumns Format_Columns
		{
			get
			{
				string s = _Xp["Format", "Columns"];
				//If there is no value to get, then get the parent value (a.k.a. default value).
				if (s == string.Empty)
					s = _Xp.ParentValue("Format", "Columns"); // get the parent value
				// If there is no parent value, then use the volian default
				// Bug fix B2015-194
				//  -Get columns from the proceudre format if available
				//   else get from the parent's format if available
				//   else default to two column
				if (s == string.Empty || s == "t")		// fnp aop1 had this (not sure about other fnp data)
				{
					if (_ProcedureInfo != null && _ProcedureInfo.ActiveFormat.PlantFormat.FormatData.SectData.StepSectionData.StepSectionLayoutData.ColumnMode != null) //This is the procedure format
						return (FormatColumns)_ProcedureInfo.ActiveFormat.PlantFormat.FormatData.SectData.StepSectionData.StepSectionLayoutData.ColumnMode;
					else if (DefaultFormat != null && DefaultFormat.PlantFormat.FormatData.SectData.StepSectionData.StepSectionLayoutData.ColumnMode != null) //This is the parent format
						return (FormatColumns)DefaultFormat.PlantFormat.FormatData.SectData.StepSectionData.StepSectionLayoutData.ColumnMode; 
					else
						return FormatColumns.TwoColumn; //default to two column
				}
				return (FormatColumns)int.Parse(s);
			}
			set
			{
				// if value being saved is same as the parent value, then clear the value (save blank).  This will
				// reset the data to use the parent value.
				string parval = _Xp.ParentValue("Format", "Columns"); // get the parent value
				if (parval.Equals(string.Empty)) // if the parent value is empty, then use the volian default
					parval = ((int)(FormatColumns.Default)).ToString();
				if (parval.Equals(((int)value).ToString()))
					_Xp["Format", "Columns"] = string.Empty; // reset to parent value
				else
					_Xp["Format", "Columns"] = ((int)value).ToString(); // save selected value
				OnPropertyChanged("Format_Columns");
			}
		}
		[Category("Format")]
		//PROPGRID: Hide Plant Format Name (using Format and Default Format)
		[Browsable(false)]
		[DisplayName("Plant Format Name")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Default Plant Format")]
		public string Format_Plant
		{
			get
			{
				return _Xp["format", "plant"];
			}
			set
			{
				_Xp["format", "plant"] = value;
				OnPropertyChanged("Format_Plant");
			}
		}
		#endregion
		#region PrintSettingsCategory       // originally from first entry in fix file and curset.dat
		[Category("Print Settings")]		//Note that this print setting is stored under 'Procedure' element
		[Browsable(false)]
		[DisplayName("Revision Number")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Revision Number")]
		public string Print_Rev
		{
			get
			{
				string s =  _Xp["Procedure", "Rev"];
				if (SelectedSlave > 0)
					s = _Xp["Slave[@index='" + SelectedSlave.ToString() + "']", "Rev"];
				return s;
			}
			set
			{
				if (SelectedSlave > 0)
					_Xp["Slave[@index='" + SelectedSlave.ToString() + "']", "Rev"] = value; // save selected value
				else
					_Xp["Procedure", "Rev"] = value;
				OnPropertyChanged("Print_Rev");
			}
		}
		//new rev_date
		[Category("Print Settings")]		//Note that this print setting is stored under 'Procedure' element
		[Browsable(false)]
		[DisplayName("Revision RevDate")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Revision RevDate")]
		public string Print_RevDate
		{
			get
			{
				string s = _Xp["Procedure", "RevDate"];
				if (SelectedSlave > 0)
					s = _Xp["Slave[@index='" + SelectedSlave.ToString() + "']", "RevDate"];
				return s;
			}
			set
			{
				if (SelectedSlave > 0)
					_Xp["Slave[@index='" + SelectedSlave.ToString() + "']", "RevDate"] = value; // save selected value
				else
					_Xp["Procedure", "RevDate"] = value;
				OnPropertyChanged("Print_RevDate");
			}
		}
		[Category("Print Settings")]
		[Browsable(false)]
		[DisplayName("User Selected ChangeBarDate View")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("The view change bars starting date stored in ChangeBarDate")]
		// B2023-015 this allows us to better determine if a change bar should be view/printed for child (slave) procedures
		// when the user selects view change bars starting date, it's stored in the procedure config under "ChangeBarDate"
		// (see the Set for Print_ChangeBarDate)
		public DateTime? Print_ViewableStartingChangeBarDate
		{
			get
			{
				DateTime? dt = null;
				string s = _Xp["Procedure", "ChangeBarDate"];
				if (s!= null && s!="")
					dt = DateTime.Parse(s);
				return dt;
			}
		}
			//new changebar_date
		[Category("Print Settings")]		//Note that this print setting is stored under 'Procedure' element
		[Browsable(false)]
		[DisplayName("Revision ChangeBarDate")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Revision ChangeBarDate")]
		public string Print_ChangeBarDate
		{
			get
			{
				string s = _Xp["Procedure", "ChangeBarDate"];
				if (SelectedSlave > 0)
					s = _Xp["Slave[@index='" + SelectedSlave.ToString() + "']", "ChangeBarDate"];
				else
				{
					if (s != "")
						return s;
					System.Xml.XmlNodeList nl = _Xp.XmlContents.SelectNodes("//Slave");
					if (nl.Count == 0)
						return s;
					DateTime dt = DateTime.MaxValue;
					foreach (System.Xml.XmlNode nd in nl)
					{
						System.Xml.XmlAttribute xa = nd.Attributes["ChangeBarDate"];
						if (xa == null)
							return s;
						DateTime dtt = DateTime.Parse(xa.InnerText);
						if (dtt < dt)
							dt = dtt;
					}
					if (dt == DateTime.MaxValue)
						return s;
					return dt.ToString("MM/dd/yyyy HH:mm:ss");
				}
				return s;
			}
			set
			{
				if (SelectedSlave > 0)
					_Xp["Slave[@index='" + SelectedSlave.ToString() + "']", "ChangeBarDate"] = value; // save selected value
				else
					_Xp["Procedure", "ChangeBarDate"] = value;
				OnPropertyChanged("Print_ChangeBarDate");
			}
		}
		[Category("Print Settings")]		//Note that this print setting is stored under 'Procedure' element
		[Browsable(false)]
		[DisplayName("Review Date")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Review Date")]
		public string Print_ReviewDate
		{
			get
			{
				string s = _Xp["Procedure", "ReviewDate"];
				if (SelectedSlave > 0)
					s = _Xp["Slave[@index='" + SelectedSlave.ToString() + "']", "ReviewDate"];
				return s;
			}
			set
			{
				if (SelectedSlave > 0)
					_Xp["Slave[@index='" + SelectedSlave.ToString() + "']", "ReviewDate"] = value; // save selected value
				else
					_Xp["Procedure", "ReviewDate"] = value;
				OnPropertyChanged("Print_ReviewDate");
			}
		}
		[Category("Print Settings")]
		//PROPGRID: Hide Number of Copies
		[Browsable(false)]
		[DisplayName("Number of Copies")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Number of Copies")]
		public int Print_NumCopies
		{
			get
			{
				string s = _Xp["PrintSettings", "numcopies"];
				if (s == string.Empty) return 1;
				return int.Parse(_Xp["PrintSettings", "numcopies"]);
			}
			set
			{
				_Xp["PrintSettings", "numcopies"] = value.ToString();
				OnPropertyChanged("Print_NumCopies");
			}
		}
		//public enum PrintPagination : int
		//{
		//    Free = 0, Fixed,
		//    [Description("Automatic")]
		//    Auto
		//}
		[Category("Print Settings")]
		[DisplayName("Pagination")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Pagination")]
		public PrintPagination Print_Pagination
		{
			get
			{
				string s = _Xp["PrintSettings", "Pagination"];
				//If there is no value to get, then get the parent value (a.k.a. default value).
				if (s == string.Empty)
					s = _Xp.ParentValue("PrintSettings", "Pagination"); // get the parent value
				// If there is no parent value, then use the volian default
				if (s == string.Empty)
					return PrintPagination.Auto;// default to volian default
				return (PrintPagination)int.Parse(s);
			}
			set
			{
				// if value being saved is same as the parent value, then clear the value (save blank).  This will
				// reset the data to use the parent value.
				string parval = _Xp.ParentValue("PrintSettings", "Pagination"); // get the parent value
				if (parval.Equals(string.Empty)) // if the parent value is empty, then use the volian default
					parval = ((int)(PrintPagination.Auto)).ToString();
				if (parval.Equals(((int)value).ToString()))
					_Xp["PrintSettings", "Pagination"] = string.Empty; // reset to parent value
				else
					_Xp["PrintSettings", "Pagination"] = ((int)value).ToString(); // save selected value
				OnPropertyChanged("Print_Pagination");
			}
		}
		//[TypeConverter(typeof(EnumDescConverter))]
		//public enum PrintWatermark : int
		//{
		//    None = 0, Reference, Draft, Master, Sample,
		//    [Description("Information Only")]
		//    InformationOnly
		//}
		[Category("Print Settings")]
		[DisplayName("Watermark")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Watermark")]
		public PrintWatermark Print_Watermark
		{
			get
			{
				string s = _Xp["PrintSettings", "Watermark"];
				//If there is no value to get, then get the parent value (a.k.a. default value).
				if (s == string.Empty)
					s = _Xp.ParentValue("PrintSettings", "Watermark"); // get the parent value
				// If there is no parent value, then use the volian default
				if (s == string.Empty)
					return PrintWatermark.Draft;// default to volian default
				return (PrintWatermark)int.Parse(s);
			}
			set
			{
				// if value being saved is same as the parent value, then clear the value (save blank).  This will
				// reset the data to use the parent value.
				string parval = _Xp.ParentValue("PrintSettings", "Watermark"); // get the parent value
				if (parval.Equals(string.Empty)) // if the parent value is empty, then use the volian default
					parval = ((int)(PrintWatermark.Draft)).ToString();
				if (parval.Equals(((int)value).ToString()))
					_Xp["PrintSettings", "Watermark"] = string.Empty; // reset to parent value
				else
					_Xp["PrintSettings", "Watermark"] = ((int)value).ToString(); // save selected value
				OnPropertyChanged("Print_Watermark");
			}
		}
		[Category("Print Settings")]
		[DisplayName("Print All Not In Merge Pdfs")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Print All Do Not Include In Merge Pdfs")]
		public bool Print_NotInMergeAll
		{
			get
			{
				string s = _Xp["PrintSettings", "NotInMergeAll"];
				// If there is no parent value, then use the volian default, false.  Note that there is no
				// parent value for this, it is only defined on the procedure.
				if (s == string.Empty) s = "false";
				return bool.Parse(s);
			}
			set
			{
				string s = _Xp["PrintSettings", "NotInMergeAll"];				// get the original value to see if a change
				bool original = (s==string.Empty) ? false : bool.Parse(s);
				if (original == value) return;
				_Xp["PrintSettings", "NotInMergeAll"] = ((bool)value).ToString();								// save selected value
				OnPropertyChanged("Print_NotInMergeAll");
			}
		}
		// Change Bar Use from 16-bit code:
		//  	No Default
		//      Without Change Bars
		//      With Default Change Bars
		//      With User Specified Change Bars
		//[TypeConverter(typeof(EnumDescConverter))]
		//public enum PrintChangeBar : int
		//{
		//    [Description("Select Before Printing")]
		//    SelectBeforePrinting = 0,
		//    [Description("Without Change Bars")]
		//    Without,
		//    [Description("With Default Change Bars")]
		//    WithDefault,
		//    [Description("Use Custom Change Bars")]
		//    WithUserSpecified
		//}
		[Category("Format Settings")]
		[DisplayName("Change Bar")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Change Bar Use")]
		public PrintChangeBar Print_ChangeBar
		{
			get
			{
				string s = _Xp["PrintSettings", "ChangeBar"];
				//If there is no value to get, then get the parent value (a.k.a. default value).
				if (s == string.Empty)
					s = _Xp.ParentValue("PrintSettings", "ChangeBar"); // get the parent value
				// If there is no parent value, then use the volian default
				if (s == string.Empty)
					return PrintChangeBar.SelectBeforePrinting;// default to volian default
				return (PrintChangeBar)int.Parse(s);
			}
			set
			{
				// if value being saved is same as the parent value, then clear the value (save blank).  This will
				// reset the data to use the parent value.
				string parval = _Xp.ParentValue("PrintSettings", "ChangeBar"); // get the parent value
				if (parval.Equals(string.Empty)) // if the parent value is empty, then use the volian default
					parval = ((int)(PrintChangeBar.SelectBeforePrinting)).ToString();
				if (parval.Equals(((int)value).ToString()))
					_Xp["PrintSettings", "ChangeBar"] = string.Empty; // reset to parent value
				else
					_Xp["PrintSettings", "ChangeBar"] = ((int)value).ToString(); // save selected value
				OnPropertyChanged("Print_ChangeBar");
			}
		}
		// User Specified Change Bar Location from16-bit code:
		//      With Text
		//      Outside Box
		//      AER on LEFT, RNO on Right
		//      To the Left of Text
		//[TypeConverter(typeof(EnumDescConverter))]
		//public enum PrintChangeBarLoc : int
		//{
		//    [Description("With Text")]
		//    WithText = 0,
		//    [Description("Outside Box")]
		//    OutsideBox,
		//    [Description("AER on Left RNO on Right")]
		//    AERleftRNOright,
		//    [Description("To the Left of the Text")]
		//    LeftOfText
		//}
		[Category("Format Settings")]
		[DisplayName("Change Bar Position")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("User Specified Change Bar Location")]
		public PrintChangeBarLoc Print_ChangeBarLoc
		{
			get
			{
				string s = _Xp["PrintSettings", "ChangeBarLoc"];
				//If there is no value to get, then get the parent value (a.k.a. default value).
				if (s == string.Empty)
					s = _Xp.ParentValue("PrintSettings", "ChangeBarLoc"); // get the parent value
				// If there is no parent value, then use the volian default
				if (s == string.Empty)
					return PrintChangeBarLoc.WithText;// default to volian default
				return (PrintChangeBarLoc)int.Parse(s);
			}
			set
			{
				// if value being saved is same as the parent value, then clear the value (save blank).  This will
				// reset the data to use the parent value.
				string parval = _Xp.ParentValue("PrintSettings", "ChangeBarLoc"); // get the parent value
				if (parval.Equals(string.Empty)) // if the parent value is empty, then use the volian default
					parval = ((int)(PrintChangeBarLoc.WithText)).ToString();
				if (parval.Equals(((int)value).ToString()))
					_Xp["PrintSettings", "ChangeBarLoc"] = string.Empty; // reset to parent value
				else
					_Xp["PrintSettings", "ChangeBarLoc"] = ((int)value).ToString(); // save selected value
				OnPropertyChanged("Print_ChangeBarLoc");
			}
		}
		// Change Bar Text from16-bit code:
		//      Date and Change ID
		//      Revision Number
		//      Change ID
		//      No Change Bar Message
		//      User Defined Message
		//[TypeConverter(typeof(EnumDescConverter))]
		//public enum PrintChangeBarText : int
		//{
		//    [Description("Date and Change ID")]
		//    DateChgID = 0,
		//    [Description("Revision Number")]
		//    RevNum,
		//    [Description("Change ID")]
		//    ChgID,
		//    [Description("No Change Bar Text")]
		//    None,
		//    [Description("Custom Change Bar Text")]
		//    UserDef
		//}
		[Category("Format Settings")]
		[DisplayName("Change Bar Text Type")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Change Bar Text")]
		public PrintChangeBarText Print_ChangeBarText
		{
			get
			{
				string s = _Xp["PrintSettings", "ChangeBarText"];
				//If there is no value to get, then get the parent value (a.k.a. default value).
				if (s == string.Empty)
					s = _Xp.ParentValue("PrintSettings", "ChangeBarText"); // get the parent value
				// If there is no parent value, then use the volian default
				if (s == string.Empty)
					return PrintChangeBarText.DateChgID;// default to volian default
				return (PrintChangeBarText)int.Parse(s);
			}
			set
			{
				// if value being saved is same as the parent value, then clear the value (save blank).  This will
				// reset the data to use the parent value.
				string parval = _Xp.ParentValue("PrintSettings", "ChangeBarText"); // get the parent value
				if (parval.Equals(string.Empty)) // if the parent value is empty, then use the volian default
					parval = ((int)(PrintChangeBarText.DateChgID)).ToString();
				if (parval.Equals(((int)value).ToString()))
					_Xp["PrintSettings", "ChangeBarText"] = string.Empty; // reset to parent value
				else
					_Xp["PrintSettings", "ChangeBarText"] = ((int)value).ToString(); // save selected value
				OnPropertyChanged("Print_ChangeBarText");
			}
		}
		[Category("Print Settings")]
		//PROPGRID: Hide User Format
		[Browsable(false)]
		[DisplayName("User Format")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("User Format")]
		public string Print_UserFormat
		{
			get
			{
				return _Xp["PrintSettings", "userformat"];
			}
			set
			{
				_Xp["PrintSettings", "userformat"] = value;
				OnPropertyChanged("Print_UserFormat");
			}
		}
		[Category("Format Settings")]
		[DisplayName("Custom Change Bar Message Line One")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("User Change Bar Message1")]
		public string Print_UserCBMess1
		{
			get
			{
				string s = _Xp["PrintSettings", "usercbmess1"];// get the saved value
				//If there is no value to get, then get the parent value (a.k.a. default value).
				if (s == string.Empty)
					s = _Xp.ParentValue("PrintSettings", "usercbmess1"); // get the parent value
				// If there is no parent value, then use the volian default
				if (s == string.Empty)
					s = "";// default to volian default
				return s;
			}
			set
			{
				// if value being saved is same as the parent value, then clear the value (save blank).  This will
				// reset the data to use the parent value.
				string parval = _Xp.ParentValue("PrintSettings", "usercbmess1"); // get the parent value
				if (parval.Equals(string.Empty)) // if the parent value is empty, then use the volian default
					parval = "";
				if (parval.Equals(value))
					_Xp["PrintSettings", "usercbmess1"] = string.Empty; // reset to parent value
				else
					_Xp["PrintSettings", "usercbmess1"] = value; // save selected value
				OnPropertyChanged("Print_UserCBMess1");
			}
		}
		[Category("Format Settings")]
		[DisplayName("Custom Change Bar Message Line Two")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("User Change Bar Message2")]
		public string Print_UserCBMess2
		{
			get
			{
				string s = _Xp["PrintSettings", "usercbmess2"];// get the saved value
				//If there is no value to get, then get the parent value (a.k.a. default value).
				if (s == string.Empty)
					s = _Xp.ParentValue("PrintSettings", "usercbmess2"); // get the parent value
				// If there is no parent value, then use the volian default
				if (s == string.Empty)
					s = "";// default to volian default
				return s;
			}
			set
			{
				// if value being saved is same as the parent value, then clear the value (save blank).  This will
				// reset the data to use the parent value.
				string parval = _Xp.ParentValue("PrintSettings", "usercbmess2"); // get the parent value
				if (parval.Equals(string.Empty)) // if the parent value is empty, then use the volian default
					parval = "";
				if (parval.Equals(value))
					_Xp["PrintSettings", "usercbmess2"] = string.Empty; // reset to parent value
				else
					_Xp["PrintSettings", "usercbmess2"] = value; // save selected value
				OnPropertyChanged("Print_UserCBMess2");
			}
		}
		// the following is for internal use only:
		[Browsable(false)]
		public string SectionStart
		{
			get
			{
				return _Xp["Procedure", "SectionStart"];
			}
			set
			{
				_Xp["Procedure", "SectionStart"] = value;
				OnPropertyChanged("Proceduret_SectionStart");
			}
		}
		#endregion
		#region EnhancedDocuments
		// Enhanced Documents
		private EnhancedDocuments _MyEnhancedDocuments = null;
		public EnhancedDocuments MyEnhancedDocuments
		{
			get
			{
				if (_MyEnhancedDocuments == null)
				{
					_MyEnhancedDocuments = EnhancedDocuments.Load(_Xp);
				}
				return _MyEnhancedDocuments;
			}
			set { _MyEnhancedDocuments = value; }
		}
		public void AddEnhancedDocument(int type, int itemid)
		{
			MyEnhancedDocuments.Add(type, itemid);
			SaveEnhancedDocuments();
		}
		public void SaveEnhancedDocuments()
		{
			// get all of the current enhanced links from datastructure in code.  This list may have been
			// modified by adding items during code execution by associating source <--> background etc.
			EnhancedDocuments edsToAdd = new EnhancedDocuments();
			foreach (EnhancedDocument ed in MyEnhancedDocuments)
				edsToAdd.Add(ed);
			// from the existing list in xml, remove any that are in the 'editted (edsToAdd) list
			// so that what remains are those that need added to xml that will then be written to database
			foreach (XmlNode xn in _Xp.XmlContents.SelectNodes("//Enhanced"))
			{
				//EnhancedDocument tmp = edsToAdd[int.Parse(xn.Attributes["Type"].Value)];
				EnhancedDocument tmp = edsToAdd.GetByType(int.Parse(xn.Attributes["Type"].Value)); // [int.Parse(xn.Attributes["Type"].Value)];
				if (tmp != null)
				{
					if (int.Parse(xn.Attributes["ItemID"].Value) != tmp.ItemID)
						xn.Attributes["ItemID"].Value = tmp.ItemID.ToString();
					edsToAdd.Remove(tmp);
				}
			}
			foreach (EnhancedDocument edadd in edsToAdd)
			{
				// Add (example): 
				// First add 'Enhanced' element:
				XmlNode newEnhNode = _Xp.XmlContents.CreateNode(XmlNodeType.Element, "Enhanced", _Xp.XmlContents.NamespaceURI);
				XmlNode xnEnh = _Xp.XmlContents.DocumentElement.AppendChild(newEnhNode);
				// Now add the 'Type' and 'ItemID' attributes:
				XmlAttribute xa = xnEnh.Attributes.Append(_Xp.XmlContents.CreateAttribute("Type"));
				xa.Value = edadd.Type.ToString();
				xa = xnEnh.Attributes.Append(_Xp.XmlContents.CreateAttribute("ItemID"));
				xa.Value = edadd.ItemID.ToString();
			}
		}
		#endregion
		#region IItemConfig Members
		[Category("Master/Slave Settings")]
		[DisplayName("Applicability")]
		[RefreshProperties(RefreshProperties.All)]
		[Description("Scope Applicability")]
		public Volian.Base.Library.BigNum MasterSlave_Applicability
		{
			get
			{
				string s = _Xp["MasterSlave", "Applicability"];
				return new Volian.Base.Library.BigNum(s);
			}
			set
			{
				if (value != null)
					_Xp["MasterSlave", "Applicability"] = value.FlagList;
				else
					_Xp["MasterSlave", "Applicability"] = null;
				OnPropertyChanged("MasterSlave_Applicability");
			}
		}
		#endregion
	}
	public interface IItemConfig
	{
		Volian.Base.Library.BigNum MasterSlave_Applicability { get; set;}
	}
}