using System;
using System.Drawing;
using System.Windows.Forms;


namespace FunctionalSubstructure
{
    
    public partial class ThumbnailBox : UserControl
    {
        /// <summary>
        /// Height of the image name label control.
        /// </summary>
        private const int ImageNameHeight = 10;

        /// <summary>
        /// Separator height, separating the label from the image.
        /// </summary>
        private const int SeparatorHeight = 5;


        /// <summary>
        /// The thumbnails sequence parent container.
        /// </summary>
        private ThumbnailsSequence thumbnailsSequence;

        /// <summary>
        /// The index of the thumbnail in the thumbnail sequence of the folder.
        /// </summary>
        private int thumbnailIndex;

        /// <summary>
        /// The image file whose thumbnail is to be displayed.
        /// </summary>
        private ImageFile imageFile;

        /// <summary>
        /// Helper delegate for the thread setting the image thumbnail.
        /// </summary>
        private delegate void SetThumbnailImageThreadDelegate(Image thumbnail);



        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="thumbnailsSequence">The thumbnails sequence parent container</param>
        /// <param name="thumbnailIndex">The index of the thumbnail in the thumbnail sequence of the folder</param>
        /// <param name="imageFile">The image file whose thumbnail is to be displayed</param>
        public ThumbnailBox(ThumbnailsSequence thumbnailsSequence, int thumbnailIndex, ImageFile imageFile)
        {
            InitializeComponent();

            this.thumbnailsSequence = thumbnailsSequence;
            this.thumbnailIndex = thumbnailIndex;
            this.imageFile = imageFile;

            SetLoadingImageThumbnail();
        }


        /// <summary>
        /// Gets or sets the index of the thumbnail in the thumbnail sequence of the folder.
        /// </summary>
        public int ThumbnailIndex
        {
            get { return thumbnailIndex; }
            set { thumbnailIndex = value; }
        }


        /// <summary>
        /// Gets the image corresponding to the thumbnail.
        /// </summary>
        public ImageFile Image
        {
            get { return imageFile; }
        }


        /// <summary>
        /// Thread method that sets the thumbnail image to be displayed.
        /// </summary>
        public void SetThumbnailImageThread()
        {
            Image anImage = imageFile.Thumbnail;

            if (this.InvokeRequired)
                this.Invoke(new SetThumbnailImageThreadDelegate(SetThumbnailImageHelper), anImage);
            else
                SetThumbnailImageHelper(anImage);
        }

        /// <summary>
        /// Thread method that reads the image from disc.
        /// </summary>
        public void ReadImageFromDiscThread()
        {
            imageFile.GetImageFromFile();
        }


        /// <summary>
        /// Helper method for setting the thumbnail image.
        /// </summary>
        /// <param name="anImage">The thumbnail image to be set</param>
        private void SetThumbnailImageHelper(Image anImage)
        {
            pbThumbnail.Image = anImage;
        }


        /// <summary>
        /// Sets the image load logo as thumbnail.
        /// </summary>
        private void SetLoadingImageThumbnail()
        {
            pbThumbnail.Image = GlobalData.LoadingImageThumbnail;
            pbThumbnail.Size = new Size(GlobalData.ThumbnailSize, GlobalData.ThumbnailSize);

            lbImageName.Size = new Size(GlobalData.ThumbnailSize, ImageNameHeight);
            lbImageName.Text = imageFile.ShortFileName;

            this.Size = new Size(GlobalData.ThumbnailSize, GlobalData.ThumbnailSize);// + ImageNameHeight + SeparatorHeight);
        }


        /// <summary>
        /// Sets the border style of the control.
        /// </summary>
        /// <param name="borderStyle">The border style to be set</param>
        public void SetBorder(BorderStyle borderStyle)
        {
            this.BorderStyle = borderStyle;
        }


        protected override bool IsInputKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Up:
                case Keys.Down:
                case Keys.Left:
                case Keys.Right:
                    return true;
                default:
                    return base.IsInputKey(keyData);
            }
        }


        /// <summary>
        /// Loads the form on the screen.
        /// </summary>
        private void ThumbnailBox_Load(object sender, EventArgs e)
        {
            foreach (Control aControl in this.Controls)
            {
                aControl.Cursor = this.Cursor;
                aControl.MouseClick += new MouseEventHandler(ThumbnailBox_MouseClick);
				aControl.MouseDoubleClick += new MouseEventHandler(ThumbnailBox_MouseDoubleClick);
            }
        }
        
        
        /// <summary>
        /// Creates a new form to display the full-size image corresponding to the thumbnail.
        /// </summary>
        private void ThumbnailBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                thumbnailsSequence.MoveToThumbnail(this);
            }
        }

		private void ThumbnailBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                thumbnailsSequence.MoveToThumbnail(this);
				thumbnailsSequence.InvokeSuggestion();
            }
        }


        private void ThumbnailBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

            }
            else if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.W))
                thumbnailsSequence.MoveToUpperThumbnail();
            else if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.S))
                thumbnailsSequence.MoveToLowerThumbnail();
            else if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.A))
                thumbnailsSequence.MoveToLeftThumbnail();
            else if ((e.KeyCode == Keys.Right) || (e.KeyCode == Keys.D))
                thumbnailsSequence.MoveToRightThumbnail();
        }

    }

}
