using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace MusicMetaTagger.UI
{
	/// <summary>
	///   GUI object for selecting an IMusicLibraryinterface
	/// </summary>
	[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof (IDesigner))]
	public partial class LibrarySelector : UserControl
	{
		public event PropertyChangedEventHandler LibraryNameChanged;

		/// <summary>
		///   Text assocatied with the imageList array objects
		/// </summary>
		private static readonly string[] LibraryNames = new[] {"iTunes", "Windows Media Player"};

		/// <summary>
		///   Gets the currently selected library.
		/// </summary>
		/// <value>The selected library.</value>
		public IMusicLibraryInterface SelectedLibrary { get; private set; }

		/// <summary>
		///   Initializes a new instance of the <see cref = "LibrarySelector" /> class.
		/// </summary>
		public LibrarySelector()
		{
			InitializeComponent();
		}

		/// <summary>
		///   Handles the SelectionChangeCommitted event of the libraryTypeComboBox control.
		/// </summary>
		/// <param name = "sender">The source of the event.</param>
		/// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
		private void libraryTypeComboBox_SelectionChangeCommitted(object sender, EventArgs e)
		{
			if (libraryTypeComboBox.SelectedIndex == 0)
			{
				SelectedLibrary = new ItunesLibraryInterface();
				pictureBox.Image = imageList.Images[0];
			}
			else if (libraryTypeComboBox.SelectedIndex == 1)
			{
				SelectedLibrary = new MediaPlayerLibraryInterface();
				pictureBox.Image = imageList.Images[1];
			}

			if (LibraryNameChanged != null)
				LibraryNameChanged.Invoke(this, new PropertyChangedEventArgs("SelectedLibrary"));
		}

		/// <summary>
		///   Handles the Click event of the LibrarySelector control.
		/// </summary>
		/// <param name = "sender">The source of the event.</param>
		/// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
		private void LibrarySelector_Click(object sender, EventArgs e)
		{
			libraryTypeComboBox.DroppedDown = !libraryTypeComboBox.DroppedDown;
		}

		/// <summary>
		///   Handles the Click event of the pictureBox control.
		/// </summary>
		/// <param name = "sender">The source of the event.</param>
		/// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
		private void pictureBox_Click(object sender, EventArgs e)
		{
			libraryTypeComboBox.DroppedDown = !libraryTypeComboBox.DroppedDown;
		}

		/// <summary>
		///   Handles the Load event of the LibrarySelector control.
		/// </summary>
		/// <param name = "sender">The source of the event.</param>
		/// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
		private void LibrarySelector_Load(object sender, EventArgs e)
		{
			libraryTypeComboBox.Items.AddRange(LibraryNames);
			libraryTypeComboBox.SelectedIndex = 0;
			libraryTypeComboBox_SelectionChangeCommitted(this, e);
		}

		/// <summary>
		///   Handles the KeyDown event of the libraryTypeComboBox control.
		/// </summary>
		/// <param name = "sender">The source of the event.</param>
		/// <param name = "e">The <see cref = "System.Windows.Forms.KeyEventArgs" /> instance containing the event data.</param>
		private void libraryTypeComboBox_KeyDown(object sender, KeyEventArgs e)
		{
			e.SuppressKeyPress = true;
		}
	}
}