using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace DevComponents.DotNetBar.Metro.Rendering
{
    internal static class DrawingHelpers
    {
        /// 
        /// Draws the border.
        /// 
        /// Graphics canvas.
        /// Bounds for border.
        /// Border thickness.
        /// Border color.
        public static void DrawBorder(Graphics g, RectangleF bounds, Thickness borderThickness, BorderColors borderColor)
        {
            if (borderColor.IsEmpty || borderThickness.IsZero) return;
            bounds.Width -= (float)borderThickness.Right;
            bounds.Height -= (float)borderThickness.Bottom;
            if (borderThickness.Right > 1) bounds.Width++;
            if (borderThickness.Bottom > 1) bounds.Height++;
            System.Drawing.Drawing2D.SmoothingMode sm = g.SmoothingMode;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
            if (borderThickness.Left > 0d && !borderColor.Left.IsEmpty)
            {
                using (Pen pen = new Pen(borderColor.Left, (float)borderThickness.Left))
                {
                    pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                    g.DrawLine(pen, bounds.X, bounds.Y, bounds.X, bounds.Bottom);
                }
            }
            if (borderThickness.Top > 0d && !borderColor.Top.IsEmpty)
            {
                using (Pen pen = new Pen(borderColor.Top, (float)borderThickness.Top))
                {
                    pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                    g.DrawLine(pen, bounds.X, bounds.Y, bounds.Right, bounds.Y);
                }
            }
            if (borderThickness.Right > 0d && !borderColor.Right.IsEmpty)
            {
                using (Pen pen = new Pen(borderColor.Right, (float)borderThickness.Right))
                {
                    pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                    g.DrawLine(pen, bounds.Right, bounds.Y, bounds.Right, bounds.Bottom + (float)borderThickness.Bottom / 2);
                }
            }
            if (borderThickness.Bottom > 0d && !borderColor.Bottom.IsEmpty)
            {
                using (Pen pen = new Pen(borderColor.Bottom, (float)borderThickness.Bottom))
                {
                    pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                    g.DrawLine(pen, bounds.X, bounds.Bottom, bounds.Right, bounds.Bottom);
                }
            }
            g.SmoothingMode = sm;
        }
        /// 
        /// Draws background.
        /// 
        /// Graphics canvas.
        /// Background bounds.
        /// Background color
        public static void DrawBackground(Graphics g, RectangleF bounds, string color)
        {
            if (string.IsNullOrEmpty(color)) return;
            using (SolidBrush brush = new SolidBrush(ColorScheme.GetColor(color)))
                g.FillRectangle(brush, bounds);
        }
        /// 
        /// Deflates the rectangle by the border thickness.
        /// 
        /// Rectangle.
        /// Border thickness
        /// Rectangle deflated by the border thickness
        public static RectangleF Deflate(RectangleF bounds, Thickness borderThickness)
        {
            if (borderThickness.IsZero) return bounds;
            bounds.X += (float)borderThickness.Left;
            bounds.Width -= (float)(borderThickness.Left + borderThickness.Right);
            bounds.Y += (float)borderThickness.Top;
            bounds.Height -= (float)(borderThickness.Top + borderThickness.Bottom);
            return bounds;
        }
    }
}