using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Design;
using System.ComponentModel;
using FastReport.Utils;

namespace FastReport.Controls
{
    /// <summary>
    /// Represents a drop-down control that allows to choose a color.
    /// </summary>
    /// <remarks>
    /// This control may be useful if you write own components for FastReport.
    /// </remarks>
    [ToolboxItem(false)]
    public class ColorDropDown : ToolStripDropDown
    {
        private ToolStripControlHost host;
        private ColorSelector colorSelector;

        /// <summary>
        /// This event is raised when you select a color.
        /// </summary>
        public event EventHandler ColorSelected;

        /// <summary>
        /// Gets or sets the control that owns this dropdown.
        /// </summary>
        /// <remarks>Set the Owner to onscreen control that initiates dropdown. This control will 
        /// be used to calculate dpi-dependent sizes.</remarks>
        public Control Owner { get; set; }

        /// <summary>
        /// Gets or sets the selected color.
        /// </summary>
        public Color Color
        {
            get { return colorSelector.Color; }
            set { colorSelector.Color = value; }
        }

        private void FColorSelector_ColorSelected(object sender, EventArgs e)
        {
            if(!(sender is TrackBar))
                Close();

            if (ColorSelected != null)
                ColorSelected(this, EventArgs.Empty);
        }

        /// <summary>
        /// Sets the UI style.
        /// </summary>
        /// <param name="style">The style to set.</param>
        public void SetStyle(UIStyle style)
        {
            colorSelector.SetStyle(style);
        }

        /// <inheritdoc/>
        protected override void OnOpening(CancelEventArgs e)
        {
            base.OnOpening(e);
            colorSelector.UpdateDpiDependencies(Owner);
            colorSelector.Localize();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ColorDropDown"/> class with default settings.
        /// </summary>
        public ColorDropDown()
        {
            Font = DrawUtils.DefaultFont;
            colorSelector = new ColorSelector();
            colorSelector.ColorSelected += FColorSelector_ColorSelected;
            host = new ToolStripControlHost(colorSelector);
            Items.Add(host);
        }
    }
}
