using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using System.Reflection;
namespace SmoObjectHierarchy
{
    public partial class Form1 : Form
    {

        String GetSteps(Type type)
        {
            String steps = String.Empty;
            PropertyInfo propertyInfo = type.GetProperty("Parent");
            if (propertyInfo != null)
            {
                steps = GetSteps(propertyInfo.PropertyType) + "/";
            }
            String thisStep = type.Name + "[Name=''";
            if(null != type.GetProperty("Schema"))
            {
                thisStep += " and Schema=''";
            }
            thisStep += "]";
            steps = steps + thisStep;
            return steps;
        }
        public Form1()
        {
          
            InitializeComponent();
          
            treeView1.BeginUpdate();
            treeView1.BackColor = Color.LightGoldenrodYellow;
            rightPad = this.ClientRectangle.Right
                - (treeView1.Location.X + treeView1.Width);
            bottomPad = this.ClientRectangle.Bottom
                - (treeView1.Location.Y + treeView1.Height);
            TreeNode node = new TreeNode("Server");
            treeView1.Nodes.Add(node);
            AddChildren(node, typeof(Server));
            treeView1.ExpandAll();
            treeView1.EndUpdate();
          
        }
        Int32 rightPad;
        Int32 bottomPad;
        private TreeNode MakeNode(TreeNode parent, Type type)
        {
            TreeNode node = new TreeNode(type.Name);
            
            parent.Nodes.Add(node);
            return node;
        }
        private void AddChildren(TreeNode parent, Type type)
        {
            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                if (propertyInfo.Name == "Parent")
                {
                    continue;
                }
                Type pType = propertyInfo.PropertyType;
                if (pType.IsSubclassOf(typeof(SqlSmoObject)))
                {
                        TreeNode child = new TreeNode(pType.Name + "(" + propertyInfo.Name + ")");
                        parent.Nodes.Add(child);
                        //child.BackColor = Color.LightGray;
                    
                        AddChildren(child, pType);
                }
                if(pType.IsSubclassOf(typeof(AbstractCollectionBase)))
                {
                    TreeNode child = new TreeNode(pType.Name + "(" + propertyInfo.Name + ")");
                    child.ToolTipText = GetSteps(pType);
                    parent.Nodes.Add(child);
                    PropertyInfo pi = pType.GetProperty("Item", new Type[] {typeof(Int32)});
                    if(pi != null)
                    {
                        TreeNode item = new TreeNode(pi.PropertyType.Name);
                        item.ToolTipText = GetSteps(pi.PropertyType);
                        child.Nodes.Add(item);
                        //child.BackColor = Color.LightGray;
                        AddChildren(item, pi.PropertyType);
                    }
                }
            }
        }
        private void SetTreeSize()
        {
            Rectangle r = this.RectangleToScreen(this.ClientRectangle);
            Int32 newWidth =
               r.Width - rightPad - treeView1.Location.X;
            Int32 newHeight =
                r.Height - bottomPad - treeView1.Location.Y;
            treeView1.Size = new Size(newWidth, newHeight);

        }
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
           
            OnResize(e);
            SetTreeSize();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            treeView1.BeginUpdate();
            treeView1.ExpandAll();
            treeView1.EndUpdate();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            treeView1.CollapseAll();
        }
    }
}