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


namespace FunctionalSubstructure
{
    
    /// <summary>
    /// Static class containing image resizing operations.
    /// </summary>
    public static class ImageResizer
    {

        /// <summary>
        /// Returns the thumbnail resized proportionally to the given maximum dimension.
        /// </summary>
        /// <param name="fileName">The name of the image file</param>
        /// <param name="newSize">The maximum dimension to resize the image to</param>
        /// <returns>The thumbnail resized proportionally to the given maximum dimension</returns>
        public static Image CreateThumbnailFromFile(string fileName, int newSize)
        {
            Image tempImage;

            try
            {
                tempImage = new Bitmap(fileName);
            }
            catch
            {
                tempImage = GlobalData.InvalidImage;
            }

            Image thumbnail = CreateThumbnailFromImage(tempImage, newSize);

            if (tempImage != GlobalData.InvalidImage)
                tempImage.Dispose();

            return thumbnail;
        }


        /// <summary>
        /// Returns the thumbnail resized proportionally to the given maximum dimension.
        /// </summary>
        /// <param name="image">The image to be resized</param>
        /// <param name="newSize">The maximum dimension to resize the image to</param>
        /// <returns>The thumbnail resized proportionally to the given maximum dimension</returns>
        public static Image CreateThumbnailFromImage(Image image, int newSize)
        {
            int width = image.Width;
            int height = image.Height;

            if (width >= height)
            {
                float aspectRatio = (float)width / (float)height;
                width = newSize;
                height = (int)((float)width / aspectRatio);
            }

            else
            {
                float aspectRatio = (float)height / (float)width;
                height = newSize;
                width = (int)((float)height / aspectRatio);
            }

            Image thumbnail = new Bitmap(width, height);

            try
            {
                using (Graphics graphics = Graphics.FromImage(thumbnail))
                    graphics.DrawImage(image, 0, 0, width, height);
            }
            catch
            {
                using (Graphics graphics = Graphics.FromImage(thumbnail))
                    graphics.DrawImage(GlobalData.InvalidImage, 0, 0, width, height);
            }

            return thumbnail;
        }


        /// <summary>
        /// Returns the full-screen image resized proportionally to the screen size.
        /// </summary>
        /// <param name="image">The image to be resized</param>
        /// <returns>The image resized proportionally to the screen size</returns>
        public static Image CreateResizedFullScreenImageFromImage(Image image)
        {
            int width = image.Width;
            int height = image.Height;

            if (width >= height)
            {
                float aspectRatio = (float)width / (float)height;

                width = Screen.PrimaryScreen.Bounds.Width;
                height = (int)((float)width / aspectRatio);

                if (height > Screen.PrimaryScreen.Bounds.Height)
                {
                    height = Screen.PrimaryScreen.Bounds.Height;
                    width = (int)(height * aspectRatio);
                }
            }

            else
            {
                float aspectRatio = (float)height / (float)width;

                height = Screen.PrimaryScreen.Bounds.Height;
                width = (int)((float)height / aspectRatio);

                if (width > Screen.PrimaryScreen.Bounds.Width)
                {
                    width = Screen.PrimaryScreen.Bounds.Width;
                    height = (int)(width * aspectRatio);
                }
            }

            Image fullScreenImage = new Bitmap(width, height);

            try
            {
                using (Graphics graphics = Graphics.FromImage(fullScreenImage))
                    graphics.DrawImage(image, 0, 0, width, height);
            }
            catch
            {
                using (Graphics graphics = Graphics.FromImage(fullScreenImage))
                    graphics.DrawImage(GlobalData.InvalidImage, 0, 0, width, height);
            }

            return fullScreenImage;
        }

    }

}

