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

namespace FastReport.Controls
{
    /// <summary>
    /// Represents the control that combines a textbox and a button.
    /// </summary>
    [ToolboxItem(false)]
    public class TextBoxButton : UserControl
    {
        private TextBox textBox;
        private Button button;

        /// <summary>
        /// Occurs when the button is clicked.
        /// </summary>
        public event EventHandler ButtonClick;
        
        /// <summary>
        /// Occurs when the text is changed.
        /// </summary>
        public new event EventHandler TextChanged;

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

        /// <summary>
        /// Gets or sets the button's image.
        /// </summary>
        public Image Image
        {
            get { return button.Image; }
            set { button.Image = value; }
        }
        
        /// <summary>
        /// Gets or sets the button's text.
        /// </summary>
        public string ButtonText
        {
            get { return button.Text; }
            set { button.Text = value; }
        }

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

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

        private void LayoutControls()
        {
            if (button != null)
                button.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);
        }

        /// <inheritdoc/>
        protected override void OnPaint(PaintEventArgs e)
        {
            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));
        }

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

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

        /// <summary>
        /// Set focus on text box.
        /// </summary>
        public void FocusTextBox()
        {
            textBox.Focus();
        }

        /// <inheritdoc/>
        /// <summary>
        /// Initializes a new instance of the <see cref="TextBoxButton"/> class.
        /// </summary>
        public TextBoxButton()
        {
            button = new Button();
            button.FlatStyle = FlatStyle.Flat;
            button.FlatAppearance.BorderSize = 0;
            button.Click += new EventHandler(FButton_Click);
            button.TabIndex = 1;
            Controls.Add(button);

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

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