using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using DevComponents.DotNetBar.Charts;
namespace DevComponents.Charts.Design
{
    public class ChartControlActionList : DesignerActionList,
        ITypeDescriptorContext, IWindowsFormsEditorService, IServiceProvider
    {
        #region Private variables
        private ChartControl _ChartControl;
        private PropertyDescriptor _PropertyDescriptor;
        private IComponentChangeService _ChangeService;
        #endregion
        /// 
        /// ChartControlActionList
        /// 
        /// Associated ChartControl
        public ChartControlActionList(ChartControl chartControl)
            : base(chartControl)
        {
            _ChartControl = chartControl;
            _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 (_ChartControl.DataSource); }
            set { SetValue("DataSource", value); }
        }
        /// 
        /// Gets or sets the GridPanel DataMember
        /// 
        public string DataMember
        {
            get { return (_ChartControl.DataMember); }
            set { SetValue("DataMember", value); }
        }
        #endregion
        #region GetSortedActionItems
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            items.Add(new DesignerActionHeaderItem("Data"));
            items.Add(new DesignerActionPropertyItem("DataSource",
                                "DataSource", "Data",
                                "Sets the default Chart DataSource."));
            items.Add(new DesignerActionPropertyItem("DataMember",
                                "DataMember", "Data",
                                "Sets the default Chart DataMamber."));
            return items;
        }
        #endregion
        #region SetValue
        private void SetValue(string property, object value)
        {
            _ChangeService.OnComponentChanging(_ChartControl, null);
            GetPropertyByName(property).SetValue(_ChartControl, value);
            _ChangeService.OnComponentChanged(_ChartControl, null, null, null);
        }
        #endregion
        #region GetPropertyByName
        /// 
        /// Gets the property via the given name
        /// 
        /// Property name
        /// PropertyDescriptor
        private PropertyDescriptor GetPropertyByName(string propName)
        {
            PropertyDescriptor prop =
                TypeDescriptor.GetProperties(_ChartControl)[propName];
            if (prop == null)
                throw new ArgumentException("Matching property not found.", propName);
            return (prop);
        }
        #endregion
        #region EditChartContainers
        /// 
        /// EditChartContainers
        /// 
        private void EditChartContainers()
        {
            _PropertyDescriptor = TypeDescriptor.GetProperties(_ChartControl.ChartPanel)["ChartContainers"];
            UITypeEditor editor = (UITypeEditor)_PropertyDescriptor.GetEditor(typeof(UITypeEditor));
            if (editor != null)
                editor.EditValue(this, this, _ChartControl.ChartPanel.ChartContainers);
        }
        #endregion
        #region ITypeDescriptorContext Members
        public IContainer Container
        {
            get { return (Component.Site.Container); }
        }
        public object Instance
        {
            get { return (Component); }
        }
        public void OnComponentChanged()
        {
            object value = _ChartControl.ChartPanel.ChartContainers;
            _ChangeService.OnComponentChanged(Component, _PropertyDescriptor, value, value);
        }
        public bool OnComponentChanging()
        {
            _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
    }
}