using System;
using System.DirectoryServices;
using System.Windows.Forms;
//Source - http://weblogs.asp.net/jezell/archive/2003/09/17/27869.aspx
//http://weblogs.austinspad.com/caustin/archive/2004/10/21/234.aspx
//http://www.codeproject.com/csharp/virtualdir.asp
namespace InstallerActions.Install
{
	/// <summary>
	/// Summary description for IISVirDirCreator.
	/// </summary>
	public class IISVirDirCreator
	{

		private string _serverName;
		private DirectoryEntry _iisServer;
		public static string VirDirSchemaName = "IIsWebVirtualDir";
		private DirectoryEntry folderRoot;

		/// <summary>
		///		Constructor
		/// </summary>
		/// <param name="serverName">Name of the IIS Server</param>
		public IISVirDirCreator(string serverName)
		{
			_serverName = serverName;
		}
		/// <summary>
		///		Default constructor uses localhost as default server
		/// </summary>
		public IISVirDirCreator()
		{
			_serverName = "localhost";
			//MessageBox.Show("Set Server Name");
		}

		/// <summary>
		///		Connect to IISServer
		/// </summary>
		public void Connect()
		{
			try
			{
				_iisServer = new DirectoryEntry("IIS://" + _serverName + "/W3SVC/1");									
				folderRoot = _iisServer.Children.Find("Root",VirDirSchemaName);
			}
			catch (Exception e)			{
				
				throw new Exception("Could not connect to: " + _serverName,e);
			}
		}

		public void CreateVirtualDirectory(string virDirName, string realPath)
		{
			//Test Connection
			try
			{
				//MessageBox.Show("Trying to Connect to web server");
				Connect();
			}
			catch (Exception e)
			{
				throw new Exception("Could not connect to: " + _serverName,e);
			}

			//MessageBox.Show("Test Connection Succeeded");
			//DirectoryEntry folderRoot = _iisServer.Children.Find("Root",VirDirSchemaName);

			try
			{
				DirectoryEntry newVirDir = folderRoot.Children.Add(virDirName, VirDirSchemaName);
				
				newVirDir.Properties["AccessRead"].Add(true);
				newVirDir.Properties["Path"][0] = realPath;
				newVirDir.Properties["AccessScript"][0] = true; 
				
				newVirDir.Properties["AppIsolated"][0] = 2;
				newVirDir.Properties["AccessFlags"][0] = 513;
				newVirDir.Properties["FrontPageWeb"][0] = 1;								

				newVirDir.Invoke("AppCreate",true);
				newVirDir.CommitChanges();
				folderRoot.CommitChanges();
				_iisServer.CommitChanges();
				
//				site.Invoke("Put", "ServerComment", webSiteName);
//				site.Invoke("Put", "KeyType", "IIsWebServer");
//				site.Invoke("Put", "ServerBindings", ":80:");
//				site.Invoke("Put", "ServerState", 2);
//				site.Invoke("Put", "FrontPageWeb", 1);
//				site.Invoke("Put", "DefaultDoc", "Default.aspx");
//				site.Invoke("Put", "SecureBindings", ":443:");
//				site.Invoke("Put", "ServerAutoStart", 1);
//				site.Invoke("Put", "ServerSize", 1);
//				site.Invoke("SetInfo");
				// Create application virtual directory
//				DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
//				siteVDir.Properties["AppIsolated"][0] = 2;
//				siteVDir.Properties["Path"][0] = pathToRoot;
//				siteVDir.Properties["AccessScript"][0] = true; 
//				siteVDir.Properties["AccessFlags"][0] = 513;
//				siteVDir.Properties["FrontPageWeb"][0] = 1;
//				siteVDir.Properties["AppRoot"][0] = "/LM/W3SVC/"+siteID+"/Root";
//				siteVDir.Properties["AppFriendlyName"][0] = "Root";
//				siteVDir.CommitChanges();
//				site.CommitChanges();
//				return siteID;
			}
			catch(Exception ex)
			{
				throw new Exception("Virtual Directory " + virDirName + " Already Exists",ex);
			}
		}

		public void CreateVirtualDirectory(string virDirName, string parentVirDirName, string realPath)
		{
			//Test Connection
			Connect();
			//MessageBox.Show("Test Connection Succeeded");
			//DirectoryEntry folderRoot = _iisServer.Children.Find("Root",VirDirSchemaName);

			DirectoryEntry parentVirDir = folderRoot.Children.Find(parentVirDirName, VirDirSchemaName);

			if (parentVirDir == null)
			{
				//MessageBox.Show("parentVirDir is null");
			}
			else
			{
				//MessageBox.Show("parentVirDir is NOT null");
			}

			try
			{
				DirectoryEntry newVirDir = parentVirDir.Children.Add(virDirName, VirDirSchemaName);
				//newVirDir.Path = pathToRoot;
				//newVirDir.Properties["Path"][0] = pathToRoot;
				//newVirDir.AuthenticationType = AuthenticationTypes.Anonymous;
				
				newVirDir.Properties["AccessRead"].Add(true);
				newVirDir.Properties["Path"][0] = realPath;
				newVirDir.Properties["AccessScript"][0] = true; 

				newVirDir.Properties["AppIsolated"][0] = 2;
				newVirDir.Properties["AccessFlags"][0] = 513;
				newVirDir.Properties["FrontPageWeb"][0] = 1;								

				newVirDir.Invoke("AppCreate",true);
				newVirDir.CommitChanges();
				parentVirDir.CommitChanges();
				folderRoot.CommitChanges();
				_iisServer.CommitChanges();
				
				//				site.Invoke("Put", "ServerComment", webSiteName);
				//				site.Invoke("Put", "KeyType", "IIsWebServer");
				//				site.Invoke("Put", "ServerBindings", ":80:");
				//				site.Invoke("Put", "ServerState", 2);
				//				site.Invoke("Put", "FrontPageWeb", 1);
				//				site.Invoke("Put", "DefaultDoc", "Default.aspx");
				//				site.Invoke("Put", "SecureBindings", ":443:");
				//				site.Invoke("Put", "ServerAutoStart", 1);
				//				site.Invoke("Put", "ServerSize", 1);
				//				site.Invoke("SetInfo");
				// Create application virtual directory
				//				DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
				//				siteVDir.Properties["AppIsolated"][0] = 2;
				//				siteVDir.Properties["Path"][0] = pathToRoot;
				//				siteVDir.Properties["AccessScript"][0] = true; 
				//				siteVDir.Properties["AccessFlags"][0] = 513;
				//				siteVDir.Properties["FrontPageWeb"][0] = 1;
				//				siteVDir.Properties["AppRoot"][0] = "/LM/W3SVC/"+siteID+"/Root";
				//				siteVDir.Properties["AppFriendlyName"][0] = "Root";
				//				siteVDir.CommitChanges();
				//				site.CommitChanges();
				//				return siteID;
			}
			catch(Exception ex)
			{
				throw new Exception("Virtual Directory " + virDirName + " Already Exists",ex);
			}
		}

		public void DeleteVirtualDirectory(string virDirName, string realPath)
		{
			//Test Connection
			try
			{
				//MessageBox.Show("Trying to Connect to web server");
				Connect();
			}
			catch (Exception e)
			{
				MessageBox.Show("Could not connect to web server: " + _serverName + 
					"\nError - " + e.ToString() + 
					"\nCould not remove the virtual directory" + virDirName + "." + 
					"\nPlease remove it manually");
			}

			//MessageBox.Show("Test Connection Succeeded");			

			try
			{
				DirectoryEntry deleteVirDir = folderRoot.Children.Find(virDirName, VirDirSchemaName);				
				folderRoot.Children.Remove(deleteVirDir);				
				folderRoot.CommitChanges();
				_iisServer.CommitChanges();			
			}
			catch(Exception ex)
			{
				MessageBox.Show("Could not remove virtual directory: " + virDirName + "." +
					"\nError - " + ex.ToString() + 					
					"\nPlease remove it manually");
			}
		}


		#region Properties
		public string ServerName
		{
			get
			{
				return _serverName;
			}
			set
			{
				_serverName = value;
			}
		}
		#endregion
	}
}
