namespace InstrumentationControls
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    internal class GraphicsUtils
    {
        public static void DrawBevel(Graphics g, Rectangle rect, BevelStyle style)
        {
            if (style != BevelStyle.None)
            {
                Point location = rect.Location;
                Point point2 = rect.Location + rect.Size;
                Pen pen = SystemPens.ControlLightLight;
                Pen pen2 = SystemPens.ControlDark;
                Pen pen3 = Pens.Black;
                Pen pen4 = Pens.Black;
                switch (style)
                {
                    case BevelStyle.Flat:
                        pen3 = pen2;
                        pen4 = pen2;
                        break;

                    case BevelStyle.Single:
                        pen3 = Pens.Black;
                        pen4 = Pens.Black;
                        break;

                    case BevelStyle.Double:
                        pen3 = Pens.Black;
                        pen4 = Pens.Black;
                        break;

                    case BevelStyle.Raised:
                        pen3 = pen;
                        pen4 = pen2;
                        break;

                    case BevelStyle.Lowered:
                        pen3 = pen2;
                        pen4 = pen;
                        break;

                    case BevelStyle.DoubleRaised:
                        pen3 = pen;
                        pen4 = pen2;
                        break;

                    case BevelStyle.DoubleLowered:
                        pen3 = pen2;
                        pen4 = pen;
                        break;

                    case BevelStyle.FrameRaised:
                        pen3 = pen;
                        pen4 = pen2;
                        break;

                    case BevelStyle.FrameLowered:
                        pen3 = pen2;
                        pen4 = pen;
                        break;
                }
                g.DrawLine(pen3, location.X, location.Y, location.X, point2.Y);
                g.DrawLine(pen3, location.X, location.Y, point2.X, location.Y);
                g.DrawLine(pen4, location.X, point2.Y, point2.X, point2.Y);
                g.DrawLine(pen4, point2.X, location.Y, point2.X, point2.Y);
                if (((style == BevelStyle.Double) || (style == BevelStyle.DoubleRaised)) || (((style == BevelStyle.DoubleLowered) || (style == BevelStyle.FrameLowered)) || (style == BevelStyle.FrameRaised)))
                {
                    if ((style == BevelStyle.FrameLowered) || (style == BevelStyle.FrameRaised))
                    {
                        Pen pen5 = pen3;
                        pen3 = pen4;
                        pen4 = pen5;
                    }
                    location.X++;
                    location.Y++;
                    point2.X--;
                    point2.Y--;
                    g.DrawLine(pen3, location.X, location.Y, location.X, point2.Y);
                    g.DrawLine(pen3, location.X, location.Y, point2.X, location.Y);
                    g.DrawLine(pen4, location.X, point2.Y, point2.X, point2.Y);
                    g.DrawLine(pen4, point2.X, location.Y, point2.X, point2.Y);
                }
            }
        }

        public static void DrawShape(Graphics g, Rectangle rect, ShapeKinds shape, Color color)
        {
            DrawShape(g, rect, shape, new Pen(color));
        }

        public static void DrawShape(Graphics g, Rectangle rect, ShapeKinds shape, Pen pen)
        {
            GraphicsPath path = ShapeGenerator.GenerateShape(shape, rect);
            g.DrawPath(pen, path);
        }

        public static void FillShape(Graphics g, Rectangle rect, ShapeKinds shape, Brush brush)
        {
            using (GraphicsPath path = ShapeGenerator.GenerateShape(shape, rect))
            {
                g.FillPath(brush, path);
            }
        }

        public static void FillShape(Graphics g, Rectangle rect, ShapeKinds shape, Color color)
        {
            using (SolidBrush brush = new SolidBrush(color))
            {
                FillShape(g, rect, shape, brush);
            }
        }

        public static void FillShape3D(Graphics g, Rectangle rect, ShapeKinds shape, Color color, bool invert)
        {
            Rectangle rectangle = rect;
            Rectangle rectangle2 = rectangle;
            if (invert)
            {
                rectangle2.X -= (int) (rectangle2.Width * 0.2f);
                rectangle2.Y -= (int) (rectangle2.Height * 0.2f);
                rectangle2.Height = (int) (rectangle2.Height * 2f);
                rectangle2.Width = (int) (rectangle2.Width * 2f);
            }
            else
            {
                rectangle2.X -= (int) (rectangle2.Width * 0.6f);
                rectangle2.Y -= (int) (rectangle2.Height * 0.6f);
                rectangle2.Height = (int) (rectangle2.Height * 1.85f);
                rectangle2.Width = (int) (rectangle2.Width * 1.85f);
            }
            ShapeKinds ellipse = ShapeKinds.Rectangle;
            if (shape == ShapeKinds.Ellipse)
            {
                ellipse = ShapeKinds.Ellipse;
            }
            using (GraphicsPath path = ShapeGenerator.GenerateShape(ellipse, rectangle2))
            {
                using (PathGradientBrush brush = new PathGradientBrush(path))
                {
                    brush.CenterColor=color;
                    Color[] colorArray = new Color[] { ScaleColor(color, 0.7f) };
                    brush.SurroundColors =colorArray;
                    FillShape(g, rect, shape, (Brush) brush);
                }
            }
        }

        public static Color GradientColor(Color startColor, Color endColor, float position)
        {
            int red = (int) ((startColor.R * (position - 1f)) + (endColor.R * position));
            int green = (int) ((startColor.G * (position - 1f)) + (endColor.G * position));
            int blue = (int) ((startColor.B * (position - 1f)) + (endColor.B * position));
            if (red > 0xff)
            {
                red = 0xff;
            }
            if (green > 0xff)
            {
                green = 0xff;
            }
            if (blue > 0xff)
            {
                blue = 0xff;
            }
            return Color.FromArgb(red, green, blue);
        }

        public static Color ScaleColor(Color sourceColor, float scale)
        {
            int red = (int) (sourceColor.R * scale);
            int green = (int) (sourceColor.G * scale);
            int blue = (int) (sourceColor.B * scale);
            if (red > 0xff)
            {
                red = 0xff;
            }
            if (green > 0xff)
            {
                green = 0xff;
            }
            if (blue > 0xff)
            {
                blue = 0xff;
            }
            return Color.FromArgb(red, green, blue);
        }

        public static Color TransparentColor(Color sourceColor, float alpha)
        {
            return Color.FromArgb((int) (alpha * 255f), sourceColor.R, sourceColor.G, sourceColor.B);
        }
    }
}
