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

namespace FastReport.Controls
{
    internal class ToolStripColorButton : ToolStripSplitButton
    {
        private Bitmap bitmap;
        private ColorDropDown dropDown;
        private Color defaultColor;

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Color Color
        {
            get { return dropDown.Color; }
            set { dropDown.Color = value; }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Color DefaultColor
        {
            get { return defaultColor; }
            set
            {
                defaultColor = value;
                Graphics g = Graphics.FromImage(bitmap);
                Color color = value == Color.Transparent ? Color.White : value;
                Brush b = new SolidBrush(color);
                g.FillRectangle(b, 1, 13, 15, 3);
                b.Dispose();
                g.Dispose();
                Invalidate();
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new int ImageIndex
        {
            get { return -1; }
            set
            {
                bitmap = Res.GetImage(value, 96);
                Image = bitmap;
            }
        }

        private void DropDown_Opening(object sender, CancelEventArgs e)
        {
            dropDown.Owner = Owner;
        }

        private void FDropDown_ColorSelected(object sender, EventArgs e)
        {
            Color = dropDown.Color;
            DefaultColor = Color;
            OnButtonClick(EventArgs.Empty);
        }

        public void SetStyle(UIStyle style)
        {
            dropDown.SetStyle(style);
        }

        public ToolStripColorButton()
        {
            dropDown = new ColorDropDown();
            dropDown.Opening += DropDown_Opening;
            dropDown.ColorSelected += FDropDown_ColorSelected;
            DropDown = dropDown;
        }

        public ToolStripColorButton(int imageIndex, Color defaultColor) : this()
        {
            ImageIndex = imageIndex;
            DefaultColor = defaultColor;
            DisplayStyle = ToolStripItemDisplayStyle.Image;
        }

    }
}
