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

namespace FastReport.Controls
{
#if !DEBUG
    [DesignTimeVisible(false)]
#endif
    internal class ParametersComboBox : UserControl
    {
        private TextBox textBox;
        private Button comboButton;
        private Report report;
        private DataColumnDropDown dropDown;
        private Timer timer;
        private int closedTicks;
        private int dpi;

        public event EventHandler DropDownOpening;
        public new event EventHandler TextChanged;

        /// <inheritdoc/>
        public override string Text
        {
            get { return textBox.Text; }
            set { textBox.Text = value; }
        }


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

        private void FTextBox_TextChanged(object sender, EventArgs e)
        {
            if (TextChanged != null)
                TextChanged(this, EventArgs.Empty);
        }

        private void FDropDown_ColumnSelected(object sender, EventArgs e)
        {
            Text = String.IsNullOrEmpty(dropDown.Column) ? "" : dropDown.Column;
            timer.Start();
        }

        private void FDropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            closedTicks = Environment.TickCount;
        }

        private void FComboButton_Click(object sender, EventArgs e)
        {
            if (Math.Abs(Environment.TickCount - closedTicks) < 100)
                return;

            DropDownOpening?.Invoke(this, EventArgs.Empty);

            dropDown.Column = Text;
            dropDown.SetSize(Width, 250);
            dropDown.Owner = this;
            dropDown.Show(this, new Point(RightToLeft == RightToLeft.Yes ? Width : 0, Height));
        }

        private void FTimer_Tick(object sender, EventArgs e)
        {
            FindForm().BringToFront();
            textBox.Focus();
            textBox.Select(Text.Length, 0);
            timer.Stop();
        }

        private void LayoutControls()
        {
            if (comboButton != null)
                comboButton.SetBounds(RightToLeft == RightToLeft.Yes ? 1 : Width - Height + 1, 1, Height - 2, Height - 2);
            if (textBox != null)
                textBox.SetBounds(RightToLeft == RightToLeft.Yes ? Height : 3, (Height - textBox.PreferredHeight) / 2 - 1, Width - Height - 3, textBox.PreferredHeight);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (dpi != this.Dpi())
            {
                UpdateImages();
                dpi = this.Dpi();
            }
            
            Graphics g = e.Graphics;
            g.FillRectangle(Enabled ? SystemBrushes.Window : SystemBrushes.Control, DisplayRectangle);
            if (Enabled)
                ControlPaint.DrawVisualStyleBorder(g, new Rectangle(0, 0, Width - 1, Height - 1));
            else
                g.DrawRectangle(SystemPens.InactiveBorder, new Rectangle(0, 0, Width - 1, Height - 1));
        }

        private void UpdateImages()
        {
            comboButton.Image = this.GetImage(182);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (timer != null)
                    timer.Dispose();
                timer = null;
            }
            base.Dispose(disposing);
        }

        /// <inheritdoc/>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            LayoutControls();
        }

        /// <inheritdoc/>
        protected override void OnLayout(LayoutEventArgs e)
        {
            base.OnLayout(e);
            LayoutControls();
        }

        public ParametersComboBox()
        {
            comboButton = new Button();
            comboButton.FlatStyle = FlatStyle.Flat;
            comboButton.FlatAppearance.BorderSize = 0;
            comboButton.Click += new EventHandler(FComboButton_Click);
            Controls.Add(comboButton);

            textBox = new TextBox();
            textBox.BorderStyle = BorderStyle.None;
            textBox.TextChanged += new EventHandler(FTextBox_TextChanged);
            Controls.Add(textBox);

            dropDown = new DataColumnDropDown();
            dropDown.DataTree.ShowColumns = false;
            dropDown.DataTree.ShowDataSources = false;
            dropDown.DataTree.ShowRelations = false;
            dropDown.DataTree.ShowParameters = true;
            dropDown.ColumnSelected += new EventHandler(FDropDown_ColumnSelected);
            dropDown.Closed += new ToolStripDropDownClosedEventHandler(FDropDown_Closed);

            timer = new Timer();
            timer.Interval = 50;
            timer.Tick += new EventHandler(FTimer_Tick);

            // prevent autoscale of child controls
            AutoScaleMode = AutoScaleMode.None;
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
        }
    }
}
