<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Xml.dll" #>
<#@ assembly name="System.Core.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq"#>
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

// This file was generated by a T4 Template. Do not modify directly, instead update the SmoTreeNodesDefinition.xml file
// and re-run the T4 template. This can be done in Visual Studio by right-click in and choosing "Run Custom Tool",
// or from the command-line on any platform by running "build.cmd -Target=CodeGen" or "build.sh -Target=CodeGen"

namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes
{
    /// <summary>
    /// Enum listing possible node types in the object explorer tree
    /// </summary>
    public enum NodeTypes
    {
<#
		var directory = Path.GetDirectoryName(Host.TemplateFile);

		string xmlFile = Path.Combine(directory, "..\\SmoModel\\SmoTreeNodesDefinition.xml");

		var allTreeNodes = GetUniqueNodeTypes(xmlFile);

		foreach (var name in allTreeNodes)
		{
				WriteLine("        {0},", name);
		}

#>
    }
}

<#+
		public static List<string> GetUniqueNodeTypes(string xmlFile)
		{
				XmlDocument doc = new XmlDocument();
				doc.Load(xmlFile);


				// Adding all node types
				HashSet<string> retElements = new HashSet<string>();
				XmlNodeList nodeList = doc.SelectNodes("/ServerExplorerTree/Node");
				foreach (XmlElement item in nodeList)
				{
						retElements.Add(item.GetAttribute("Name"));
						foreach (XmlElement child in GetChildren(xmlFile, item.GetAttribute("Name")))
						{
								retElements.Add(child.GetAttribute("Name"));
						}
				}

				// Adding UniqueTreeNodes
				foreach(var name in GetUniqueTreeNodes(xmlFile))
				{
						retElements.Add(name);
				}

				// Adding types that are not present anywhere. These types are referred in tests.
				retElements.Add("Folder");

				var retList = retElements.ToList();
				retList.Sort();
				return retList;
		}

		public static List<XmlElement> GetChildren(string xmlFile, string parentName)
		{
				XmlElement nodeElement = GetNodeElement(xmlFile, parentName);
				XmlDocument doc = new XmlDocument();
				doc.Load(xmlFile);

				List<XmlElement> retElements = new List<XmlElement>();
				XmlNodeList nodeList = doc.SelectNodes(string.Format("/ServerExplorerTree/Node[@Name='{0}']/Child", parentName));
				foreach (var item in nodeList)
				{
						XmlElement itemAsElement = item as XmlElement;
						if (itemAsElement != null)
						{
								retElements.Add(itemAsElement);
						}
				}
				return retElements;
		}

		public static XmlElement GetNodeElement(string xmlFile, string nodeName)
		{
				XmlDocument doc = new XmlDocument();
		        doc.Load(xmlFile);
				return (XmlElement)doc.SelectSingleNode(string.Format("/ServerExplorerTree/Node[@Name='{0}']", nodeName));
		}

		public static List<string> GetUniqueTreeNodes(string xmlFile)
		{
				XmlDocument doc = new XmlDocument();
				doc.Load(xmlFile);

				List<string> retElements = new List<string>();
				XmlNodeList nodeList = doc.SelectNodes("/ServerExplorerTree/CodeGenOptions/UniqueTreeNode");
				foreach (XmlElement item in nodeList)
				{
						retElements.Add(item.GetAttribute("Name").Replace("TreeNode", string.Empty));

				}
				return retElements;
		}
#>