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

namespace DockableUI.Tabs
{
    class TabStrip : ToolStrip
    {
        private List<TabButton> tabs = new List<TabButton>();
        private Dictionary<DockingForm, TabButton> formTabTable = new Dictionary<DockingForm, TabButton>();
        private TabButton selectedTab;

        public TabButton SelectedTab
        {
            get { return selectedTab; }
        }

        public List<TabButton> Tabs
        {
            get { return tabs; }
        }

        public TabStrip()
        {
            this.Renderer = new TabsRenderer();
        }

        public void RemoveTab(DockingForm control)
        {
            if (!formTabTable.ContainsKey(control))
            {
                throw new KeyNotFoundException("DockingForm hasn't docked");
            }
            RemoveTab(formTabTable[control]);
        }

        public void RemoveTab(TabButton btn)
        {
            btn.AssignedControl.BaseDock = DockStyle.None;
            this.Parent.Controls.Remove(btn.AssignedControl);
            btn.AssignedControl.BaseParent = null;
            btn.AssignedControl.Dock = DockStyle.None;
            tabs.Remove(btn);
            formTabTable.Remove(btn.AssignedControl);
            this.Items.Remove(btn);
            if (selectedTab == btn)
            {
                if (tabs.Count > 0)
                {
                    tabChanging(tabs[0], EventArgs.Empty);
                }
                else
                {
                    selectedTab = null;
                    (this.Parent as DockedFormsContainer).Visible = false;
                }
            }
            btn.Dispose();
        }

        public void AddTab(DockingForm control)
        {
            TabButton btn = new TabButton();
            btn.AssignedControl = control;
            btn.Click += new EventHandler(tabChanging);
            //btn.MouseMove += new MouseEventHandler(tabMouseMove);
            //btn.MouseDragAway += new EventHandler(tabDragAway);
            //btn.MouseUp += new MouseEventHandler(tabMouseUp);
            btn.Text = control.Text;
            btn.ContextMenuStrip = control.DockingMenu;
            btn.Image = control.Icon.ToBitmap();
            tabs.Add(btn);
            formTabTable.Add(control, btn);
            this.Items.Add(btn);
            if (selectedTab == null)
            {
                selectedTab = btn;
                btn.Checked = true;
                btn.AssignedControl.BaseParent = this.Parent;
                btn.AssignedControl.BaseDock = DockStyle.Fill;
                btn.AssignedControl.Show();
            }
            (this.Parent as DockedFormsContainer).Visible = true;
        }

        //void tabMouseUp(object sender, MouseEventArgs e)
        //{
        //    if (draggingover != null)
        //    {
        //        TabButton btn = sender as TabButton;
        //        if (btn != null)
        //        {
        //            DockingForm control=btn.AssignedControl;
        //            if (draggingover == control.Parent.ContainerLeft)
        //            {
        //                RemoveTab(btn);
        //                control.Dock = DockStyle.Left;
        //            }
        //            else if (draggingover == control.Parent.ContainerRight)
        //            {
        //                RemoveTab(btn);
        //                control.Dock = DockStyle.Right;
        //            }
        //            else if (draggingover == control.Parent.ContainerBottom)
        //            {
        //                RemoveTab(btn);
        //                control.Dock = DockStyle.Bottom;
        //            }
        //            else if (draggingover == control.Parent.ContainerFill)
        //            {
        //                RemoveTab(btn);
        //                control.Dock = DockStyle.Fill;
        //            }
        //            //draggingover.AddTab(control);
        //        }
        //    }
        //    Cursor = Cursors.Default;
        //    draggingover = null;
        //}

        //private DockedFormsContainer draggingover;
        //void tabDragAway(object sender, EventArgs e)
        //{
        //    System.Diagnostics.Debug.Print("over");
        //    TabButton btn = sender as TabButton;
        //    if (btn != null)
        //    {
        //        //DockingForm control = btn.AssignedControl;
        //        //RemoveTab(btn);
        //        draggingover = btn.AssignedControl.Parent._getDfcAtCursor();
        //        if (draggingover != null)
        //        {
        //            //c.Highlight = true;
        //            //c.Invalidate();
        //            Cursor = Cursors.UpArrow;
        //        }
        //        else
        //        {
        //            Cursor = Cursors.Default;
        //        }
        //        //control.ShowDialog();
        //    }
        //}

        //void tabMouseMove(object sender, MouseEventArgs e)
        //{
        //    if (e.Button == System.Windows.Forms.MouseButtons.Left)
        //    {
        //        TabButton btn = sender as TabButton;
        //        if (btn != null)
        //        {
        //            DockingForm control = btn.AssignedControl;
        //            RemoveTab(btn);
        //            //control.ShowDialog();
        //        }
        //    }
        //}

        void tabChanging(object sender, EventArgs e)
        {
            TabButton btn = sender as TabButton;
            if (btn != null)
            {
                if (selectedTab != null)
                {
                    selectedTab.Checked = false;
                    selectedTab.AssignedControl.Visible = false;
                }
                selectedTab = btn;
                btn.Checked = true;
                btn.AssignedControl.BaseParent = this.Parent;
                btn.AssignedControl.BaseDock = DockStyle.Fill;
                btn.AssignedControl.Show();
            }
        }
    }
}
