using System;
using System.Windows.Forms;
using System.Drawing;
 
namespace smDragDropTestApp
{
    /// <summary>
    /// Controls what happens in the tile.
    /// </summary>
	public class smTile : System.Windows.Forms.PictureBox
	{
        #region Protected member variables
        /// <summary>
        /// The image used when it has an item
        /// </summary>
		protected System.Drawing.Bitmap filledImage;
        /// <summary>
        /// Image used when there is no item
        /// </summary>
		protected System.Drawing.Bitmap emptyImage;
        /// <summary>
        /// Image used when the drag is over the tile
        /// </summary>
		protected System.Drawing.Bitmap overImage;
        /// <summary>
        /// Signals whether it holds an item or not
        /// </summary>
		protected bool hasItem;
        #endregion

        #region Empty constructor
        /// <summary>
        /// Loads the picutres and initializes the tile
        /// </summary>
		public smTile()
		{
			// Load the pictures
			filledImage = new System.Drawing.Bitmap("dotBox.jpg");
			emptyImage = new System.Drawing.Bitmap("emptyBox.jpg");
			overImage = new System.Drawing.Bitmap("overBox.jpg");

			// Initialize
			hasItem = false;

            // Initialize events and properties
			this.Image = emptyImage;
			this.Height = emptyImage.Height;
			this.Width = emptyImage.Width;
			this.MouseDown += new MouseEventHandler(OnMouseDown);
			this.DragEnter += new DragEventHandler(OnDragEnter);
			this.DragDrop += new DragEventHandler(OnDragDrop);
			this.DragLeave += new EventHandler(OnDragLeave);
			this.AllowDrop = true;
		}
        #endregion

        #region HasItem
        /// <value>
        /// bool that signals whether this tile contains an item or not (read only)
        /// </value>
		public bool HasItem
		{
			get
			{
				return hasItem;
			}
		}
        #endregion

        #region OnDragDrop
        /// <summary>
        /// Handles the DragDrop event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		public void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
            // Switch the cursor to the arrow
			this.Cursor = Cursors.Default;

			// Put an item here
			PutItem();			
		}
        #endregion

        #region OnDragEnter
        /// <summary>
        /// Handles the DragEnter event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		public void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
			if (hasItem == false) {
		
		        // Check to see if we can handle what is being dragged
                if (e.Data.GetDataPresent(DataFormats.Bitmap)) {
                
                    // Change the cursor to a hand
				    this.Cursor = Cursors.Hand;

                    // Lets it know what effects can be done
					e.Effect = DragDropEffects.Copy;

                    // Change the image
					this.Image = overImage;
				}			
			}
			else {
                // Change the cursor to the arrow
				this.Cursor = Cursors.Default;
			}
		}
        #endregion

        #region OnDragLeave
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		public void OnDragLeave(object sender, System.EventArgs e)
		{
            // Only do something if the tile has an item
			if (hasItem == false) {
                // Reset the cursor
				this.Cursor = Cursors.Default;
                // Switch the image back to empty
				this.Image = emptyImage;
			}
		}
        #endregion

        #region OnGiveFeedback
        /// <summary>
        /// Handles the feedback event
        /// </summary>
        /// <param name="gfbEvent"></param>
		protected override void OnGiveFeedback(GiveFeedbackEventArgs gfbEvent)
		{
            // Allow us to use our own cursors when dragging
			gfbEvent.UseDefaultCursors = false;			
		}
        #endregion

        #region OnMouseDown
        /// <summary>
        /// Handles the mouse down event
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">MouseEventArgs</param>
		public void OnMouseDown(object sender,MouseEventArgs e)   
		{
			// Only allowing dragging if there is an item
			if (hasItem) {
				
				// Start the dragging process
				DragDropEffects effect = DoDragDrop(this.Image, DragDropEffects.Copy);
				
				// Check to see if the image was dropped somewhere
				if (effect != DragDropEffects.None) {

					// It was dropped so remove the item
					RemoveItem();
				}			
			}
		}
        #endregion

        #region PutItem
        /// <summary>
        /// Put an item on the tile.
        /// </summary>
		public void PutItem()
		{
            // Set the hasItem signal
			hasItem = true;
            // Set the image to filled
			this.Image = filledImage;
		}
        #endregion

        #region RemoveItem
        /// <summary>
        /// Takes the item off the tile
        /// </summary>
		public void RemoveItem()
		{
            // Set the signal to false
			hasItem = false;
            // Set the image to empty
			this.Image = emptyImage;
		}
        #endregion
	}
}