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

namespace DockableUI
{
    public partial class DockingForm : Form
    {
        //internal static DockingForm DraggingForm;
        //private bool resizing = false;

        #region Properties

        //private DockedFormsContainer myContainer;
        private DockContainer myParent;
        public new DockContainer Parent
        {
            get { return myParent; }
            set
            {
                if (value != myParent)
                {
                    if (this.Dock != DockStyle.None)
                    {
                        throw new ArgumentException("The parent cannot be changed  when the form is docked.");
                    }
                    this.myParent = value;
                }
            }
        }

        internal Control BaseParent
        {
            get { return base.Parent; }
            set { base.Parent = value; }
        }

        internal DockStyle BaseDock
        {
            get { return base.Dock; }
            set { base.Dock = value; }
        }

        [DefaultValue(false)]
        public new bool ShowInTaskbar
        {
            get { return base.ShowInTaskbar; }
            set { base.ShowInTaskbar = value; }
        }

        [DefaultValue(typeof(FormBorderStyle), "None"), ReadOnly(true)]
        protected new FormBorderStyle FormBorderStyle
        {
            get { return base.FormBorderStyle; }
        }

        private FormBorderStyle undockedFormBorderStyle = FormBorderStyle.SizableToolWindow;
        [DefaultValue(typeof(FormBorderStyle), "SizableToolWindow")]
        public FormBorderStyle UndockedFormBorderStyle
        {
            get { return undockedFormBorderStyle; }
            set
            {
                undockedFormBorderStyle = value;
            }
        }

        private DockStyle dock = DockStyle.None;
        public new DockStyle Dock
        {
            get { return dock; }
            set
            {
                if (value != dock)
                {
                    //this.Parent.Resize += new EventHandler(Parent_Resize);
                    //base.Dock = value;
                    switch (value)
                    {
                        case DockStyle.Bottom:
                            dock = value;
                            base.TopLevel = false;
                            //base.Parent = myParent.ContainerBottom;
                            base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                            myParent.ContainerBottom.AddTab(this);
                            //base.Dock = DockStyle.Fill;
                            //myContainer.Parent = this.Parent;
                            //this.Dock = value;
                            break;
                        case DockStyle.Left:
                            dock = value;
                            base.TopLevel = false;
                            //base.Parent = myParent.ContainerLeft;
                            base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                            myParent.ContainerLeft.AddTab(this);
                            //base.Dock = DockStyle.Fill;
                            break;
                        case DockStyle.Right:
                            dock = value;
                            base.TopLevel = false;
                            //base.Parent = myParent.ContainerRight;
                            base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                            myParent.ContainerRight.AddTab(this);
                            //base.Dock = DockStyle.Fill;
                            //myContainer.Parent = this.Parent;
                            //myContainer.Dock = value;
                            break;
                        case DockStyle.Fill:
                            dock = value;
                            base.TopLevel = false;
                            //base.Parent = myParent.ContainerFill;
                            base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                            myParent.ContainerFill.AddTab(this);
                            //base.Dock = DockStyle.Fill;
                            //this.MaximumSize = new Size(this.Parent.ClientSize.Width, this.Parent.ClientSize.Height);
                            //this.MinimumSize = new Size(this.Parent.ClientSize.Width, this.Parent.ClientSize.Height);
                            break;
                        //case DockStyle.Top:
                        //    base.TopLevel = false;
                        //    base.Parent = myParent;
                        //    base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                        //    base.Dock = DockStyle.Fill;
                        //    //myContainer.Parent = this.Parent;
                        //    myContainer.Dock = value;
                        //    break;
                        case DockStyle.None:
                            base.Visible = false;
                            dock = value;
                            //base.TopLevel = true;
                            //base.FormBorderStyle = undockedFormBorderStyle;
                            //base.ShowInTaskbar = false;

                            //this.Show();
                            //this.MaximumSize = new Size(0, 0);
                            //this.MinimumSize = new Size(0, 0);
                            break;
                    }
                }
            }
        }

        #endregion

        #region ParentEvents Handler

        //void Parent_Resize(object sender, EventArgs e)
        //{
        //    switch (this.Dock)
        //    {
        //        case DockStyle.Bottom:
        //        case DockStyle.Top:
        //            this.MaximumSize = new Size(this.Parent.ClientSize.Width, Parent.ClientSize.Height);
        //            this.MinimumSize = new Size(this.Parent.ClientSize.Width, 24);
        //            break;
        //        case DockStyle.Left:
        //        case DockStyle.Right:
        //            this.MaximumSize = new Size(this.Parent.ClientSize.Width, this.Parent.ClientSize.Height);
        //            this.MinimumSize = new Size(24, this.Parent.ClientSize.Height);
        //            break;
        //        case DockStyle.Fill:
        //            this.MaximumSize = new Size(this.Parent.ClientSize.Width, this.Parent.ClientSize.Height);
        //            this.MinimumSize = new Size(this.Parent.ClientSize.Width, this.Parent.ClientSize.Height);
        //            break;
        //        case DockStyle.None:
        //            this.MaximumSize = new Size(0, 0);
        //            this.MinimumSize = new Size(0, 0);
        //            break;
        //    }
        //}

        #endregion

        #region Events

        #endregion

        public DockingForm()
        {
            InitializeComponent();
            this.ShowInTaskbar = false;
            base.FormBorderStyle = UndockedFormBorderStyle;
            //myContainer = new DockedFormsContainer();
        }

        private void fillToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DockedFormsContainer control = base.Parent as DockedFormsContainer;
            if (control != null)
            {
                control.RemoveTab(this);
            }
            this.Dock = DockStyle.Fill;
        }

        private void leftToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DockedFormsContainer control= base.Parent as DockedFormsContainer;
            if (control !=null)
            {
                control.RemoveTab(this);
            }
            this.Dock = DockStyle.Left;
        }

        private void rightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DockedFormsContainer control = base.Parent as DockedFormsContainer;
            if (control != null)
            {
                control.RemoveTab(this);
            }
            this.Dock = DockStyle.Right;
        }

        private void bottomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DockedFormsContainer control = base.Parent as DockedFormsContainer;
            if (control != null)
            {
                control.RemoveTab(this);
            }
            this.Dock = DockStyle.Bottom;
        }
    }
}
