using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using FastReport.DevComponents.DotNetBar;
using FastReport.Utils;

namespace FastReport.Controls
{
    internal class LineStyleButtonItem : ButtonItem
    {
        private LineStyle lineStyle;
        private ItemContainer container;
        public event EventHandler StyleSelected;
        bool isOpen = false;

        public LineStyle LineStyle
        {
            get { return lineStyle; }
            set
            {
                lineStyle = value;
                foreach (BaseItem item in container.SubItems)
                {
                    (item as ButtonItem).Checked = (LineStyle)(item as ButtonItem).Tag == value;
                }
            }
        }

        private void AddSubItem(LineStyle style)
        {
            ButtonItem item = new ButtonItem();
            Control owner = ContainerControl as Control;
            if (owner == null)
                return;

            Bitmap bmp = new Bitmap(owner.LogicalToDevice(88), owner.LogicalToDevice(14));
            using (Graphics g = Graphics.FromImage(bmp))
            {
                using (Pen pen = new Pen(Color.Black, owner.LogicalToDevice(2)))
                {
                    DashStyle[] styles = new DashStyle[] {
                        DashStyle.Solid, DashStyle.Dash, DashStyle.Dot, DashStyle.DashDot, DashStyle.DashDotDot, DashStyle.Solid };
                    pen.DashStyle = styles[(int)style];
                    if (style == LineStyle.Double)
                    {
                        pen.Width = owner.LogicalToDevice(5);
                        pen.CompoundArray = new float[] { 0, 0.4f, 0.6f, 1 };
                    }
                    g.DrawLine(pen, owner.LogicalToDevice(4), owner.LogicalToDevice(7),
                        owner.LogicalToDevice(84), owner.LogicalToDevice(7));
                }
            }

            item.Image = bmp;
            item.Tag = style;
            item.Checked = style == lineStyle;
            item.Click += item_Click;
            container.SubItems.Add(item);
        }

        private void item_Click(object sender, EventArgs e)
        {
            lineStyle = (LineStyle)(sender as ButtonItem).Tag;
            (sender as ButtonItem).Checked = true;
            if (StyleSelected != null)
                StyleSelected(this, EventArgs.Empty);
        }

        protected override void OnPopupClose(EventArgs e)
        {
            base.OnPopupClose(e);
            isOpen = false;
        }


        public override void InternalMouseDown(MouseEventArgs objArg)
        {
            isOpen = !isOpen;
            container.SubItems.Clear();
            UpdateItems();
            Expanded = isOpen;
        }

        private void UpdateItems()
        {
            AddSubItem(LineStyle.Solid);
            AddSubItem(LineStyle.Dash);
            AddSubItem(LineStyle.Dot);
            AddSubItem(LineStyle.DashDot);
            AddSubItem(LineStyle.DashDotDot);
            AddSubItem(LineStyle.Double);
        }

        public override void UpdateDpiDependencies()
        {
            base.UpdateDpiDependencies();
            container.SubItems.Clear();
            UpdateItems();
        }

        public LineStyleButtonItem()
        {
            container = new ItemContainer();
            container.LayoutOrientation = eOrientation.Vertical;
            SubItems.Add(container);
            PopupType = ePopupType.ToolBar;
            UpdateItems();
        }
    }
}
