namespace RyzStudio.Windows.ThemedForms
{
    using RyzStudio.Drawing;
    using System;
	using System.ComponentModel;
	using System.Drawing;
    using System.Windows.Forms;

    public partial class TListBox : RyzStudio.Windows.ThemedForms.TUserControl
	{
        protected readonly Padding textboxPadding = new Padding(6, 2, 4, 2);

		public TListBox() : base()
        {
            InitializeComponent();

            this.Font = new Font(this.Font, FontStyle.Regular);
            this.Margin = new Padding(10, 4, 10, 4);

            listBox1.Font = this.Font;
            listBox1.BorderStyle = BorderStyle.None;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            //int b = (styleActive.BorderWidth + 1) + styleActive.BorderPadding;

            //this.Height = comboBox1.Height + (b + textboxPadding.Top) + ((b - 1) + textboxPadding.Bottom);
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);

            listBox1.Focus();
        }

        [Browsable(true), EditorBrowsable(EditorBrowsableState.Advanced)]
        [Category("Appearance")]
        public System.Windows.Forms.ListBox ListBox { get => listBox1; set => listBox1 = value; }

        [Browsable(true), EditorBrowsable(EditorBrowsableState.Advanced)]
        [Category("Appearance")]
        public TButton SubmitButton { get; set; } = null;

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public new Padding Margin { get => base.Margin; set => base.Margin = value; }

        [Category("Action")]
        [Browsable(true)]
        public event EventHandler OnAdd;

        [Category("Action")]
        [Browsable(true)]
        public event EventHandler OnEdit;

        protected override void updateBackground(Graphics g, ThemeStyle style)
        {
            int b = (styleActive.BorderWidth + 1) + styleActive.BorderPadding;

            this.Padding = new Padding((b + textboxPadding.Left), (b + textboxPadding.Top), ((b - 1) + textboxPadding.Right), ((b - 1) + textboxPadding.Bottom));

            Rectangoid area = new Rectangoid(this.ClientRectangle, style.BorderRadius, style.BorderWidth);
            g.FillPath(new SolidBrush(style.BackColour), area.ToGraphicsPath());
            g.DrawPath(new Pen(new SolidBrush(style.BorderColour), style.BorderWidth), area.ToGraphicsPath());
        }

        /// <summary>
        /// Add
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            this.OnAdd?.Invoke(sender, e);
        }

        /// <summary>
        /// Edit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageBox5_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            this.OnEdit?.Invoke(sender, e);
        }

        /// <summary>
        /// Remove
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageBox2_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            if (listBox1.SelectedIndex < 0)
            {
                return;
            }

            int pos = listBox1.SelectedIndex;

            listBox1.Items.RemoveAt(pos);

            if (pos > (listBox1.Items.Count - 1))
            {
                pos = (listBox1.Items.Count - 1);
            }

            listBox1.SelectedIndex = pos;
        }

        /// <summary>
        /// Move up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageBox3_MouseClick(object sender, MouseEventArgs e)
        {
            if (e != null)
            {
                if (e.Button != MouseButtons.Left)
                {
                    return;
                }
            }

            if (listBox1.SelectedIndex <= 0)
            {
                return;
            }

            if (listBox1.SelectedItem == null)
            {
                return;
            }

            object item = listBox1.SelectedItem;
            int pos = listBox1.SelectedIndex;

            listBox1.Items.RemoveAt(pos);
            listBox1.Items.Insert((pos - 1), item);

            listBox1.SelectedIndex = (pos - 1);
        }

        /// <summary>
        /// Move down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageBox4_MouseClick(object sender, MouseEventArgs e)
        {
            if (e != null)
            {
                if (e.Button != MouseButtons.Left)
                {
                    return;
                }
            }

            if (listBox1.SelectedIndex >= (listBox1.Items.Count - 1))
            {
                return;
            }

            if (listBox1.SelectedItem == null)
            {
                return;
            }

            object item = listBox1.SelectedItem;
            int pos = listBox1.SelectedIndex;

            listBox1.Items.RemoveAt(pos);
            listBox1.Items.Insert((pos + 1), item);

            listBox1.SelectedIndex = (pos + 1);
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) => imageBox5_MouseClick(sender, e);

        private void listBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (this.SubmitButton != null)
                    {
                        this.SubmitButton.PerformClick();
                    }

                    break;
                case Keys.Escape:
                    close();
                    break;
                case Keys.Up:
                    if (e.Alt)
                    {
                        imageBox3_MouseClick(sender, null);
                    }

                    break;
                case Keys.Down:
                    if (e.Alt)
                    {
                        imageBox4_MouseClick(sender, null);
                    }

                    break;
                default: break;
            }
        }

    }
}