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

namespace FastReport.Controls
{
    internal class LineWidthButtonItem : ButtonItem
    {
        private float lineWidth;
        private ItemContainer container;
        public event EventHandler WidthSelected;
        bool isOpen = false;

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

        private void AddSubItem(float width, string text)
        {
            ButtonItem item = new ButtonItem();
            Control owner = ContainerControl as Control;
            if (owner == null)
                return;

            Bitmap bmp = new Bitmap(owner.LogicalToDevice(64), owner.LogicalToDevice(14));
            using (Graphics g = Graphics.FromImage(bmp))
            {
                using (Pen pen = new Pen(Color.Black, owner.LogicalToDevice(width)))
                {
                    g.DrawLine(pen, owner.LogicalToDevice(4), owner.LogicalToDevice(7),
                        owner.LogicalToDevice(60), owner.LogicalToDevice(7));
                }
            }

            item.Image = bmp;
            item.Text = text;
            item.ButtonStyle = eButtonStyle.ImageAndText;
            item.Tag = width;
            item.Checked = (float)width == lineWidth;
            item.Click += new EventHandler(item_Click);
            container.SubItems.Add(item);
        }

        private void item_Click(object sender, EventArgs e)
        {
            lineWidth = (float)(sender as ButtonItem).Tag;
            (sender as ButtonItem).Checked = true;
            if (WidthSelected != null)
                WidthSelected(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(0.25f, "0.25");
            AddSubItem(0.5f, "0.5");
            AddSubItem(1, "1");
            AddSubItem(1.5f, "1.5");
            AddSubItem(2, "2");
            AddSubItem(3, "3");
            AddSubItem(4, "4");
            AddSubItem(5, "5");
        }

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

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