using System;
using System.ComponentModel;
using System.Drawing;
namespace DevComponents.DotNetBar.Charts.Style
{
    ///
    /// PieChartVisualStyles
    ///
    [TypeConverter(typeof(VisualStylesConverter))]
    public class PieChartVisualStyles : VisualStyles
    {
        #region Copy
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public PieChartVisualStyles Copy()
        {
            PieChartVisualStyles styles = new PieChartVisualStyles();
            for (int i = 0; i < Styles.Length; i++)
            {
                PieChartVisualStyle vstyle = Styles[i];
                if (vstyle != null)
                    styles.Styles[i] = vstyle.Copy();
            }
            return (styles);
        }
        #endregion
    }
    /// 
    /// Represents the visual style of a Pie Chart element.
    /// 
    [TypeConverter(typeof(BlankExpandableObjectConverter))]
    public class PieChartVisualStyle : ContainerVisualStyle
    {
        #region Private variables
        private Background _AlternateGridBackground;
        private PieGridLineVisualStyle _GridLineVisualStyle;
        private PieReferenceLineVisualStyle _ReferenceLineVisualStyle;
        private PieCenterVisualStyles _PieCenterVisualStyles;
        #endregion
        #region Public properties
        #region AlternateGridBackground
        /// 
        /// Gets or sets the alternate, or interlaced, pie background (used
        /// when ShowPieGrid is enabled).
        /// 
        [Description("Indicates the alternate, or interlaced, pie background (used when ShowPieGrid is enabled)")]
        public Background AlternateGridBackground
        {
            get
            {
                if (_AlternateGridBackground == null)
                {
                    _AlternateGridBackground = Background.Empty;
                    UpdateChangeHandler(null, _AlternateGridBackground);
                }
                return (_AlternateGridBackground);
            }
            set
            {
                if (_AlternateGridBackground != value)
                {
                    UpdateChangeHandler(_AlternateGridBackground, value);
                    _AlternateGridBackground = value;
                    OnPropertyChangedEx("AlternateGridBackground", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeAlternateGridBackground()
        {
            return (_AlternateGridBackground != null && _AlternateGridBackground.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetAlternateGridBackground()
        {
            AlternateGridBackground = null;
        }
        #endregion
        #region GridLineVisualStyle
        /// 
        /// Gets or sets the visual style to be used for GridLine elements.
        /// 
        [Category("Style")]
        [Description("Indicates visual style to be used for GridLine elements.")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PieGridLineVisualStyle GridLineVisualStyle
        {
            get
            {
                if (_GridLineVisualStyle == null)
                {
                    _GridLineVisualStyle = new PieGridLineVisualStyle();
                    UpdateChangeHandler(null, _GridLineVisualStyle);
                }
                return (_GridLineVisualStyle);
            }
            set
            {
                if (_GridLineVisualStyle != value)
                {
                    PieGridLineVisualStyle oldValue = _GridLineVisualStyle;
                    _GridLineVisualStyle = value;
                    OnStyleChanged("GridLinesVisualStyle", oldValue, value);
                }
            }
        }
        #endregion
        #region PieCenterVisualStyles
        /// 
        /// Gets or sets the visual styles to be used for Pie Center elements.
        /// 
        [Category("Style")]
        [Description("Indicates visual styles to be used for Pie Center elements.")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PieCenterVisualStyles PieCenterVisualStyles
        {
            get
            {
                if (_PieCenterVisualStyles == null)
                {
                    _PieCenterVisualStyles = new PieCenterVisualStyles();
                    UpdateChangeHandler(null, _PieCenterVisualStyles);
                    UpdatePropertyHandler(null, _PieCenterVisualStyles);
                }
                return (_PieCenterVisualStyles);
            }
            set
            {
                if (value != _PieCenterVisualStyles)
                {
                    PieCenterVisualStyles oldValue = _PieCenterVisualStyles;
                    _PieCenterVisualStyles = value;
                    OnStyleChanged("PieCenterVisualStyles", oldValue, value);
                    UpdatePropertyHandler(oldValue, value);
                }
            }
        }
        #region UpdatePropertyHandler
        protected void UpdatePropertyHandler(
            PieCenterVisualStyles oldValue, PieCenterVisualStyles newValue)
        {
            if (oldValue != null)
            {
                for (int i = 0; i < oldValue.Count; i++)
                {
                    BaseVisualStyle style = oldValue[(StyleType)i];
                    style.PropertyChanged -= style_PropertyChanged;
                }
            }
            if (newValue != null)
            {
                for (int i = 0; i < newValue.Count; i++)
                {
                    BaseVisualStyle style = newValue[(StyleType)i];
                    style.PropertyChanged += style_PropertyChanged;
                }
            }
        }
        #region style_PropertyChanged
        void style_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            BaseVisualStyle style = sender as BaseVisualStyle;
            if (style != null)
            {
                style.StyleUpdateCount++;
                BaseVisualStyle parent = style.Parent;
                while (parent != null)
                {
                    parent.StyleUpdateCount++;
                    parent = parent.Parent;
                }
            }
        }
        #endregion
        #endregion
        #endregion
        #region ReferenceLineVisualStyle
        /// 
        /// Gets or sets the visual style to be used for ReferenceLine elements 
        /// 
        [Category("Style")]
        [Description("Indicates visual style to be used for ReferenceLine elements ")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PieReferenceLineVisualStyle ReferenceLineVisualStyle
        {
            get
            {
                if (_ReferenceLineVisualStyle == null)
                {
                    _ReferenceLineVisualStyle = new PieReferenceLineVisualStyle();
                    UpdateChangeHandler(null, _ReferenceLineVisualStyle);
                }
                return (_ReferenceLineVisualStyle);
            }
            set
            {
                if (_ReferenceLineVisualStyle != value)
                {
                    PieReferenceLineVisualStyle oldValue = _ReferenceLineVisualStyle;
                    _ReferenceLineVisualStyle = value;
                    OnStyleChanged("ReferenceLineVisualStyle", oldValue, value);
                }
            }
        }
        #endregion
        #region IsEmpty
        /// 
        /// Gets whether the style is logically Empty.
        /// 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Description("Gets whether the style is logically Empty.")]
        public override bool IsEmpty
        {
            get
            {
                return ((_AlternateGridBackground == null || _AlternateGridBackground.IsEmpty) &&
                    (_GridLineVisualStyle == null || _GridLineVisualStyle.IsEmpty) &&
                    (_PieCenterVisualStyles == null || _PieCenterVisualStyles.IsEmpty) &&
                    (_ReferenceLineVisualStyle == null || _ReferenceLineVisualStyle.IsEmpty) &&
                    (base.IsEmpty == true));
            }
        }
        #endregion
        #endregion
        #region ApplyStyle
        /// 
        /// Applies the style to instance of this style.
        /// 
        /// Style to apply.
        public void ApplyStyle(PieChartVisualStyle style)
        {
            base.ApplyStyle(style);
            if (style != null)
            {
                if (style._AlternateGridBackground != null && style.AlternateGridBackground.IsEmpty == false)
                    AlternateGridBackground = style.AlternateGridBackground.Copy();
                if (style._GridLineVisualStyle != null && style._GridLineVisualStyle.IsEmpty == false)
                    GridLineVisualStyle.ApplyStyle(style.GridLineVisualStyle);
                if (style._PieCenterVisualStyles != null)
                {
                    for (int i = 0; i < style._PieCenterVisualStyles.Styles.Length; i++)
                    {
                        if (style._PieCenterVisualStyles.Styles[i] != null)
                            PieCenterVisualStyles.GetStyle(i).ApplyStyle(style._PieCenterVisualStyles.Styles[i]);
                    }
                }
                if (style._ReferenceLineVisualStyle != null && style._ReferenceLineVisualStyle.IsEmpty == false)
                    ReferenceLineVisualStyle.ApplyStyle(style._ReferenceLineVisualStyle);
            }
        }
        #endregion
        #region ApplyDefaults
        public override void ApplyDefaults()
        {
            if (Alignment == Alignment.NotSet)
                Alignment = Alignment.MiddleCenter;
            if (_AlternateGridBackground == null)
                AlternateGridBackground = new Background(Color.AliceBlue);
            GridLineVisualStyle.ApplyDefaults();
            if (_GridLineVisualStyle.LineColor.IsEmpty == true)
                GridLineVisualStyle.LineColor = Color.LightGray;
            ReferenceLineVisualStyle.ApplyDefaults();
            base.ApplyDefaults();
        }
        #endregion
        #region Copy
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public new PieChartVisualStyle Copy()
        {
            PieChartVisualStyle style = new PieChartVisualStyle();
            CopyTo(style);
            return (style);
        }
        #endregion
        #region CopyTo
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public void CopyTo(PieChartVisualStyle style)
        {
            base.CopyTo(style);
            style.AlternateGridBackground = (_AlternateGridBackground != null) ? _AlternateGridBackground.Copy() : null;
            style.GridLineVisualStyle = (_GridLineVisualStyle != null) ? _GridLineVisualStyle.Copy() : null;
            style.ReferenceLineVisualStyle = (_ReferenceLineVisualStyle != null) ? _ReferenceLineVisualStyle.Copy() : null;
            style.PieCenterVisualStyles = (_PieCenterVisualStyles != null) ? _PieCenterVisualStyles.Copy() : null;
        }
        #endregion
        #region GetSerialData
        internal override SerialElementCollection GetSerialData(string serialName)
        {
            SerialElementCollection sec = new SerialElementCollection();
            if (serialName != null)
            {
                if (serialName.Equals("") == true)
                    serialName = "PieChartVisualStyle";
                sec.AddStartElement(serialName);
            }
            if (_AlternateGridBackground != null && _AlternateGridBackground.IsEmpty == false)
                sec.AddElement(_AlternateGridBackground.GetSerialData("AlternateBackground"));
            if (_GridLineVisualStyle != null && _GridLineVisualStyle.IsEmpty == false)
                sec.AddElement(_GridLineVisualStyle.GetSerialData("GridLineVisualStyle"));
            if (_PieCenterVisualStyles != null && _PieCenterVisualStyles.IsEmpty == false)
                sec.AddElement(_PieCenterVisualStyles.GetSerialData("PieCenterVisualStyles"));
            if (_ReferenceLineVisualStyle != null && _ReferenceLineVisualStyle.IsEmpty == false)
                sec.AddElement(_ReferenceLineVisualStyle.GetSerialData("ReferenceLineVisualStyle"));
            sec.AddElement(base.GetSerialData(null));
            if (serialName != null)
                sec.AddEndElement(serialName);
            return (sec);
        }
        #endregion
        #region PutSerialData
        #region ProcessValue
        internal override void ProcessValue(SerialElement se)
        {
            switch (se.Name)
            {
                default:
                    base.ProcessValue(se);
                    break;
            }
        }
        #endregion
        #region ProcessCollection
        internal override void ProcessCollection(SerialElement se)
        {
            SerialElementCollection sec = se.Sec;
            switch (se.Name)
            {
                case "AlternateBackground":
                    sec.PutSerialData(AlternateGridBackground);
                    break;
                case "GridLineVisualStyle":
                    sec.PutSerialData(GridLineVisualStyle);
                    break;
                case "PieCenterVisualStyles":
                    sec.PutSerialData(PieCenterVisualStyles);
                    break;
                case "ReferenceLineVisualStyle":
                    sec.PutSerialData(ReferenceLineVisualStyle);
                    break;
                default:
                    base.ProcessCollection(se);
                    break;
            }
        }
        #endregion
        #endregion
        #region IDisposable
        /// 
        /// Dispose
        /// 
        public override void Dispose()
        {
            AlternateGridBackground = null;
            GridLineVisualStyle = null;
            PieCenterVisualStyles = null;
            base.Dispose();
        }
        #endregion
    }
}