using System.ComponentModel;
using System.Drawing.Design;
namespace DevComponents.DotNetBar.Charts.Style
{
    /// 
    /// Represents the visual style of a Bar Series
    /// 
    [ToolboxItem(false), DesignTimeVisible(false)]
    [TypeConverter(typeof(VisualStylesConverter))]
    public class ChartBarVisualStyle : BaseVisualStyle
    {
        #region Private variables
        private ChartLineVisualStyle _Border;
        private ChartLineVisualStyle _AlternateBorder;
        private Background _Background;
        private Background _AlternateBackground;
        #endregion
        #region Public properties
        #region AlternateBackground
        /// 
        /// Gets or sets the background for the bar, when it
        /// extends to the alternate side of the bar origin.
        /// 
        [Description("Indicates the background for the bar, when it extends to the alternate side of the bar origin.")]
        public Background AlternateBackground
        {
            get
            {
                if (_AlternateBackground == null)
                {
                    _AlternateBackground = Background.Empty;
                    UpdateChangeHandler(null, _AlternateBackground);
                }
                return (_AlternateBackground);
            }
            set
            {
                if (_AlternateBackground != value)
                {
                    UpdateChangeHandler(_AlternateBackground, value);
                    _AlternateBackground = value;
                    OnPropertyChangedEx("AlternateBackground", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeAlternateBackground()
        {
            return (_AlternateBackground != null && _AlternateBackground.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetAlternateBackground()
        {
            AlternateBackground = null;
        }
        #endregion
        #region AlternateBorder
        /// 
        /// Gets or sets the border style for the bar, when it
        /// extends to the alternate side of the bar origin.
        /// 
        [Category("Style")]
        [Description("Indicates the border style for the bar, when it extends to the alternate side of the bar origin.")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ChartLineVisualStyle AlternateBorder
        {
            get
            {
                if (_AlternateBorder == null)
                {
                    _AlternateBorder = new ChartLineVisualStyle();
                    UpdateChangeHandler(null, _AlternateBorder);
                }
                return (_AlternateBorder);
            }
            set
            {
                if (_AlternateBorder != value)
                {
                    ChartLineVisualStyle oldValue = _AlternateBorder;
                    _AlternateBorder = value;
                    OnStyleChanged("AlternateBorder", oldValue, value);
                    if (oldValue != null)
                        oldValue.Dispose();
                }
            }
        }
        #endregion
        #region Background
        /// 
        /// Gets or sets the background for the bar.
        /// 
        [Description("Indicates the background for the bar.")]
        public Background Background
        {
            get
            {
                if (_Background == null)
                {
                    _Background = Background.Empty;
                    UpdateChangeHandler(null, _Background);
                }
                return (_Background);
            }
            set
            {
                if (_Background != value)
                {
                    UpdateChangeHandler(_Background, value);
                    _Background = value;
                    OnPropertyChangedEx("Background", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeBackground()
        {
            return (_Background != null && _Background.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetBackground()
        {
            Background = null;
        }
        #endregion
        #region Border
        /// 
        /// Gets or sets the border style for the bar.
        /// 
        [Category("Style")]
        [Description("Indicates the border style for the bar.")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ChartLineVisualStyle Border
        {
            get
            {
                if (_Border == null)
                {
                    _Border = new ChartLineVisualStyle();
                    UpdateChangeHandler(null, _Border);
                }
                return (_Border);
            }
            set
            {
                if (_Border != value)
                {
                    ChartLineVisualStyle oldValue = _Border;
                    _Border = value;
                    OnStyleChanged("Border", oldValue, value);
                    if (oldValue != null)
                        oldValue.Dispose();
                }
            }
        }
        #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 ((_AlternateBackground == null || _AlternateBackground.IsEmpty == true) &&
                    (_AlternateBorder == null || _AlternateBorder.IsEmpty) &&
                    (_Background == null || _Background.IsEmpty) &&
                    (_Border == null || _Border.IsEmpty) &&
                    (base.IsEmpty == true));
            }
        }
        #endregion
        #endregion
        #region ApplyStyle
        /// 
        /// Applies the style to instance of this style.
        /// 
        /// Style to apply.
        public void ApplyStyle(ChartBarVisualStyle style)
        {
            if (style != null)
            {
                base.ApplyStyle(style);
                if (style._AlternateBackground != null && style._AlternateBackground.IsEmpty == false)
                    AlternateBackground = style._AlternateBackground.Copy();
                if (style._AlternateBorder != null && style._AlternateBorder.IsEmpty == false)
                    AlternateBorder.ApplyStyle(style._AlternateBorder);
                if (style._Background != null && style._Background.IsEmpty == false)
                    Background = style._Background.Copy();
                if (style._Border != null && style._Border.IsEmpty == false)
                    Border.ApplyStyle(style._Border);
            }
        }
        #endregion
        #region Copy
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public new ChartBarVisualStyle Copy()
        {
            ChartBarVisualStyle style = new ChartBarVisualStyle();
            CopyTo(style);
            return (style);
        }
        #endregion
        #region CopyTo
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public void CopyTo(ChartBarVisualStyle style)
        {
            base.CopyTo(style);
            style.AlternateBackground = (_AlternateBackground != null) ? _AlternateBackground.Copy() : null;
            style.AlternateBorder = (_AlternateBorder != null) ? _AlternateBorder.Copy() : null;
            style.Background = (_Background != null) ? _Background.Copy() : null;
            style.Border = (_Border != null) ? _Border.Copy() : null;
        }
        #endregion
        #region GetSerialData
        internal override SerialElementCollection GetSerialData(string serialName)
        {
            SerialElementCollection sec = new SerialElementCollection();
            if (serialName != null)
            {
                if (serialName.Equals("") == true)
                    serialName = "ChartBarVisualStyle";
                sec.AddStartElement(serialName);
            }
            if (_AlternateBackground != null && _AlternateBackground.IsEmpty == false)
                sec.AddElement(_AlternateBackground.GetSerialData("AlternateBackground"));
            if (_AlternateBorder != null && _AlternateBorder.IsEmpty == false)
                sec.AddElement(_AlternateBorder.GetSerialData("AlternateBorder"));
            if (_Background != null && _Background.IsEmpty == false)
                sec.AddElement(_Background.GetSerialData("Background"));
            if (_Border != null && _Border.IsEmpty == false)
                sec.AddElement(_Border.GetSerialData("Border"));
            sec.AddElement(base.GetSerialData(null));
            if (serialName != null)
                sec.AddEndElement(serialName);
            return (sec);
        }
        #endregion
        #region PutSerialData
        #region ProcessCollection
        internal override void ProcessCollection(SerialElement se)
        {
            SerialElementCollection sec = se.Sec;
            switch (se.Name)
            {
                case "AlternateBackground":
                    sec.PutSerialData(AlternateBackground);
                    break;
                case "AlternateBorder":
                    sec.PutSerialData(AlternateBorder);
                    break;
                case "Background":
                    sec.PutSerialData(Background);
                    break;
                case "Border":
                    sec.PutSerialData(Border);
                    break;
                default:
                    base.ProcessCollection(se);
                    break;
            }
        }
        #endregion
        #endregion
        #region IDisposable
        /// 
        /// Dispose
        /// 
        public override void Dispose()
        {
            AlternateBackground = null;
            Background = null;
            Border = null;
            base.Dispose();
        }
        #endregion
    }
}