using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Collections.Specialized;

namespace NotepadEnhanced.Helpers
{
    public static class ExtensionMethods
    {
        /// <summary>
        /// Adjusts the size of the current font by adding or subtracting from its current size then
        /// returning the new font
        /// </summary>
        public static Font SetSize(this Font oldFont, int amount)
        {
            if (oldFont.Size <= 1 && amount < 0) amount = 1;
            return new Font(oldFont.FontFamily.Name, oldFont.Size + amount, oldFont.Style);
        }

        /// <summary>
        /// Adjusts the size of the current font by adding or subtracting from its current size then
        /// returning the new font
        /// </summary>
        public static Font SetStyle(this Font oldFont, FontStyle style)
        {
            return new Font(oldFont.FontFamily.Name, oldFont.Size, style);
        }

        /// <summary>
        /// Gets whether this number is an odd one
        /// </summary>
        public static bool IsOdd(this int number)
        {
            return ((number & 1).Equals(0));
        }

        /// <summary>
        /// Converts a color to solid color represented by an integer
        /// </summary>
        public static int ToRgb(this Color color)
        {
            return (color.R) | (color.G << 8) | (color.B << 16);
        }

        public static StringCollection Reverse(this StringCollection SC)
        {
            var temp = new StringCollection();

            for (int i = SC.Count - 1; i >= 0; i--)
                temp.Add(SC[i]);

            return temp;
        }

        /// <summary>
        /// Gets the most basic info for this font
        /// </summary>
        public static string GetBasicInfo(this Font font)
        {
            return font.FontFamily.Name + " " + font.Size;
        }

        public static bool IsFileLarge(this FileInfo info)
        {
            // any thing over 120000 bytes will be considered a large document
            const int SIZE_THRESHOLD = 120000;
            if (!info.Exists)  return false;
            return (info.Length > SIZE_THRESHOLD);
        }

        /// <summary>
        /// Docks the form to the left of the containing screen
        /// </summary>
        public static void DockLeft(this Form form)
        {
            form.WindowState = FormWindowState.Normal;
            Rectangle bounds = Screen.GetWorkingArea(form);
            form.Bounds = new Rectangle(bounds.X, bounds.Y, bounds.Width / 2, bounds.Height);
        }

        /// <summary>
        /// Docks the form to the bottom of the containing screen
        /// </summary>
        public static void DockBottom(this Form form)
        {
            form.WindowState = FormWindowState.Normal;
            Rectangle bounds = Screen.GetWorkingArea(form);
            int height = bounds.Height / 2;
            form.Bounds = new Rectangle(bounds.X, bounds.Y + height, bounds.Width, height);
        }

        /// <summary>
        /// Docks the form to the top of the containing screen
        /// </summary>
        public static void DockTop(this Form form)
        {
            form.WindowState = FormWindowState.Normal;
            Rectangle bounds = Screen.GetWorkingArea(form);
            form.Bounds = new Rectangle(bounds.X, bounds.Y, bounds.Width,  bounds.Height / 2);
        }

        /// <summary>
        /// Docks the form to the right of the containing screen
        /// </summary>
        public static void DockRight(this Form form)
        {
            form.WindowState = FormWindowState.Normal;
            Rectangle bounds = Screen.GetWorkingArea(form);
            int xPos = bounds.X + bounds.Width / 2;
            form.Bounds = new Rectangle(xPos, bounds.Y, bounds.Width / 2, bounds.Height);
        }
    }
}
