using System;
using System.ComponentModel.Design;
using System.Drawing;
namespace DevComponents.Tree
{
	/// 
	/// Represents class for static tree utilities.
	/// 
	public class Utilites
	{
		/// 
		/// Initializes control with default settings for connectors and nodes.
		/// 
		/// Control to initialize.
		public static void InitializeTree(TreeGX tree)
		{
			InitializeTree(tree,new ComponentFactory());
		}
		
		/// 
		/// Initializes control with default settings for connectors and nodes.
		/// 
		/// Control to initialize.
		/// Factory to use to create new instances of objects.
		internal static void InitializeTree(TreeGX tree, ComponentFactory factory)
		{
			tree.RootConnector=factory.CreateComponent(typeof(NodeConnector)) as NodeConnector;
			tree.RootConnector.LineWidth=5;
			tree.RootConnector.LineColor=SystemColors.Highlight;
			tree.NodesConnector=factory.CreateComponent(typeof(NodeConnector)) as NodeConnector;
			tree.NodesConnector.LineWidth=5;
			tree.NodesConnector.LineColor=SystemColors.Highlight;
			eStyleBorderType border=eStyleBorderType.Solid;
			ElementStyle style=factory.CreateComponent(typeof(ElementStyle)) as ElementStyle;
			
			style.BackColorSchemePart=eColorSchemePart.BarBackground;
			style.BackColor2SchemePart=eColorSchemePart.BarBackground2;
			style.BackColorGradientAngle=90;
			style.CornerDiameter=4;
			style.CornerType=eCornerType.Rounded;
			style.BorderLeft=border;
			style.BorderLeftWidth=1;
			style.BorderTop=border;
			style.BorderTopWidth=1;
			style.BorderBottom=border;
			style.BorderBottomWidth=1;
			style.BorderRight=border;
			style.BorderRightWidth=1;
			style.BorderColorSchemePart=eColorSchemePart.BarDockedBorder;
			style.PaddingBottom=3;
			style.PaddingLeft=3;
			style.PaddingRight=3;
			style.PaddingTop=3;
			style.TextColor=Color.FromArgb(0,0,128);
			style.Font=tree.Font;
			tree.Styles.Add(style);
			tree.NodeStyle=style;
		}
		
		/// 
		/// Creates new style and adds it to styles collection
		/// 
		/// Tree to assign style to
		/// Style factory
		/// 
		/// 
		/// 
		/// 
		internal static ElementStyle CreateStyle(ComponentFactory factory, string description, Color borderColor, Color backColor, Color backColor2, int gradientAngle, Color textColor)
		{
			eStyleBorderType border=eStyleBorderType.Solid;
			ElementStyle style=factory.CreateComponent(typeof(ElementStyle)) as ElementStyle;
			
			style.Description = description;
			style.BackColor = backColor;
			style.BackColor2=backColor2;
			style.BackColorGradientAngle=gradientAngle;
			style.CornerDiameter=4;
			style.CornerType=eCornerType.Rounded;
			style.BorderLeft=border;
			style.BorderLeftWidth=1;
			style.BorderTop=border;
			style.BorderTopWidth=1;
			style.BorderBottom=border;
			style.BorderBottomWidth=1;
			style.BorderRight=border;
			style.BorderRightWidth=1;
			style.BorderColor=borderColor;
			style.PaddingBottom=3;
			style.PaddingLeft=3;
			style.PaddingRight=3;
			style.PaddingTop=3;
			style.TextColor=textColor;
			
			return style;
		}
		/// 
		/// Returns reference to a node that is hosting given control.
		/// 
		/// Reference to the TreeGX control instance
		/// Control instance to look for
		/// Reference to a node hosting control or null if node could not be found
		public static Node FindNodeForControl(TreeGX tree, System.Windows.Forms.Control c)
		{
			if(tree==null || c==null || tree.Nodes.Count==0)
				return null;
			
			Node node = tree.Nodes[0];
			while(node!=null)
			{
				foreach(Cell cell in node.Cells)
				{
					if(cell.HostedControl==c)
						return node;
				}
				node = node.NextVisibleNode;
			}
			
			return null;
		}
	}
	
	#region ComponentFactory
	/// 
	/// Represents internal component factory with design-time support.
	/// 
	internal class ComponentFactory
	{
		private IDesignerHost m_Designer=null; 
		/// 
		/// Creates new instace of the class.
		/// 
		/// Reference to DesignerHost to use for creation of new components.
		public ComponentFactory(IDesignerHost designer)
		{
			m_Designer=designer;
		}
		/// 
		/// Creates new instace of the class.
		/// 
		public ComponentFactory() {}
		/// 
		/// Creates component and returns reference to the new instace.
		/// 
		/// Type that identifies component to create.
		/// New instace of the component.
		public object CreateComponent(Type type)
		{
			object o=null;
			if(m_Designer!=null)
				o=m_Designer.CreateComponent(type);
			else
				o=type.Assembly.CreateInstance(type.FullName);
			return o;
		}
	}
	#endregion
}