using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Forms.Design.Behavior;
using DevComponents.AdvTree;
using DevComponents.AdvTree.Design;
using DevComponents.DotNetBar.Controls;
namespace DevComponents.DotNetBar.Design
{
    public class ToolboxItemDesigner : ComponentDesignerWithAction
    {
        #region Constructor
        /// 
        /// Initializes a new instance of the LayoutItemBaseDesigner class.
        /// 
        public ToolboxItemDesigner()
        {
        }
        #endregion
        #region Implementation
        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
        public override void InitializeNewComponent(IDictionary defaultValues)
        {
            base.InitializeNewComponent(defaultValues);
            ToolboxItem item = (ToolboxItem)this.Component;
            item.Text = item.Name;
            item.Symbol = "\ue251";
            item.SymbolSet = eSymbolSet.Material;
        }
        private DesignerActionListCollection _ActionLists = null;
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (this._ActionLists == null)
                {
                    this._ActionLists = new DesignerActionListCollection();
                    this._ActionLists.Add(new ToolboxItemActionList(this));
                }
                return this._ActionLists;
            }
        }
        public void MoveItemUp()
        {
            IComponentChangeService cc = (IComponentChangeService)GetService(typeof(IComponentChangeService));
            ToolboxItem item = (ToolboxItem)this.Component;
            BaseItem parent = item.Parent;
            if (parent == null) return;
            int index = parent.SubItems.IndexOf(item);
            if (index == 0)
            {
                // Move to previous group
                if (parent.Parent != null && parent.Parent.SubItems.IndexOf(parent) > 0)
                {
                    int j = parent.Parent.SubItems.IndexOf(parent);
                    for (int i = j; i >= 0; i--)
                    {
                        BaseItem previousItem = parent.Parent.SubItems[i];
                        if (previousItem is ToolboxGroup && previousItem.Visible)
                        {
                            cc.OnComponentChanging(parent, TypeDescriptor.GetProperties(parent)["SubItems"]);
                            parent.SubItems.Remove(item);
                            cc.OnComponentChanged(parent, TypeDescriptor.GetProperties(parent)["SubItems"], null, null);
                            cc.OnComponentChanging(parent.Parent, TypeDescriptor.GetProperties(parent.Parent)["SubItems"]);
                            parent.Parent.SubItems.Add(item);
                            cc.OnComponentChanged(parent.Parent, TypeDescriptor.GetProperties(parent)["SubItems"], null, null);
                            break;
                        }
                    }
                }
            }
            else
            {
                cc.OnComponentChanging(parent, TypeDescriptor.GetProperties(parent)["SubItems"]);
                parent.SubItems.Remove(item);
                parent.SubItems.Insert(index - 1, item);
                cc.OnComponentChanged(parent, TypeDescriptor.GetProperties(parent)["SubItems"], null, null);
            }
            this.RecalcLayout();
        }
        public override void RecalcLayout()
        {
            ToolboxItem item = this.Component as ToolboxItem;
            if (item != null && item.GetToolboxControl() != null)
                item.GetToolboxControl().RecalcLayout();
        }
        public void MoveItemDown()
        {
            IComponentChangeService cc = (IComponentChangeService)GetService(typeof(IComponentChangeService));
            ToolboxItem item = (ToolboxItem)this.Component;
            BaseItem parent = item.Parent;
            if (parent == null) return;
            int index = parent.SubItems.IndexOf(item);
            if (index == parent.SubItems.Count - 1)
            {
                // Move to next group
                if (parent.Parent != null && parent.Parent.SubItems.IndexOf(parent) < parent.Parent.SubItems.Count - 1)
                {
                    int j = parent.Parent.SubItems.IndexOf(parent);
                    for (int i = j; i < parent.Parent.SubItems.Count; i++)
                    {
                        BaseItem previousItem = parent.Parent.SubItems[i];
                        if (previousItem is ToolboxGroup && previousItem.Visible)
                        {
                            cc.OnComponentChanging(parent, TypeDescriptor.GetProperties(parent)["SubItems"]);
                            parent.SubItems.Remove(item);
                            cc.OnComponentChanged(parent, TypeDescriptor.GetProperties(parent)["SubItems"], null, null);
                            cc.OnComponentChanging(parent.Parent, TypeDescriptor.GetProperties(parent.Parent)["SubItems"]);
                            parent.Parent.SubItems.Add(item);
                            cc.OnComponentChanged(parent.Parent, TypeDescriptor.GetProperties(parent)["SubItems"], null, null);
                            break;
                        }
                    }
                }
            }
            else
            {
                cc.OnComponentChanging(parent, TypeDescriptor.GetProperties(parent)["SubItems"]);
                parent.SubItems.Remove(item);
                parent.SubItems.Insert(index + 1, item);
                cc.OnComponentChanged(parent, TypeDescriptor.GetProperties(parent)["SubItems"], null, null);
            }
            this.RecalcLayout();
        }
        //private void OnComponentRemoved(object sender, ComponentEventArgs e)
        //{
        //    if (e.Component == this.Component)
        //    {
        //        ToolboxItem item = e.Component as ToolboxItem;
        //        BaseItem parent = item.Parent;
        //        Controls.ToolboxControl lc = item.GetToolboxControl();
        //        if (parent != null)
        //        {
        //            IComponentChangeService cc = (IComponentChangeService)GetService(typeof(IComponentChangeService));
        //            if (cc != null)
        //                cc.OnComponentChanging(parent, TypeDescriptor.GetProperties(parent)["SubItems"]);
        //            parent.SubItems.Remove(item);
        //            if (cc != null)
        //                cc.OnComponentChanged(parent, TypeDescriptor.GetProperties(parent)["SubItems"], null, null);
        //            if (lc != null)
        //            {
        //                lc.Invalidate();
        //                IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
        //                if (host != null)
        //                {
        //                    IDesigner designer = host.GetDesigner(lc);
        //                    if (designer is ToolboxControlDesigner)
        //                    {
        //                        ((ToolboxControlDesigner)designer).SelectionRefresh();
        //                    }
        //                }
        //            }
        //        }
        //    }
        //}
        protected override void OnDesignerSelectionChanged(bool isSelected)
        {
            ToolboxItem item = (ToolboxItem)this.Component;
            item.Checked = isSelected;
            base.OnDesignerSelectionChanged(isSelected);
        }
        public override System.Collections.ICollection AssociatedComponents
        {
            get
            {
                ToolboxItem item = this.Component as ToolboxItem;
                if (item.SubItems.Count == 0)
                    return base.AssociatedComponents;
                ArrayList items = new ArrayList();
                item.SubItems.CopyTo(items);
                return items;
            }
        }
        protected override void PreFilterProperties(IDictionary properties)
        {
            base.PreFilterProperties(properties);
            properties["Visible"] = TypeDescriptor.CreateProperty(typeof(ToolboxItemDesigner),
                (PropertyDescriptor)properties["Visible"], new Attribute[]
                {
                    new DefaultValueAttribute(true),
                    new BrowsableAttribute(true),
                    new CategoryAttribute("Appearance")
                });
        }
        /// 
        /// Gets or sets whether item is visible.
        /// 
        [DefaultValue(true), Browsable(true), Category("Appearance"), Description("Indicates visiblity of the item.")]
        public bool Visible
        {
            get { return (bool)ShadowProperties["Visible"]; }
            set
            {
                // this value is not passed to the actual control
                this.ShadowProperties["Visible"] = value;
            }
        }
        public override Control GetHostControl()
        {
            return GetParentControl().Parent;
        }
        #endregion
    }
    #region ToolboxItemActionList
    internal class ToolboxItemActionList : DesignerActionList
    {
        private ToolboxItemDesigner _Designer = null;
        /// 
        /// Initializes a new instance of the AdvTreeActionList class.
        /// 
        /// 
        public ToolboxItemActionList(ToolboxItemDesigner designer)
            : base(designer.Component)
        {
            _Designer = designer;
        }
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            items.Add(new DesignerActionHeaderItem("Position"));
            //items.Add(new DesignerActionHeaderItem("Columns"));
            //items.Add(new DesignerActionHeaderItem("Selection"));
            items.Add(new DesignerActionMethodItem(this, "MoveItemUp", "Move Up", "Position", true));
            items.Add(new DesignerActionMethodItem(this, "MoveItemDown", "Move Down", "Position", true));
            //items.Add(new DesignerActionPropertyItem("CellEdit", "Allow node text editing?", "Nodes", "Indicates whether node cells are editable"));
            //items.Add(new DesignerActionMethodItem(this, "EditColumns", "Edit Columns...", "Columns", "Edit Tree Control Columns", true));
            //items.Add(new DesignerActionPropertyItem("ColumnsVisible", "Column header visible?", "Columns", "Indicates whether tree column header is visible"));
            //items.Add(new DesignerActionPropertyItem("GridColumnLines", "Show grid column lines?", "Columns", "Indicates whether grid lines are visible"));
            //items.Add(new DesignerActionPropertyItem("GridRowLines", "Show grid row lines?", "Nodes", "Indicates whether grid lines between nodes are visible"));
            //items.Add(new DesignerActionPropertyItem("GridLinesColor", "Grid lines color:", "Columns", "Indicates custom color for grid lines"));
            //items.Add(new DesignerActionPropertyItem("SelectionBox", "Show selection?", "Selection", "Indicates whether selection is shown for selected node"));
            //items.Add(new DesignerActionPropertyItem("SelectionBoxStyle", "Selection style:", "Selection", "Indicates selection style"));
            //items.Add(new DesignerActionPropertyItem("HotTracking", "Highlight mouse over node?", "Selection", "Indicates whether node that mouse is over is highlighted"));
            return items;
        }
        public void MoveItemUp()
        {
            _Designer.MoveItemUp();
        }
        public void MoveItemDown()
        {
            _Designer.MoveItemDown();
        }
    }
    #endregion
}