using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar.SuperGrid;
using System;
using System.Collections;
namespace DevComponents.SuperGrid.Design
{
    /// 
    /// SuperGridDesigner
    /// 
    public class SuperGridDesigner : ControlDesigner
    {
        #region Private variables
        private SuperGridControl _SuperGrid;
        private DesignerActionListCollection _ActionLists;
        #endregion
        #region Initialize
        /// 
        /// Initializes our designer
        /// 
        /// 
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            if (component.Site.DesignMode == true)
                _SuperGrid = component as SuperGridControl;
				
#if !TRIAL
            IDesignerHost dh = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
            if (dh != null)
                dh.LoadComplete += new EventHandler(DesignerLoadComplete);
#endif
            // If our component is removed we need to clean-up
            IComponentChangeService cc = (IComponentChangeService)GetService(typeof(IComponentChangeService));
            if (cc != null)
                cc.ComponentRemoving += new ComponentEventHandler(OnComponentRemoving);
        }
        #endregion
        #region Dispose
        protected override void Dispose(bool disposing)
        {
            // If our component is removed we need to clean-up
            IComponentChangeService cc = (IComponentChangeService)GetService(typeof(IComponentChangeService));
            if (cc != null)
                cc.ComponentRemoving -= new ComponentEventHandler(OnComponentRemoving);
        }
        #endregion
        #region Verbs
        /// 
        /// Creates our verb collection
        /// 
        public override DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerb[] verbs = new DesignerVerb[]
                {
                    //new DesignerVerb("KnobStyle 1", SetStyle1),
                    //new DesignerVerb("KnobStyle 2", SetStyle2),
                    //new DesignerVerb("KnobStyle 3", SetStyle3),
                    //new DesignerVerb("KnobStyle 4", SetStyle4),
                };
                return (new DesignerVerbCollection(verbs));
            }
        }
        #endregion
        #region ActionLists
        /// 
        /// Gets our DesignerActionListCollection list
        /// 
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (_ActionLists == null)
                {
                    _ActionLists = new DesignerActionListCollection();
                    _ActionLists.Add(new SuperGridActionList(_SuperGrid));
                }
                return (_ActionLists);
            }
        }
        #endregion
        #region OnSetCursor
        protected override void OnSetCursor()
        {
            if (InScrollBar(Control.MousePosition) == true)
                Cursor.Current = Cursors.Default;
            base.OnSetCursor();
        }
        #region InScrollBar
        private bool InScrollBar(Point point)
        {
            if (_SuperGrid != null)
            {
                Point pt = _SuperGrid.PointToClient(point);
                if (_SuperGrid.HScrollBar.Visible)
                {
                    if (_SuperGrid.HScrollBar.Bounds.Contains(pt))
                        return (true);
                }
                if (_SuperGrid.VScrollBar.Visible)
                {
                    if (_SuperGrid.VScrollBar.Bounds.Contains(pt))
                        return (true);
                }
            }
            return (false);
        }
        #endregion
        #endregion
        #region OnComponentRemoving
        /// Called when component is about to be removed from designer.
        /// Event sender.
        /// Event arguments.
        public void OnComponentRemoving(object sender, ComponentEventArgs e)
        {
            if (e.Component == this.Component)
            {
                IDesignerHost dh = (IDesignerHost)GetService(typeof(IDesignerHost));
                if (dh == null)
                    return;
                ArrayList list = new ArrayList(AssociatedComponents);
                foreach (IComponent c in list)
                    dh.DestroyComponent(c);
            }
        }
        #endregion
        #region AssociatedComponents
        /// 
        /// Returns all components associated with this control
        /// 
        public override ICollection AssociatedComponents
        {
            get
            {
                ArrayList c = new ArrayList(base.AssociatedComponents);
                SuperGridControl sg = Control as SuperGridControl;
                if (sg != null)
                    GetComponents(sg.PrimaryGrid, c);
                return (c);
            }
        }
        #region GetComponents
        private void GetComponents(GridContainer cont, ArrayList c)
        {
            if (cont is GridPanel)
            {
                foreach (GridColumn col in ((GridPanel)cont).Columns)
                    c.Add(col);
            }
            if (cont.Rows != null && cont.Rows.Count > 0)
            {
                foreach (GridElement item in cont.Rows)
                {
                    c.Add(item);
                    if (item is GridRow)
                    {
                        foreach (GridCell cell in ((GridRow)item).Cells)
                            c.Add(cell);
                    }
                    if (item is GridContainer)
                        GetComponents((GridContainer)item, c);
                }
            }
        }
        #endregion
        #endregion
        #region GetHitTest
        protected override bool GetHitTest(Point point)
        {
            if (InScrollBar(point) == true)
                return (true);
            return (base.GetHitTest(point));
        }
        #endregion
        #region Licensing Stuff
        public override void InitializeNewComponent(IDictionary defaultValues)
        {
            base.InitializeNewComponent(defaultValues);
            SetDesignTimeDefaults();
        }
        private void SetDesignTimeDefaults()
        {
            SuperGridControl c = this.Control as SuperGridControl;
#if !TRIAL
            string key = GetLicenseKey();
            c.LicenseKey = key;
#endif
        }
#if !TRIAL
        internal static string GetLicenseKey()
        {
            string key = "";
            Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.LocalMachine;
            regkey = regkey.OpenSubKey("Software\\DevComponents\\Licenses", false);
            if (regkey != null)
            {
                object keyValue = regkey.GetValue("DevComponents.DotNetBar.DotNetBarManager2");
                if (keyValue != null)
                    key = keyValue.ToString();
            }
            return key;
        }
        private void DesignerLoadComplete(object sender, EventArgs e)
        {
            IDesignerHost dh = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
            if (dh != null)
                dh.LoadComplete -= new EventHandler(DesignerLoadComplete);
            string key = GetLicenseKey();
            SuperGridControl grid = this.Control as SuperGridControl;
            if (key != "" && grid != null && grid.LicenseKey == "" && grid.LicenseKey != key)
                TypeDescriptor.GetProperties(grid)["LicenseKey"].SetValue(grid, key);
        }
#endif
        #endregion
    }
}