using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.ComponentModel;
using DescriptiveEnum;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Windows.Forms;
using Volian.Base.Library;
using System.Drawing;
namespace VEPROMS.CSLA.Library
{
	#region FormatConfig
	// FormatConfig is the data structure that holds the UCF (User Control Of Format) data.  It is stored in the database as xml in the 
	// Formats table's Config field.  This uses Xml Serialization to translate the data from these data structures back/forth from the
	// xml stored in the database.  Note that fields that the user can change have an associate 'Orig' field which contains the original
	// data from the format this is derived from.  This is used for the 'Reset' to the original value.
	[Serializable]
	[TypeConverter(typeof(ExpandableObjectConverter))]
	public class FormatConfig : ConfigDynamicTypeDescriptor, INotifyPropertyChanged
	{
		#region DynamicTypeDescriptor
		internal override bool IsReadOnly
		{
			get { return false; }
		}
		#endregion
		#region XML
		private XMLProperties _Xp;
		private XMLProperties Xp
		{
			get { return _Xp; }
		}
		#endregion
		#region Attributes
		[XmlAttribute("Name")]
		private string _Name;
		[Category(" Identification")]
		[DisplayName(" Name")]
		[Description("Names user control of format")]
		[XmlIgnore]			// ignore it when doing serialization
		public string Name
		{
			get { return _Name; }
			set { _Name = value; }
		}
		[XmlAttribute("Description")]
		private string _Description;
		[Category(" Identification")]
		[Description("Describes user control of format")]
		[XmlIgnore]			// ignore it when doing serialization
		public string Description
		{
			get { return _Description; }
			set { _Description = value; }
		}
		#endregion Attributes
		#region Elements
		private PlantFormatx _PlantFormat;
		[DisplayName("Plant Format")]
		[Description("Format values and settings that be modified for the selected format")]
		public PlantFormatx PlantFormat
		{
			get { return _PlantFormat; }
			set { _PlantFormat = value; }
		}
		#endregion Elements
		#region Constructors
		private Format _Format;
		private FormatInfo _FormatInfo;
		public FormatConfig(string xml)
		{
			if (xml == string.Empty) xml = "";
		}
		public FormatConfig(FormatInfo fi)
		{
			_FormatInfo = fi;
		}
		public FormatConfig(Format f)
		{
			_Format = f;
		}
		public FormatConfig()
		{
			PlantFormat = new PlantFormatx();
		}
		#endregion Constructors
		#region Serialize
		public string ConvertToString()
		{
			return GenericSerializer.StringSerialize(this);
		}
		public static FormatConfig Get(string xml)
		{
			return GenericSerializer.StringDeserialize(xml);
		}
		public override string ToString()
		{
			return "PlantFormat";
		}
		#endregion
	#endregion FormatConfig
		#region PlantFormat
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		[Category("Data")]
		public class PlantFormatx
		{
			[XmlElement]
			private FormatData _FormatData = new FormatData();
			[DisplayName("Step Settings")]
			[Description("Values used when formatting of steps (edit and print)")]
			public FormatData FormatData
			{
				get { return _FormatData; }
				set { _FormatData = value; }
			}
			[XmlElement]
			private DocStyles _DocStyles;
			[DisplayName("Section Type Settings")]
			[Description("Values used when printing sections")]
			public DocStyles DocStyles
			{
				get { return _DocStyles; }
				set { _DocStyles = value; }
			}
			public PlantFormatx()
			{
			}
			public override string ToString()
			{
				return "Plant Format";
			}
		}
		#endregion PlantFormat
		#region FormatData
		// FormatData maps to the PlantFormat/Format data and contains properties & data objects that are implemented for the user to 
		// control of the format.  NOTE that any additional format items must be added here or in docstyles.
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		public class FormatData
		{
			[XmlElement]
			private Flags _Flags = new Flags();
			[Description("Various flags that affect editing and printing")]
			public Flags Flags
			{
				get { return _Flags; }
				set { _Flags = value; }
			}
			[XmlElement]
			private CheckOffHeaderList _CheckOffHeaderList;
			[DisplayName("CheckOff Headers List")]
			[Description("CheckOff Headers Usage")]
			public CheckOffHeaderList CheckOffHeaderList
			{
				get { return _CheckOffHeaderList; }
				set { _CheckOffHeaderList = value; }
			}
			[XmlElement]
			private CheckOffList _CheckOffList;
			[DisplayName("CheckOff List")]
			[Description("CheckOff Usage")]
			public CheckOffList CheckOffList
			{
				get { return _CheckOffList; }
				set { _CheckOffList = value; }
			}
			[XmlElement]
			private float? _CheckOffXOffAdj;
			//[XmlAttribute("CheckOffXOff")]
			[DisplayName("CheckOff XOffset Adjust")]
			[Description("Check Off X Offset Adjustment (in)")]
			public float? CheckOffXOffAdj
			{
				get { return _CheckOffXOffAdj; }
				set { _CheckOffXOffAdj = value; }
			}
			[XmlElement]
			private ReplaceStrData _ReplaceStrData;
			[DisplayName("Replace Words List")]
			[Description("Replace Words with Other Text and/or attributes")]
			public ReplaceStrData ReplaceStrData
			{
				get { return _ReplaceStrData; }
				set { _ReplaceStrData = value; }
			}
			[XmlElement]
			private StepData _StepData;
			[DisplayName("Step List")]
			[Description("List of step types for which formatting can be set")]
			public StepData StepData
			{
				get { return _StepData; }
				set { _StepData = value; }
			}
			public FormatData()
			{
				ReplaceStrData = new ReplaceStrData();
			}
			public override string ToString()
			{
				return "Format Data";
			}
		}
		#endregion FormatData
		#region Flags
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		public class Flags
		{
			private bool? _CheckOffUCF;
			[DisplayName("UCF CheckOffs")]
			[Description("Additional UCF Check Offs and Sign Offs")]
			public bool? CheckOffUCF
			{
				get { return _CheckOffUCF; }
				set { _CheckOffUCF = value; }
			}
			private bool? _PartialStepCompression;
			[DisplayName("Partial Step Compression")]
			[Description("Automatically compress last sub-steps to fit on page")]
			public bool? PartialStepCompression
			{
				get { return _PartialStepCompression; }
				set { _PartialStepCompression = value; }
			}
			private bool? _CompressSteps;
			[DisplayName("Compress Steps")]
			[Description("Automatically compress entire step to fit on page")]
			public bool? CompressSteps
			{
				get { return _CompressSteps; }
				set { _CompressSteps = value; }
			}
			public override string ToString()
			{
				return " ";
			}
		}
		#endregion Flags
		#region ReplaceStrData (list)
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		[Editor(typeof(PropGridCollEditor), typeof(System.Drawing.Design.UITypeEditor))]
		public class ReplaceStrData : List
		{
			[Browsable(false)]
			public int Capacity { get { return base.Capacity; } }
			[Browsable(false)]
			public int Count { get { return base.Count; } }
			public ReplaceStr this[int index]
			{
				get { return (ReplaceStr)base[index]; }
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return "Replace Words List Count = " + base.Count.ToString();
			}
			public static ReplaceStrData Get(string xml)
			{
				return GenericSerializer.StringDeserialize(xml);
			}
		}
		#endregion ReplaceStrData
		#region ReplaceStr
		[Flags]
		public enum E_ReplaceFlagsUCF : uint
		{
			High = 0x0001,		    // Do ReplaceWords in HIGH LEVEL STEPS
			RNO = 0x0002,		    // Do ReplaceWords in RNOS
			Caution = 0x0004,		    // Do ReplaceWords in CAUTIONS
			Note = 0x0008,		    // Do ReplaceWords in NOTES
			Table = 0x0010,		    // Do ReplaceWords in TABLES
			Substep = 0x0020,		    // Do ReplaceWords in SUBSTEPS
			Attach = 0x0040,		    // Do ReplaceWords in ATTACHMENTS
			Bkgd = 0x0080,		// Do ReplaceWords in BACKGROUNDS
			DiffUnit = 0x0100,		// Do ReplaceWords ONLY for different UNIT # 
			TOC = 0x0200,		    // Do in auto table-of-contents 
			StatTree = 0x0400,
			HLSSetpnt = 0x0800,		// Do ReplaceWords in HighLevelStep SETPoiNTs
			Trans = 0x1000,		    // Do ReplaceWords in TRANSITIONS
			Setpoint = 0x2000,		// Do ReplaceWords in SETPOINTS
			// Case Sensitivity Flags - default is off (Case Sensitive Replace)
			CaseInsens = 0x0000C000,	// Do ReplaceWords for all words thatmatch, regardless of case,
			//   and replace with the ReplaceWith string as is
			// B2019-022: do not support CaseInsensFirst & CaseInsensAll - there is no supporting replace word code:
			//CaseInsensFirst = 0x4000,	// Do ReplaceWords for all words thatexactly match the ReplaceWord, 
			//   except the case of the first character may be different
			//CaseInsensAll = 0x8000,	// Do ReplaceWords for all words that match the ReplaceWord, regardless of case
			Partials = 0x10000,   // Do replace even on partial matches
			Plackeep = 0x20000,   // Do replace in PlaceKeepers
			InSecTitle = 0x40000,
			BeforeTrans = 0x80000,   // Only do replace if the string occurs immediately before a transition.
			BeforeList = 0x100000,   // C2021-045 Only if the text ends with a colon ":"
			PageList = 0x200000,     // F2021-053 Do replace words for PageList items that are ROs
			FirstWord = 0x400000,   // C2021-056 Do only if is the first word in the text
			NotInRO = 0x800000      // B2022-015 BNPPalr: Determine whether RO text should have Replace Words applied
		}
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		public class ReplaceStr
		{
			private int _State = 0;			// 0 = no change, -1 deleted, 1 added, 2 modified
			[XmlAttribute("State")]
			[Browsable(false)]		        // Don't show in the property grid
			public int State
			{
				get { return _State; }
				set { _State = value; }
			}
			private E_ReplaceFlagsUCF _Flag;
			[Editor(typeof(FlagEnumUIEditor), typeof(System.Drawing.Design.UITypeEditor))]
			[XmlAttribute("Flag")]
			[DisplayName("Flags (Use In)")]         // Note that [Description] is not used in collection items (that use collection editor)
			public E_ReplaceFlagsUCF Flag
			{
				get { return (E_ReplaceFlagsUCF)_Flag; }
				set { _Flag = value; }
			}
			private string _ReplaceWord;
			[XmlAttribute("ReplaceWord")]
			[DisplayName("Replace Word")]
			public string ReplaceWord
			{
				get { return _ReplaceWord; }
				set
				{
					if (this._ReplaceWith == null) _ReplaceWith = value;
					_ReplaceWord = value;
				}
			}
			private string _ReplaceWith;
			[Editor(typeof(RtfEditor), typeof(System.Drawing.Design.UITypeEditor))]
			[XmlAttribute("ReplaceWith")]
			[DisplayName("With")]
			public string ReplaceWith
			{
				get { return _ReplaceWith; }
				set { _ReplaceWith = value; }
			}
			public ReplaceStr()
			{
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return ReplaceWord;
			}
			public static ReplaceStr Get(string xml)
			{
				return GenericSerializer.StringDeserialize(xml);
			}
		}
		#endregion ReplaceStr
		#region CheckOffHeaders (list)
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		[Editor(typeof(PropGridCollEditor), typeof(System.Drawing.Design.UITypeEditor))]
		public class CheckOffHeaderList : List
		{
			[Browsable(false)]
			public int Capacity { get { return base.Capacity; } }
			[Browsable(false)]
			public int Count { get { return base.Count; } }
			public CheckOffHeader this[int index]
			{
				get { return (CheckOffHeader)base[index]; }
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return "CheckOffHeader Count = " + base.Count.ToString();
			}
		}
		#endregion CheckOffHeaders
		#region CheckOffHeader
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		public class CheckOffHeader
		{
			private string _CheckOffHeading;
			[XmlAttribute("CheckOffHeading")]
			[Browsable(false)]
			public string CheckOffHeading
			{
				get { return _CheckOffHeading; }
				set { _CheckOffHeading = value; }
			}
			private string _Index;
			[XmlAttribute("Index")]
			[Browsable(false)]				// Don't show in the property grid
			public string Index
			{
				get { return _Index; }
				set { _Index = value; }
			}
			private bool? _Active;
			[DisplayName("Active CheckOff Header")]
			[Description("Allow use of this CheckOff Header")]
			public bool? Active
			{
				get { return _Active; }
				set { _Active = value; }
			}
			private bool? _OrigActive;
			[XmlIgnore]
			[Browsable(false)]
			public bool? OrigActive
			{
				get { return _OrigActive; }
				set { _OrigActive = value; }
			}
			public CheckOffHeader()
			{
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return CheckOffHeading;
			}
		}
		#endregion CheckOffHeader
		#region CheckOffs (list)
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		[Editor(typeof(PropGridCollEditor), typeof(System.Drawing.Design.UITypeEditor))]
		public class CheckOffList : List
		{
			[Browsable(false)]
			public int Capacity { get { return base.Capacity; } }
			[Browsable(false)]
			public int Count { get { return base.Count; } }
			public CheckOff this[int index]
			{
				get { return (CheckOff)base[index]; }
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return "CheckOff Count = " + base.Count.ToString();
			}
		}
		#endregion CheckOffs
		#region CheckOff
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		public class CheckOff
		{
			private string _MenuItem;
			[XmlAttribute("MenuItem")]
			[Browsable(false)]
			public string MenuItem
			{
				get { return _MenuItem; }
				set { _MenuItem = value; }
			}
			private string _Index;
			[XmlAttribute("Index")]
			[Browsable(false)]				// Don't show in the property grid
			public string Index
			{
				get { return _Index; }
				set { _Index = value; }
			}
			private bool? _Active;
			[DisplayName("Active CheckOff")]
			[Description("Allow use of this CheckOff")]
			public bool? Active
			{
				get { return _Active; }
				set { _Active = value; }
			}
			private bool? _OrigActive;
			[XmlIgnore]
			[Browsable(false)]
			public bool? OrigActive
			{
				get { return _OrigActive; }
				set { _OrigActive = value; }
			}
			public CheckOff()
			{
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return MenuItem;
			}
		}
		#endregion CheckOff
		#region StepData (list)
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		[Editor(typeof(PropGridCollEditor), typeof(System.Drawing.Design.UITypeEditor))]
		public class StepData : List
		{
			[Browsable(false)]
			public int Capacity { get { return base.Capacity; } }
			[Browsable(false)]
			public int Count { get { return base.Count; } }
			public Step this[int index]
			{
				get { return (Step)base[index]; }
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return "Step Count = " + base.Count.ToString();
			}
			public static StepData Get(string xml)
			{
				return GenericSerializer.StringDeserialize(xml);
			}
		}
		#endregion StepData
		#region Step
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		public class Step
		{
			private string _Index;
			[XmlAttribute("Index")]
			[Browsable(false)]				// Don't show in the property grid
			public string Index
			{
				get { return _Index; }
				set { _Index = value; }
			}
			private string _Type;
			[XmlAttribute("Type")]
			[Browsable(false)]		// Don't show in the property grid
			public string Type
			{
				get { return _Type; }
				set { _Type = value; }
			}
			private FontDesc _FontDesc;
			[XmlElement("FontDesc")]
			public FontDesc FontDesc
			{
				get { return _FontDesc; }
				set { _FontDesc = value; }
			}
			public Step()
			{
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return Type;
			}
			public static Step Get(string xml)
			{
				return GenericSerializer.StringDeserialize(xml);
			}
		}
		#endregion Step
		#region FontDesc
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		public class FontDesc
		{
			// The Font object allows the conversion from a windows font back/forth from a string.  This is how the
			// data is stored in the database, i.e. a string representing the font.
			private Font _Font;
			[XmlAttribute("Font")]
			[Browsable(false)]
			public string Font
			{
				get
				{
					System.Drawing.FontConverter cvt = new System.Drawing.FontConverter();
					return cvt.ConvertToString(_WindowsFont);
				}
				set
				{
					System.Drawing.FontConverter cvt = new System.Drawing.FontConverter();
					_WindowsFont = cvt.ConvertFromString(value ?? "Arial, 10pt") as System.Drawing.Font;
				}
			}
			private System.Drawing.Font _OrigWindowsFont;
			[XmlIgnore]
			[Browsable(false)]
			public System.Drawing.Font OrigWindowsFont
			{
				get { return _OrigWindowsFont; }
				set { _OrigWindowsFont = value; }
			}
			private System.Drawing.Font _WindowsFont;
			[XmlIgnore]
			public System.Drawing.Font WindowsFont
			{
				get { return _WindowsFont; }
				set { _WindowsFont = value; }
			}
			public FontDesc()
			{
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return "FontDescription";
			}
			public static FontDesc Get(string xml)
			{
				return GenericSerializer.StringDeserialize(xml);
			}
		}
		#endregion Font
		#region DocStyles
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		[Editor(typeof(PropGridCollEditor), typeof(System.Drawing.Design.UITypeEditor))]
		public class DocStyles : List
		{
			[Browsable(false)]
			public int Capacity { get { return base.Capacity; } }
			[Browsable(false)]
			public int Count { get { return base.Count; } }
			public DocStyle this[int index]
			{
				get { return (DocStyle)base[index]; }
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return "Section Type Count = " + base.Count.ToString();
			}
		}
		#endregion DocStyles
		#region DocStyle
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		public class DocStyle
		{
			private string _Name;
			[XmlAttribute("Name")]
			[Browsable(false)]
			public string Name
			{
				get { return _Name; }
				set { _Name = value; }
			}
			private string _Index;
			[Browsable(false)]
			[XmlAttribute("Index")]
			public string Index
			{
				get { return _Index; }
				set { _Index = value; }
			}
			private Layout _Layout;
			[XmlElement("Layout")]
			public Layout Layout
			{
				get { return _Layout; }
				set { _Layout = value; }
			}
			public DocStyle()
			{
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return Name;
			}
		}
		#endregion DocStyle
		#region Layout
		[Serializable]
		[TypeConverter(typeof(ExpandableObjectConverter))]
		public class Layout : IXmlSerializable
		{
			private float? _PageLength;
			[XmlAttribute("PageLength")]
			[DisplayName("Page Length (in)")]
			public float? PageLength
			{
				get { return _PageLength; }
				set { _PageLength = value; }
			}
			private float? _OrigPageLength;
			[XmlIgnore]
			[Browsable(false)]
			public float? OrigPageLength
			{
				get { return _OrigPageLength; }
				set { _OrigPageLength = value; }
			}
			private float? _LeftMargin;
			[XmlAttribute("LeftMargin")]
			[DisplayName("Left Margin (in)")]
			public float? LeftMargin
			{
				get { return _LeftMargin; }
				set { _LeftMargin = value; }
			}
			private float? _OrigLeftMargin;
			[XmlIgnore]
			[Browsable(false)]
			public float? OrigLeftMargin
			{
				get { return _OrigLeftMargin; }
				set { _OrigLeftMargin = value; }
			}
			public Layout()
			{
			}
			public string ConvertToString()
			{
				return GenericSerializer.StringSerialize(this);
			}
			public override string ToString()
			{
				return "Layout";
			}
			#region IXmlSerializable members
			// The read & write for these is necessary since they are within same xml element, and one may be empty, so
			// no attribute should be listed in the xml.
			public void WriteXml(XmlWriter writer)
			{
				if (LeftMargin != null) writer.WriteAttributeString("LeftMargin", LeftMargin.ToString());
				if (PageLength != null) writer.WriteAttributeString("PageLength", PageLength.ToString());
			}
			public void ReadXml(XmlReader reader)
			{
				if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "Layout")
				{
					string str = reader["PageLength"];
					if (str != null && str != "") PageLength = float.Parse(str);
					str = reader["LeftMargin"];
					if (str != null && str != "") LeftMargin = float.Parse(str);
				}
			}
			public XmlSchema GetSchema()
			{
				return (null);
			}
			#endregion
		}
		#endregion Layout
	}
}