using System;
using System.ComponentModel;
using System.Drawing;
namespace DevComponents.DotNetBar.Charts.Style
{
    ///
    /// ChartLegendVisualStyles
    ///
    [TypeConverter(typeof(VisualStylesConverter))]
    public class ChartLegendItemVisualStyles : VisualStyles
    {
        #region Copy
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public ChartLegendItemVisualStyles Copy()
        {
            ChartLegendItemVisualStyles styles = new ChartLegendItemVisualStyles();
            for (int i = 0; i < Styles.Length; i++)
            {
                ChartLegendItemVisualStyle vstyle = Styles[i];
                if (vstyle != null)
                    styles.Styles[i] = vstyle.Copy();
            }
            return (styles);
        }
        #endregion
    }
    /// 
    /// Represents the visual style of a Legend item element.
    /// 
    [TypeConverter(typeof(VisualStylesConverter))]
    public class ChartLegendItemVisualStyle : BaseVisualStyle
    {
        #region Private variables
        private Background _Background;
        private Color _TextColor = Color.Empty;
        private Font _Font;
        private Tbool _AllowWrap = Tbool.NotSet;
        private Alignment _Alignment = Alignment.NotSet;
        private int _MaxLineCount;
        private Background _CheckBoxBackground;
        private Color _CheckBoxBorderColor = Color.Empty;
        private Color _CheckBoxCheckColor = Color.Empty;
        private PointMarkerVisualStyle _MarkerVisualStyle;
        private Background _DisabledMarkerBackground;
        private Color _DisabledTextColor = Color.Empty;
        #endregion
        #region Public properties
        #region Alignment
        /// 
        /// Gets or sets the alignment of the text
        /// 
        [DefaultValue(Alignment.NotSet), Category("Appearance")]
        [Description("Indicates the alignment of the text.")]
        public Alignment Alignment
        {
            get { return (_Alignment); }
            set
            {
                if (_Alignment != value)
                {
                    _Alignment = value;
                    OnPropertyChangedEx("Alignment", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region AllowWrap
        /// 
        /// Gets or sets whether text wrapping is permitted
        /// 
        [DefaultValue(Tbool.NotSet), Category("Appearance")]
        [Description("Indicates whether text wrapping is permitted.")]
        public Tbool AllowWrap
        {
            get { return (_AllowWrap); }
            set
            {
                if (_AllowWrap != value)
                {
                    _AllowWrap = value;
                    OnPropertyChangedEx("AllowWrap", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region Background
        /// 
        /// Gets or sets the style background.
        /// 
        [Description("Indicates the style background")]
        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 CheckBoxBackground
        /// 
        /// Gets or sets the CheckBox background.
        /// 
        [Description("Indicates the CheckBox background")]
        public Background CheckBoxBackground
        {
            get
            {
                if (_CheckBoxBackground == null)
                {
                    _CheckBoxBackground = Background.Empty;
                    UpdateChangeHandler(null, _CheckBoxBackground);
                }
                return (_CheckBoxBackground);
            }
            set
            {
                if (_CheckBoxBackground != value)
                {
                    UpdateChangeHandler(_CheckBoxBackground, value);
                    _CheckBoxBackground = value;
                    OnPropertyChangedEx("CheckBoxBackground", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeCheckBoxBackground()
        {
            return (_CheckBoxBackground != null && _CheckBoxBackground.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetCheckBoxBackground()
        {
            CheckBoxBackground = null;
        }
        #endregion
        #region CheckBoxBorderColor
        /// 
        /// Gets or sets the CheckBox border color.
        /// 
        [Description("Indicates the CheckBox border color")]
        public Color CheckBoxBorderColor
        {
            get { return (_CheckBoxBorderColor); }
            set
            {
                if (_CheckBoxBorderColor != value)
                {
                    _CheckBoxBorderColor = value;
                    OnPropertyChangedEx("CheckBoxBorderColor", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeCheckBoxBorderColor()
        {
            return (_CheckBoxBorderColor.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetCheckBoxBorderColor()
        {
            CheckBoxBorderColor = Color.Empty;
        }
        #endregion
        #region CheckBoxCheckColor
        /// 
        /// Gets or sets the CheckBox check color.
        /// 
        [Description("Indicates the CheckBox check color")]
        public Color CheckBoxCheckColor
        {
            get { return (_CheckBoxCheckColor); }
            set
            {
                if (_CheckBoxCheckColor != value)
                {
                    _CheckBoxCheckColor = value;
                    OnPropertyChangedEx("CheckBoxCheckColor", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeCheckBoxCheckColor()
        {
            return (_CheckBoxCheckColor.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetCheckBoxCheckColor()
        {
            CheckBoxCheckColor = Color.Empty;
        }
        #endregion
        #region DisabledMarkerBackground
        /// 
        /// Gets or sets the Disabled Marker Background style.
        /// 
        [Description("Indicates the Disabled Marker Background style")]
        public Background DisabledMarkerBackground
        {
            get
            {
                if (_DisabledMarkerBackground == null)
                {
                    _DisabledMarkerBackground = Background.Empty;
                    UpdateChangeHandler(null, _DisabledMarkerBackground);
                }
                return (_DisabledMarkerBackground);
            }
            set
            {
                if (_DisabledMarkerBackground != value)
                {
                    UpdateChangeHandler(_DisabledMarkerBackground, value);
                    _DisabledMarkerBackground = value;
                    OnPropertyChangedEx("DisabledMarkerBackground", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeDisabledMarkerBackground()
        {
            return (_DisabledMarkerBackground != null && _DisabledMarkerBackground.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetDisabledMarkerBackground()
        {
            DisabledMarkerBackground = null;
        }
        #endregion
        #region DisabledTextColor
        /// 
        /// Gets or sets the Disabled TextColor
        /// 
        [Category("Appearance")]
        [Description("Indicates whether the items in the Legend are equally spaced.")]
        public Color DisabledTextColor
        {
            get { return (_DisabledTextColor); }
            set
            {
                if (value != _DisabledTextColor)
                {
                    _DisabledTextColor = value;
                    OnPropertyChangedEx("DisabledTextColor", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeDisabledTextColor()
        {
            return (_DisabledTextColor.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetDisabledTextColor()
        {
            DisabledTextColor = Color.Empty;
        }
        #endregion
        #region Font
        /// 
        /// Gets or sets the style Font
        /// 
        [DefaultValue(null)]
        [Description("Indicates the style Font")]
        public Font Font
        {
            get { return (_Font); }
            set
            {
                if (_Font != value)
                {
                    _Font = value;
                    OnPropertyChangedEx("Font", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region MarkerVisualStyle
        /// 
        /// Gets or sets the visual styles for the Legend Marker.
        /// 
        [Category("Style")]
        [Description("Indicates the visual styles for the PointMarker.")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public PointMarkerVisualStyle MarkerVisualStyle
        {
            get
            {
                if (_MarkerVisualStyle == null)
                {
                    _MarkerVisualStyle = new PointMarkerVisualStyle();
                    UpdateChangeHandler(null, _MarkerVisualStyle);
                }
                return (_MarkerVisualStyle);
            }
            set
            {
                if (_MarkerVisualStyle != value)
                {
                    PointMarkerVisualStyle oldValue = _MarkerVisualStyle;
                    _MarkerVisualStyle = value;
                    OnStyleChanged("MarkerVisualStyle", oldValue, value);
                    if (oldValue != null)
                        oldValue.Dispose();
                }
            }
        }
        #endregion
        #region MaxLineCount
        /// 
        /// Gets or sets the maximum number of lines for the item text.
        /// 
        [Category("Appearance")]
        [Description("Indicates the maximum number of lines for the item text.")]
        [DefaultValue(0)]
        public int MaxLineCount
        {
            get { return (_MaxLineCount); }
            set
            {
                if (value != _MaxLineCount)
                {
                    _MaxLineCount = value;
                    OnPropertyChangedEx("MaxLineCount", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region TextColor
        /// 
        /// Gets or sets the Text color.
        /// 
        [Description("Indicates the Text color")]
        public Color TextColor
        {
            get { return (_TextColor); }
            set
            {
                if (_TextColor != value)
                {
                    _TextColor = value;
                    OnPropertyChangedEx("TextColor", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeTextColor()
        {
            return (_TextColor.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetTextColor()
        {
            TextColor = Color.Empty;
        }
        #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 ((_Alignment == Alignment.NotSet) && 
                    (_AllowWrap == Tbool.NotSet) &&
                    (_Background == null || _Background.IsEmpty == true) &&
                    (_CheckBoxBackground == null || _CheckBoxBackground.IsEmpty == true) &&
                    (_CheckBoxBorderColor.IsEmpty == true) &&
                    (_CheckBoxCheckColor.IsEmpty == true) &&
                    (_DisabledMarkerBackground == null || _DisabledMarkerBackground.IsEmpty == true) &&
                    (_DisabledTextColor.IsEmpty == true) &&
                    (_Font == null) &&
                    (_MarkerVisualStyle == null || _MarkerVisualStyle.IsEmpty) &&
                    (_MaxLineCount == 0) &&
                    (_TextColor.IsEmpty == true) &&
                    (base.IsEmpty == true));
            }
        }
        #endregion
        #endregion
        #region GetStringFormatFlags
        internal void GetStringFormatFlags(StringFormat sf)
        {
            if (_AllowWrap == Tbool.False || _MaxLineCount <= 1)
                sf.FormatFlags |= StringFormatFlags.NoWrap;
            sf.Trimming = StringTrimming.EllipsisCharacter;
            switch (Alignment)
            {
                case Alignment.TopCenter:
                    sf.LineAlignment = StringAlignment.Near;
                    sf.Alignment = StringAlignment.Center;
                    break;
                case Alignment.TopRight:
                    sf.LineAlignment = StringAlignment.Near;
                    sf.Alignment = StringAlignment.Far;
                    break;
                case Alignment.NotSet:
                case Alignment.MiddleLeft:
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Near;
                    break;
                case Alignment.MiddleCenter:
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Center;
                    break;
                case Alignment.MiddleRight:
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Far;
                    break;
                case Alignment.BottomLeft:
                    sf.LineAlignment = StringAlignment.Far;
                    sf.Alignment = StringAlignment.Near;
                    break;
                case Alignment.BottomCenter:
                    sf.LineAlignment = StringAlignment.Far;
                    sf.Alignment = StringAlignment.Center;
                    break;
                case Alignment.BottomRight:
                    sf.LineAlignment = StringAlignment.Far;
                    sf.Alignment = StringAlignment.Far;
                    break;
            }
        }
        #endregion
        #region ApplyStyle
        /// 
        /// Applies the style to instance of this style.
        /// 
        /// Style to apply.
        public void ApplyStyle(ChartLegendItemVisualStyle style)
        {
            if (style != null)
            {
                base.ApplyStyle(style);
                if (style.Alignment != Alignment.NotSet)
                    Alignment = style.Alignment;
                if (style.AllowWrap != Tbool.NotSet)
                    AllowWrap = style.AllowWrap;
                if (style._Background != null && style._Background.IsEmpty == false)
                    Background = style._Background.Copy();
                if (style._CheckBoxBackground != null && style._CheckBoxBackground.IsEmpty == false)
                    CheckBoxBackground = style._CheckBoxBackground.Copy();
                if (style._CheckBoxBorderColor.IsEmpty == false)
                    CheckBoxBorderColor = style._CheckBoxBorderColor;
                if (style._CheckBoxCheckColor.IsEmpty == false)
                    CheckBoxCheckColor = style._CheckBoxCheckColor;
                if (style._DisabledMarkerBackground != null && style._DisabledMarkerBackground.IsEmpty == false)
                    DisabledMarkerBackground = style.DisabledMarkerBackground.Copy();
                if (style.DisabledTextColor.IsEmpty == false)
                    DisabledTextColor = style.DisabledTextColor;
                if (style.Font != null)
                    Font = style.Font;
                if (style._MarkerVisualStyle != null && style.MarkerVisualStyle.IsEmpty == false)
                    MarkerVisualStyle.ApplyStyle(style.MarkerVisualStyle);
                if (style.MaxLineCount != 0)
                    MaxLineCount = style.MaxLineCount;
                if (style._TextColor.IsEmpty == false)
                    TextColor = style._TextColor;
            }
        }
        #endregion
        #region Copy
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public new ChartLegendItemVisualStyle Copy()
        {
            ChartLegendItemVisualStyle style = new ChartLegendItemVisualStyle();
            CopyTo(style);
            return (style);
        }
        #endregion
        #region CopyTo
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public void CopyTo(ChartLegendItemVisualStyle style)
        {
            base.CopyTo(style);
            style.Alignment = _Alignment;
            style.AllowWrap = _AllowWrap;
            style.Background = (_Background != null) ? _Background.Copy() : null;
            style.CheckBoxBackground = (_CheckBoxBackground != null) ? _CheckBoxBackground.Copy() : null;
            style.CheckBoxBorderColor = _CheckBoxBorderColor;
            style.CheckBoxCheckColor = _CheckBoxCheckColor;
            style.DisabledMarkerBackground = (_DisabledMarkerBackground != null ? _DisabledMarkerBackground.Copy() : null);
            style.DisabledTextColor = _DisabledTextColor;
            style.Font = (_Font != null) ? (Font)_Font.Clone() : null;
            style.MarkerVisualStyle = (_MarkerVisualStyle != null) ? _MarkerVisualStyle.Copy() : null;
            style.MaxLineCount = _MaxLineCount;
            style.TextColor = _TextColor;
        }
        #endregion
        #region GetSerialData
        internal override SerialElementCollection GetSerialData(string serialName)
        {
            SerialElementCollection sec = new SerialElementCollection();
            if (serialName != null)
            {
                if (serialName.Equals("") == true)
                    serialName = "ChartLegendItemVisualStyle";
                sec.AddStartElement(serialName);
            }
            sec.AddValue("Alignment", _Alignment, Alignment.NotSet);
            sec.AddValue("AllowWrap", _AllowWrap, Tbool.NotSet);
            if (_Background != null && _Background.IsEmpty == false)
                sec.AddElement(_Background.GetSerialData("Background"));
            if (_CheckBoxBackground != null && _CheckBoxBackground.IsEmpty == false)
                sec.AddElement(_CheckBoxBackground.GetSerialData("CheckBoxBackground"));
            sec.AddValue("CheckBoxBorderColor", _CheckBoxBorderColor, Color.Empty);
            sec.AddValue("CheckBoxCheckColor", _CheckBoxCheckColor, Color.Empty);
 
            if (_Font != null)
                sec.AddValue("Font", XmlSerializableFont.ConvertToString(Font));
            if (_MarkerVisualStyle != null && _MarkerVisualStyle.IsEmpty == false)
                sec.AddElement(_MarkerVisualStyle.GetSerialData("MarkerVisualStyle"));
            sec.AddValue("MaxLineCount", _MaxLineCount, 0);
            sec.AddValue("TextColor", _TextColor, Color.Empty);
            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)
            {
                case "Alignment":
                    Alignment = (Alignment)se.GetValueEnum(typeof(Alignment));
                    break;
                case "AllowWrap":
                    AllowWrap = (Tbool)se.GetValueEnum(typeof(Tbool));
                    break;
                case "CheckBoxBorderColor":
                    CheckBoxBorderColor = se.GetValueColor();
                    break;
                case "CheckBoxCheckColor":
                    CheckBoxCheckColor = se.GetValueColor();
                    break;
                case "Font":
                    Font = XmlSerializableFont.ConvertFromString(se.StringValue);
                    break;
                case "MaxLineCount":
                    MaxLineCount = int.Parse(se.StringValue);
                    break;
                case "TextColor":
                    TextColor = se.GetValueColor();
                    break;
                default:
                    base.ProcessValue(se);
                    break;
            }
        }
        #endregion
        #region ProcessCollection
        internal override void ProcessCollection(SerialElement se)
        {
            SerialElementCollection sec = se.Sec;
            switch (se.Name)
            {
                case "Background":
                    sec.PutSerialData(Background);
                    break;
                case "CheckBoxBackground":
                    sec.PutSerialData(CheckBoxBackground);
                    break;
                case "MarkerVisualStyle":
                    sec.PutSerialData(MarkerVisualStyle);
                    break;
                default:
                    base.ProcessCollection(se);
                    break;
            }
        }
        #endregion
        #endregion
        #region IDisposable
        public override void Dispose()
        {
            Background = null;
            CheckBoxBackground = null;
            MarkerVisualStyle = null;
            base.Dispose();
        }
        #endregion
    }
}