using System;
using System.Drawing;
using System.IO;


namespace FunctionalSubstructure
{
    
    /// <summary>
    /// Flyweight pattern image file class.
    /// </summary>
    public class ImageFile
        : IDisposable
    {
        
        /// <summary>
        /// The name of the image file.
        /// </summary>
        private string fileName;

        /// <summary>
        /// The image extracted from the file.
        /// </summary>
        private Image anImage;


        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="fileName">The name of the image file</param>
        public ImageFile(string fileName)
        {
            this.fileName = fileName;
        }

		/// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="image">The name of the image file</param>
        public ImageFile(Image img, int index)
        {
			this.fileName = index.ToString();
            this.anImage = img;
        }


        /// <summary>
        /// Gets the image from the file.
        /// </summary>
        public void GetImageFromFile()
        {
            this.anImage = this.FullSizeImage;
        }

        /// <summary>
        /// Gets the full-sized image corresponding to an image file.
        /// </summary>
        public Image FullSizeImage
        {
            get
            {
                try
                {
                    return new Bitmap(fileName);
                }
                catch
                {
                    return GlobalData.InvalidImage;
                }
            }
        }

        /// <summary>
        /// Gets the thumbnail image corresponding to an image file.
        /// </summary>
        public Image Thumbnail
        {
            get
            { 
                Image thumbnail;

                try
                {
                    if (anImage != null)
                        thumbnail = ImageResizer.CreateThumbnailFromImage(anImage, GlobalData.ThumbnailSize);
                    else
                        thumbnail = ImageResizer.CreateThumbnailFromFile(fileName, GlobalData.ThumbnailSize);
                }
                catch
                {
                    thumbnail = GlobalData.InvalidImageThumbnail;
                }
                finally
                {
                    if ((anImage != null) && (anImage != GlobalData.InvalidImage))
                    {
                        anImage.Dispose();
                        anImage = null;
                    }
                }

                return thumbnail;
            }
        }

        /// <summary>
        /// Gets the file name (without path) corresponding to the image.
        /// </summary>
        public string ShortFileName
        {
            get { return fileName.Substring(fileName.LastIndexOf(Path.DirectorySeparatorChar) + 1); }
        }


        /// <summary>
        /// Gets the file name (with path) corresponding to the image.
        /// </summary>
        public string LongFileName
        {
            get { return fileName; }
        }


        public void Dispose()
        {
            if ((anImage != null) && (anImage != GlobalData.InvalidImage))
            {
                anImage.Dispose();
                anImage = null;
            }
            GC.SuppressFinalize(this);
        }

        ~ImageFile()
        {
            if ((anImage != null) && (anImage != GlobalData.InvalidImage))
                anImage.Dispose();
        }

    }

}
