using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using FastReport.Utils;

namespace FastReport.Controls
{
#if !DEBUG
    [DesignTimeVisible(false)]
#endif
    internal class LineStyleControl : Control
    {
        private LineStyle[] styles;
        private LineStyle style;
        private float lineWidth;
        private Color lineColor;
        private bool showBorder;

        public event EventHandler StyleSelected;

        public LineStyle Style
        {
            get { return style; }
            set
            {
                style = value;
                Refresh();
            }
        }

        public float LineWidth
        {
            get { return lineWidth; }
            set
            {
                lineWidth = value;
                Refresh();
            }
        }

        public Color LineColor
        {
            get { return lineColor; }
            set
            {
                lineColor = value;
                Refresh();
            }
        }

        public bool ShowBorder
        {
            get { return showBorder; }
            set
            {
                showBorder = value;
                Refresh();
            }
        }

        private void DrawHighlight(Graphics g, Rectangle rect)
        {
            using (Brush brush = new SolidBrush(Color.FromArgb(193, 210, 238)))
                g.FillRectangle(brush, rect);
            using (Pen pen = new Pen(Color.FromArgb(49, 106, 197)))
                g.DrawRectangle(pen, rect);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // draw control border
            if (showBorder)
            {
                using (Pen p = new Pen(Color.FromArgb(127, 157, 185)))
                    g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
            }
            // draw items
            for (int i = 0; i < styles.Length; i++)
            {
                // highlight active style
                if (this.styles[i] == style)
                    DrawHighlight(g, new Rectangle(
                        this.LogicalToDevice(4), 
                        i * this.LogicalToDevice(15) + this.LogicalToDevice(4), 
                        Width - this.LogicalToDevice(9), 
                        this.LogicalToDevice(15)));
                using (Pen p = new Pen(lineColor, this.LogicalToDevice(lineWidth < 1.5f ? 1.5f : lineWidth)))
                {
                    DashStyle[] dashStyles = new DashStyle[] {
                    DashStyle.Solid, DashStyle.Dash, DashStyle.Dot, DashStyle.DashDot, DashStyle.DashDotDot, DashStyle.Solid };
                    p.DashStyle = dashStyles[(int)this.styles[i]];
                    if (this.styles[i] == LineStyle.Double)
                    {
                        p.Width *= 2.5f;
                        p.CompoundArray = new float[] { 0, 0.4f, 0.6f, 1 };
                    }
                    g.DrawLine(p, this.LogicalToDevice(8), i * this.LogicalToDevice(15) + this.LogicalToDevice(12), Width - this.LogicalToDevice(8), i * this.LogicalToDevice(15) + this.LogicalToDevice(12));
                };
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            int i = (e.Y - this.LogicalToDevice(4)) / this.LogicalToDevice(15);
            if (i < 0)
                i = 0;
            if (i > styles.Length - 1)
                i = styles.Length - 1;

            Style = styles[i];
            if (StyleSelected != null)
                StyleSelected(this, EventArgs.Empty);
        }

        public LineStyleControl()
        {
            styles = new LineStyle[] {
                LineStyle.Solid, LineStyle.Dash, LineStyle.Dot, LineStyle.DashDot, LineStyle.DashDotDot, LineStyle.Double };
            lineColor = Color.Black;
            lineWidth = 1;
            showBorder = true;
            BackColor = SystemColors.Window;
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
            Size = new Size(70, 100);
        }
    }
}
