using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar.SuperGrid;
namespace DevComponents.SuperGrid.Design
{
    public class SuperGridActionList : DesignerActionList,
        ITypeDescriptorContext, IWindowsFormsEditorService, IServiceProvider
    {
        #region Private variables
        private SuperGridControl _SuperGrid;
        private PropertyDescriptor _PropertyDescriptor;
        private IComponentChangeService _ChangeService;
        private IDesignerHost dh;
        #endregion
        /// 
        /// SuperGridActionList
        /// 
        /// Associated SuperGridControl
        public SuperGridActionList(SuperGridControl superGrid)
            : base(superGrid)
        {
            _SuperGrid = superGrid;
            _ChangeService = Component.Site.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
        }
        #region Public properties
        /// 
        /// Gets or sets the GridPanel DataSource
        /// 
        [AttributeProvider(typeof(IListSource))]
        public object DataSource
        {
            get { return (_SuperGrid.PrimaryGrid.DataSource); }
            set { SetValue("DataSource", value); }
        }
        /// 
        /// Gets or sets whether rows can be deleted
        /// 
        public bool AllowRowDelete
        {
            get { return (_SuperGrid.PrimaryGrid.AllowRowDelete); }
            set { SetValue("AllowRowDelete", value); }
        }
        /// 
        /// Gets or sets whether rows can be Inserted
        /// 
        public bool AllowRowInsert
        {
            get { return (_SuperGrid.PrimaryGrid.AllowRowInsert); }
            set { SetValue("AllowRowInsert", value); }
        }
        /// 
        /// Gets or sets whether the Insert Row is displayed at runtime
        /// 
        public bool ShowInsertRow
        {
            get { return (_SuperGrid.PrimaryGrid.ShowInsertRow); }
            set { SetValue("ShowInsertRow", value); }
        }
        /// 
        /// Gets or sets which Grid Lines are displayed
        /// 
        public GridLines GridLines
        {
            get { return (_SuperGrid.PrimaryGrid.GridLines); }
            set { SetValue("GridLines", value); }
        }
        /// 
        /// Gets or sets whether Tree Lines are displayed
        /// 
        public bool ShowTreeLines
        {
            get { return (_SuperGrid.PrimaryGrid.ShowTreeLines); }
            set { SetValue("ShowTreeLines", value); }
        }
        /// 
        /// Gets or sets whether Tree Buttons are displayed
        /// 
        public bool ShowTreeButtons
        {
            get { return (_SuperGrid.PrimaryGrid.ShowTreeButtons); }
            set { SetValue("ShowTreeButtons", value); }
        }
        /// 
        /// Gets or sets whether to use the Alternate Row Style
        /// 
        public bool UseAlternateRowStyle
        {
            get { return (_SuperGrid.PrimaryGrid.UseAlternateRowStyle); }
            set { SetValue("UseAlternateRowStyle", value); }
        }
        /// 
        /// Gets or sets whether to use the Alternate Column Style
        /// 
        public bool UseAlternateColumnStyle
        {
            get { return (_SuperGrid.PrimaryGrid.UseAlternateColumnStyle); }
            set { SetValue("UseAlternateColumnStyle", value); }
        }
        /// 
        /// Gets or sets whether to allow multi selection
        /// 
        public bool MultiSelect
        {
            get { return (_SuperGrid.PrimaryGrid.MultiSelect); }
            set { SetValue("MultiSelect", value); }
        }
        /// 
        /// Gets or sets the selection granularity
        /// 
        public SelectionGranularity SelectionGranularity
        {
            get { return (_SuperGrid.PrimaryGrid.SelectionGranularity); }
            set { SetValue("SelectionGranularity", value); }
        }
        #endregion
        #region SetValue
        private void SetValue(string property, object value)
        {
            _ChangeService.OnComponentChanging(_SuperGrid.PrimaryGrid, null);
            GetPrimaryGridPropertyByName(property).SetValue(_SuperGrid.PrimaryGrid, value);
            _ChangeService.OnComponentChanged(_SuperGrid.PrimaryGrid, null, null, null);
        }
        #endregion
        #region GetPrimaryGridPropertyByName
        /// 
        /// Gets the PrimaryGrid property via the given name
        /// 
        /// Property name
        /// PropertyDescriptor
        private PropertyDescriptor GetPrimaryGridPropertyByName(string propName)
        {
            PropertyDescriptor prop =
                TypeDescriptor.GetProperties(_SuperGrid.PrimaryGrid)[propName];
            if (prop == null)
                throw new ArgumentException("Matching property not found.", propName);
            return (prop);
        }
        #endregion
        #region GetSortedActionItems
        /// 
        /// Gets the SortedActionItems
        /// 
        /// DesignerActionItemCollection
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            items.Add(new DesignerActionPropertyItem("DataSource", "Data Source",
                "Data", "Sets the GridPanel Data Source."));
            items.Add(new DesignerActionPropertyItem("AllowRowDelete", "Allow Row Delete",
                "User Interaction", "Determines whether rows can be deleted."));
            items.Add(new DesignerActionPropertyItem("AllowRowInsert", "Allow Row Insert",
                "User Interaction", "Determines whether rows can be inserted."));
            items.Add(new DesignerActionPropertyItem("ShowInsertRow", "Show Insert Row",
                "User Interaction", "Determines whether the InsertRow is shown at runtime."));
            items.Add(new DesignerActionPropertyItem("GridLines", "Grid Lines",
                "Appearance", "Determines which Grid Lines are displayed."));
            items.Add(new DesignerActionPropertyItem("ShowTreeLines", "Show Tree Lines",
                "Appearance", "Determines whether Tree Lines are displayed."));
            items.Add(new DesignerActionPropertyItem("ShowTreeButtons", "Show Tree Buttons",
                "Appearance", "Determines whether Tree Buttons are displayed."));
            items.Add(new DesignerActionPropertyItem("UseAlternateColumnStyle", "Use Alternate Column Style",
                "Style", "Determines whether to use the defined Alternate Column Style."));
            items.Add(new DesignerActionPropertyItem("UseAlternateRowStyle", "Use Alternate Row Style",
                "Style", "Determines whether to use the defined Alternate Row Style."));
            items.Add(new DesignerActionPropertyItem("SelectionGranularity", "Selection Granularity",
                "Behavior", "Determines user selection granularity."));
            items.Add(new DesignerActionPropertyItem("MultiSelect", "Enable Multi Selection",
                "Behavior", "Determines whether Multi Selection is enabled."));
            if (DataSource != null)
            {
                items.Add(new DesignerActionMethodItem(this, "GenBoundColumns", "Generate Bound Columns",
                    "Data1", "Clears all previous bound column definitions and\r\ngenerates new column definitions from current DataSource."));
            }
            items.Add(new DesignerActionMethodItem(this, "EditColumns", "Edit Columns...", "Data2"));
            items.Add(new DesignerActionMethodItem(this, "EditRows", "Edit Rows...", "Data2"));
            return (items);
        }
        #endregion
        #region EditColumns
        /// 
        /// EditColumns
        /// 
        private void EditColumns()
        {
            _PropertyDescriptor = TypeDescriptor.GetProperties(_SuperGrid.PrimaryGrid)["Columns"];
            UITypeEditor editor = (UITypeEditor)_PropertyDescriptor.GetEditor(typeof(UITypeEditor));
            if (editor != null)
                editor.EditValue(this, this, _SuperGrid.PrimaryGrid.Columns);
        }
        #endregion
        #region EditRows
        /// 
        /// EditRows
        /// 
        private void EditRows()
        {
            _PropertyDescriptor = TypeDescriptor.GetProperties(_SuperGrid.PrimaryGrid)["Rows"];
            UITypeEditor editor = (UITypeEditor)_PropertyDescriptor.GetEditor(typeof(UITypeEditor));
            if (editor != null)
                editor.EditValue(this, this, _SuperGrid.PrimaryGrid.Rows);
        }
        #endregion
        #region GenBoundColumns
        /// 
        /// GenBoundColumns
        /// 
        private void GenBoundColumns()
        {
            dh =  (IDesignerHost)GetService(typeof(IDesignerHost));
            if (dh != null)
            {
                DesignerTransaction dt = dh.CreateTransaction();
                try
                {
                    IComponentChangeService change =
                        GetService(typeof(IComponentChangeService)) as IComponentChangeService;
                    if (change != null)
                    {
                        change.OnComponentChanging(Component, null);
                        GridColumnCollection colc = _SuperGrid.PrimaryGrid.Columns;
                        _SuperGrid.PrimaryGrid.GenerateColumns(CreateGridColumn);
                        GridColumnCollection cc = _SuperGrid.PrimaryGrid.Columns;
                        for (int i = cc.Count - 1; i >= 0; i--)
                        {
                            GridColumn col = cc[i];
                            if (string.IsNullOrEmpty(col.DataPropertyName) == false && col.IsDataBound == false)
                                cc.RemoveAt(i);
                        }
                        change.OnComponentChanged(Component, null, null, null);
                    }
                }
                catch
                {
                    dt.Cancel();
                }
                finally
                {
                    if (dt.Canceled == false)
                        dt.Commit();
                }
            }
        }
        #region CreateGridColumn
        private GridColumn CreateGridColumn()
        {
            if (dh != null)
                return (dh.CreateComponent(typeof(GridColumn)) as GridColumn);
            return (null);
        }
        #endregion
        #endregion
        #region ITypeDescriptorContext Members
        public IContainer Container
        {
            get { return (Component.Site.Container); }
        }
        public object Instance
        {
            get { return (Component); }
        }
        public void OnComponentChanged()
        {
            object value = _SuperGrid.PrimaryGrid.Columns;
            _ChangeService.OnComponentChanged(Component, _PropertyDescriptor, value, value);
        }
        public bool OnComponentChanging()
        {
            if (Component is SuperGridControl)
                _ChangeService.OnComponentChanging(((SuperGridControl)Component).PrimaryGrid, _PropertyDescriptor);
            else
                _ChangeService.OnComponentChanging(Component, _PropertyDescriptor);
            return true;
        }
        public PropertyDescriptor PropertyDescriptor
        {
            get { return (_PropertyDescriptor); }
        }
        #endregion
        #region IWindowsFormsEditorService Members
        public void CloseDropDown()
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public void DropDownControl(Control control)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public DialogResult ShowDialog(Form dialog)
        {
            return (dialog.ShowDialog());
        }
        #endregion
        #region IServiceProvider
        object IServiceProvider.GetService(Type serviceType)
        {
            if (serviceType.Equals(typeof(IWindowsFormsEditorService)))
                return (this);
            return GetService(serviceType);
        }
        #endregion
    }
}