using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MetaEditor
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MetaEditorPanel : UserControl
    {
        public ControlCollection PanelControls { get { return ribbonClientPanel1.Controls; } }

        /// <summary>
        /// 
        /// </summary>
        public MetaEditorPanel()
        {
            InitializeComponent();
            vScrollBar1.SmallChange = 5;
        }

        private void UpdateScrollBar()
        {
            // No controls || no scrollbar needed
            if (ribbonClientPanel1.Controls.Count == 0 || (ribbonClientPanel1.Height - this.Height) <= 0)
            {
                vScrollBar1.Enabled = false;
                vScrollBar1.Value = 0;
                vScrollBar1.LargeChange = 0;
                vScrollBar1.Maximum = 100;
            }
            else
            {
                vScrollBar1.Enabled = true;
                if (ValueIsMax()) SetValueToNewMax(this.Height, ribbonClientPanel1.Height);
                vScrollBar1.Maximum = ribbonClientPanel1.Height;
                vScrollBar1.LargeChange = this.Height;
            }

            ScrollEventArgs sea = new ScrollEventArgs(ScrollEventType.ThumbPosition, vScrollBar1.Value);
            vScrollBar1_Scroll(null, sea);
        }

        private bool ValueIsMax()
        {
            return (vScrollBar1.Value + vScrollBar1.LargeChange >= vScrollBar1.Maximum);
        }

        private void SetValueToNewMax(int newLargeChange, int newMax)
        {
            vScrollBar1.Value = newMax - newLargeChange;
        }

        /// <summary>
        /// Adds an array of control objects to the collection.
        /// </summary>
        /// <param name="controls">An array of System.Windows.Forms.Control objects to add to the collection. </param>
        public void AddControlRange(Control[] controls)
        {
            ribbonClientPanel1.Controls.AddRange(controls);
            ribbonClientPanel1.Height = ribbonClientPanel1.PreferredSize.Height;
            UpdateScrollBar();
        }

        /// <summary>
        /// Removes all controls from the collection.
        /// </summary>
        public void ClearControls()
        {
            ribbonClientPanel1.Controls.Clear();
            vScrollBar1.Value = 0;
            vScrollBar1.Enabled = false;
        }

        /// <summary>
        /// Gets the width of the control area.
        /// </summary>
        public int GetWidth()
        {
            return ribbonClientPanel1.Width;
        }

        public event EventHandler OnResizeHandler;

        public void MetaEditorPanel_Resize(object sender, EventArgs e)
        {
            ribbonClientPanel1.Width = this.Width - vScrollBar1.Width;
            if (OnResizeHandler != null) OnResizeHandler(ribbonClientPanel1, EventArgs.Empty);

            ribbonClientPanel1.Height = (
                this.Height >= ribbonClientPanel1.PreferredSize.Height ?
                this.Height :
                ribbonClientPanel1.PreferredSize.Height
                );

            UpdateScrollBar();
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            ribbonClientPanel1.Top = -e.NewValue;
        }
    }
}
