using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
namespace VSMediaPlayer
{
	/// <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;

      //If the add-in is loaded from the Add-in Manager dialog, then
      // create and show the tool window:
      if (connectMode == Extensibility.ext_ConnectMode.ext_cm_AfterStartup)
      {
        CreateMediaPlayerToolWindow();
      }
		}

		/// <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)
		{
		}

		/// <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)
		{
      //If the add-in is loaded at startup, then
      // create and show the tool window:
      CreateMediaPlayerToolWindow();
		}

		/// <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)
		{
		}

    void CreateMediaPlayerToolWindow()
    {
      EnvDTE.Windows windows;
      EnvDTE.Window mediaPlayerWindow;
      object controlObject = null;
      string mediaPlayerProgID = "MediaPlayer.MediaPlayer";
      string toolWindowCaption = "Windows Media Player";
      string toolWindowGuid = "{AB5E549E-F823-44BB-8161-BE2BD5D698D8}";

      //Create and show a tool window that hosts the 
      // Windows Media Player control:
      windows = _applicationObject.Windows;
      mediaPlayerWindow = windows.CreateToolWindow(_addInInstance,
                                                   mediaPlayerProgID,
                                                   toolWindowCaption,
                                                   toolWindowGuid,
                                                   ref controlObject);
      mediaPlayerWindow.Visible = true;

      //Play the Windows "Tada" sound:
      //Can only get the system directory (Eg: C:\windows\system32), 
      // need to change this to the Windows install dir
      string mediaFile = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System);
      mediaFile += "\\..\\media\\tada.wav";
      MediaPlayer.IMediaPlayer2 mediaPlayer = (MediaPlayer.IMediaPlayer2)controlObject;

      object[] propertiesWindowObjects = { mediaPlayer };
      mediaPlayerWindow.SetSelectionContainer(ref propertiesWindowObjects);

      mediaPlayer.AutoStart = true;
      mediaPlayer.FileName = mediaFile;
    }
		
		private DTE2 _applicationObject;
		private AddIn _addInInstance;
	}
}