using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace DevComponents.DotNetBar.Layout
{
    internal static class DrawingHelpers
    {
        /// 
        /// Draws the border.
        /// 
        /// Graphics canvas.
        /// Bounds for border.
        /// Border thickness.
        /// Border pattern.
        /// Border color.
        public static void DrawBorder(Graphics g, RectangleF bounds, Thickness borderThickness, BorderPattern borderPattern, BorderColors borderColor)
        {
            if (borderColor.IsEmpty || borderThickness.IsZero || borderPattern.IsEmpty) return;
            Region oldClip = g.Clip;
            g.SetClip(bounds, System.Drawing.Drawing2D.CombineMode.Intersect);
            bounds.Width -= Math.Max(1, (float)borderThickness.Horizontal / 2);
            bounds.Height -= Math.Max(1, (float)borderThickness.Vertical / 2);
            if (borderThickness.Left > 1)
                bounds.X += (float)borderThickness.Left / 2;
            if (borderThickness.Top > 1)
                bounds.Y += (float)borderThickness.Top / 2;
            if (borderThickness.Left > 0d && !borderColor.Left.IsEmpty && borderPattern.IsLeftVisible)
            {
                using (Pen pen = new Pen(borderColor.Left, (float)borderThickness.Left))
                {
                    pen.DashStyle = (System.Drawing.Drawing2D.DashStyle)borderPattern.Left;
                    pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                    g.DrawLine(pen, bounds.X, bounds.Y - (float)(borderThickness.Top / 2), bounds.X, bounds.Bottom + (float)(borderThickness.Bottom / 2));
                }
            }
            if (borderThickness.Right > 0d && !borderColor.Right.IsEmpty && borderPattern.IsRightVisible)
            {
                using (Pen pen = new Pen(borderColor.Right, (float)borderThickness.Right))
                {
                    pen.DashStyle = (System.Drawing.Drawing2D.DashStyle)borderPattern.Right;
                    pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                    g.DrawLine(pen, bounds.Right, bounds.Y - (float)(borderThickness.Top / 2), bounds.Right, bounds.Bottom + (float)(borderThickness.Bottom / 2));
                }
            }
            
            if (borderThickness.Top > 0d && !borderColor.Top.IsEmpty && borderPattern.IsTopVisible)
            {
                using (Pen pen = new Pen(borderColor.Top, (float)borderThickness.Top))
                {
                    pen.DashStyle = (System.Drawing.Drawing2D.DashStyle)borderPattern.Top;
                    pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                    g.DrawLine(pen, bounds.X, bounds.Y, bounds.Right, bounds.Y);
                }
            }
            
            if (borderThickness.Bottom > 0d && !borderColor.Bottom.IsEmpty && borderPattern.IsBottomVisible)
            {
                using (Pen pen = new Pen(borderColor.Bottom, (float)borderThickness.Bottom))
                {
                    pen.DashStyle = (System.Drawing.Drawing2D.DashStyle)borderPattern.Bottom;
                    pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
                    g.DrawLine(pen, bounds.X, bounds.Bottom, bounds.Right, bounds.Bottom);
                }
            }
            g.Clip = oldClip;
            if (oldClip != null) oldClip.Dispose();
        }
        public static void PaintStyle(Graphics g, SimpleStyle style, Rectangle bounds)
        {
            if (style.IsPainted && !bounds.IsEmpty)
            {
                if (style.Background.IsBackgroundSet)
                {
                    using (Brush brush = style.Background.CreateBrush(bounds))
                        g.FillRectangle(brush, bounds);
                }
                if (!style.BorderColors.IsEmpty && !style.BorderThickness.IsZero)
                {
                    DrawingHelpers.DrawBorder(g, bounds, style.BorderThickness, style.BorderPattern, style.BorderColors);
                }
            }
        }
    }
}