using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
namespace BuildEvents
{
	/// <summary>The object for implementing an Add-in.</summary>
	/// <seealso class='IDTExtensibility2' />
	public class Connect : IDTExtensibility2
	{
		/// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
		public Connect()
		{
		}

		/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
		/// <param term='application'>Root object of the host application.</param>
		/// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
		/// <param term='addInInst'>Object representing this Add-in.</param>
		/// <seealso class='IDTExtensibility2' />
		public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
		{
			_applicationObject = (DTE2)application;
			_addInInstance = (AddIn)addInInst;

      //Setup all available event handlers by creating a new instance
      // of the appropriate delegates:
      buildEvents = _applicationObject.Events.BuildEvents;
      buildEvents.OnBuildBegin += new _dispBuildEvents_OnBuildBeginEventHandler(OnBuildBegin);
      buildEvents.OnBuildDone += new _dispBuildEvents_OnBuildDoneEventHandler(OnBuildDone);
      buildEvents.OnBuildProjConfigBegin += new _dispBuildEvents_OnBuildProjConfigBeginEventHandler(OnBuildProjConfigBegin);
      buildEvents.OnBuildProjConfigDone += new _dispBuildEvents_OnBuildProjConfigDoneEventHandler(OnBuildProjConfigDone);
		}

		/// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
		/// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
		/// <param term='custom'>Array of parameters that are host application specific.</param>
		/// <seealso class='IDTExtensibility2' />
		public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
		{
      //The Add-in is closing. Disconnect the event handlers:
      buildEvents.OnBuildBegin -= new _dispBuildEvents_OnBuildBeginEventHandler(OnBuildBegin);
      buildEvents.OnBuildDone -= new _dispBuildEvents_OnBuildDoneEventHandler(OnBuildDone);
      buildEvents.OnBuildProjConfigBegin -= new _dispBuildEvents_OnBuildProjConfigBeginEventHandler(OnBuildProjConfigBegin);
      buildEvents.OnBuildProjConfigDone -= new _dispBuildEvents_OnBuildProjConfigDoneEventHandler(OnBuildProjConfigDone);
		}

		/// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
		/// <param term='custom'>Array of parameters that are host application specific.</param>
		/// <seealso class='IDTExtensibility2' />		
		public void OnAddInsUpdate(ref Array custom)
		{
		}

		/// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
		/// <param term='custom'>Array of parameters that are host application specific.</param>
		/// <seealso class='IDTExtensibility2' />
		public void OnStartupComplete(ref Array custom)
		{
		}

		/// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
		/// <param term='custom'>Array of parameters that are host application specific.</param>
		/// <seealso class='IDTExtensibility2' />
		public void OnBeginShutdown(ref Array custom)
		{
		}

    //Handlers for each event fired for build events:
    void OnBuildBegin(EnvDTE.vsBuildScope Scope, EnvDTE.vsBuildAction Action)
    {
      string toDisplay;
      toDisplay = "OnBuildBegin\n";
      toDisplay += "Scope: " + Scope.ToString() + "\n";
      toDisplay += "Action: " + Action.ToString();
      System.Windows.Forms.MessageBox.Show(toDisplay);
    }

    void OnBuildDone(EnvDTE.vsBuildScope Scope, EnvDTE.vsBuildAction Action)
    {
      string toDisplay;
      toDisplay = "OnBuildDone\n";
      toDisplay += "Scope: " + Scope.ToString() + "\n";
      toDisplay += "Action: " + Action.ToString();
      System.Windows.Forms.MessageBox.Show(toDisplay);
    }

    void OnBuildProjConfigBegin(string Project, string ProjectConfig, string Platform, string SolutionConfig)
    {
      string toDisplay;
      toDisplay = "OnBuildProjConfigBegin\n";
      toDisplay += "Project: " + Project + "\n";
      toDisplay += "Platform: " + Platform + "\n";
      toDisplay += "Solution Configuration: " + SolutionConfig;
      System.Windows.Forms.MessageBox.Show(toDisplay);
    }

    void OnBuildProjConfigDone(string Project, string ProjectConfig, string Platform, string SolutionConfig, bool Success)
    {
      string toDisplay;
      toDisplay = "OnBuildProjConfigDone\n";
      toDisplay += "Project: " + Project + "\n";
      toDisplay += "Platform: " + Platform + "\n";
      toDisplay += "Solution Configuration: " + SolutionConfig + "\n";
      toDisplay += "Success: " + Success;
      System.Windows.Forms.MessageBox.Show(toDisplay);
    }

		
		private DTE2 _applicationObject;
		private AddIn _addInInstance;

    //The BuildEvents delegate handler variable:
    private EnvDTE.BuildEvents buildEvents;
	}
}