using System;
using System.ComponentModel;
using System.Drawing;
namespace DevComponents.DotNetBar.Charts.Style
{
    /// 
    /// Represents the visual style of a Tickmark Label element.
    /// 
    [TypeConverter(typeof(VisualStylesConverter))]
    public class TickmarkLabelVisualStyle : BaseVisualStyle
    {
        #region Private variables
        private Font _Font;
        private Tbool _AllowWrap = Tbool.NotSet;
        private Color _TextColor = Color.Empty;
        private Alignment _TextAlignment = Alignment.NotSet;
        private string _TextFormat;
        private int _MaxLineCount = -1;
        private int _MaxWidth = -1;
        #endregion
        #region Public properties
        #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 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 MaxLineCount
        /// 
        /// Gets or sets the maximum number of Label Text lines.
        /// 
        [DefaultValue(-1), Category("Appearance")]
        [Description("Indicates the maximum number of Label Text lines.")]
        public int MaxLineCount
        {
            get { return (_MaxLineCount); }
            set
            {
                if (value != _MaxLineCount)
                {
                    _MaxLineCount = value;
                    OnPropertyChangedEx("MaxLineCount", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region MaxWidth
        /// 
        /// Gets or sets the maximum width of the lable.
        /// 
        [DefaultValue(-1), Category("Appearance")]
        [Description("Indicates the maximum width of the lable.")]
        public int MaxWidth
        {
            get { return (_MaxWidth); }
            set
            {
                if (value != _MaxWidth)
                {
                    _MaxWidth = value;
                    OnPropertyChangedEx("MaxWidth", VisualChangeType.Layout);
                }
            }
        }
        #endregion
        #region TextAlignment
        /// 
        /// Gets or sets the alignment of the axis Text
        /// 
        [DefaultValue(Alignment.NotSet), Category("Appearance")]
        [Description("Indicates the alignment of the axis Text.")]
        public Alignment TextAlignment
        {
            get { return (_TextAlignment); }
            set
            {
                if (_TextAlignment != value)
                {
                    _TextAlignment = value;
                    OnPropertyChangedEx("TextAlignment", 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 TextFormat
        /// 
        /// Gets or sets the Text Format specifier
        /// 
        [DefaultValue(null)]
        [Description("Indicates the Text Format specifier")]
        public string TextFormat
        {
            get { return (_TextFormat); }
            set
            {
                if (_TextFormat != value)
                {
                    _TextFormat = value;
                    OnPropertyChangedEx("TextFormat", VisualChangeType.Layout);
                }
            }
        }
        #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 ((_AllowWrap == Tbool.NotSet) &&
                    (_Font == null) &&
                    (_MaxLineCount < 0) &&
                    (_MaxWidth < 0) &&
                    (_TextAlignment == Alignment.NotSet) &&
                    (_TextColor.IsEmpty == true) &&
                    (string.IsNullOrEmpty(_TextFormat) == true) &&
                    (base.IsEmpty == true));
            }
        }
        #endregion
        #endregion
        #region GetTextFormatFlags
        internal eTextFormat GetTextFormatFlags()
        {
            eTextFormat tf = eTextFormat.WordEllipsis |
                eTextFormat.NoPadding | eTextFormat.NoPrefix;
            if (AllowWrap == Tbool.True)
                tf |= eTextFormat.WordBreak;
            tf |= eTextFormat.HorizontalCenter | eTextFormat.VerticalCenter;
            return (tf);
        }
        #endregion
        #region GetStringFormatFlags
        internal void GetStringFormatFlags(StringFormat sf)
        {
            if (AllowWrap == Tbool.False)
                sf.FormatFlags |= StringFormatFlags.NoWrap;
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
        }
        #endregion
        #region ApplyStyle
        /// 
        /// Applies the style to instance of this style.
        /// 
        /// Style to apply.
        public void ApplyStyle(TickmarkLabelVisualStyle style)
        {
            if (style != null)
            {
                base.ApplyStyle(style);
                if (style.AllowWrap != Tbool.NotSet)
                    AllowWrap = style.AllowWrap;
                if (style.Font != null)
                    Font = style.Font;
                if (style.MaxLineCount > 0)
                    MaxLineCount = style.MaxLineCount;
                if (style.MaxWidth > 0)
                    MaxWidth = style.MaxWidth;
                if (style.TextAlignment != Alignment.NotSet)
                    TextAlignment = style.TextAlignment;
                if (style.TextColor.IsEmpty == false)
                    TextColor = style.TextColor;
                if (string.IsNullOrEmpty(style.TextFormat) == false)
                    TextFormat = style.TextFormat;
            }
        }
        #endregion
        #region Copy
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public new TickmarkLabelVisualStyle Copy()
        {
            TickmarkLabelVisualStyle style = new TickmarkLabelVisualStyle();
            CopyTo(style);
            return (style);
        }
        #endregion
        #region CopyTo
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public void CopyTo(TickmarkLabelVisualStyle style)
        {
            base.CopyTo(style);
            style.AllowWrap = _AllowWrap;
            style.Font = (_Font != null) ? (Font)_Font.Clone() : null;
            style.MaxLineCount = _MaxLineCount;
            style.MaxWidth = _MaxWidth;
            style.TextAlignment = _TextAlignment;
            style.TextColor = _TextColor;
            style.TextFormat = _TextFormat;
        }
        #endregion
        #region GetSerialData
        internal override SerialElementCollection GetSerialData(string serialName)
        {
            SerialElementCollection sec = new SerialElementCollection();
            if (serialName != null)
            {
                if (serialName.Equals("") == true)
                    serialName = "TickmarkLabelVisualStyle";
                sec.AddStartElement(serialName);
            }
            sec.AddValue("AllowWrap", AllowWrap, Tbool.NotSet);
            if (_Font != null)
                sec.AddValue("Font", XmlSerializableFont.ConvertToString(Font));
            sec.AddValue("MaxLineCount", MaxLineCount, -1);
            sec.AddValue("MaxWidth", MaxWidth, -1);
            sec.AddValue("TextAlignment", TextAlignment, Alignment.NotSet);
            sec.AddValue("TextColor", TextColor, Color.Empty);
            sec.AddValue("TextFormat", TextFormat, null);
            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 "AllowWrap":
                    AllowWrap = (Tbool)se.GetValueEnum(typeof(Tbool));
                    break;
                case "Font":
                    Font = XmlSerializableFont.ConvertFromString(se.StringValue);
                    break;
                case "MaxLineCount":
                    MaxLineCount = int.Parse(se.StringValue);
                    break;
                case "MaxWidth":
                    MaxWidth = int.Parse(se.StringValue);
                    break;
                case "TextAlignment":
                    TextAlignment = (Alignment)se.GetValueEnum(typeof(Alignment));
                    break;
                case "TextColor":
                    TextColor = se.GetValueColor();
                    break;
                case "TextFormat":
                    TextFormat = se.StringValue;
                    break;
                default:
                    base.ProcessValue(se);
                    break;
            }
        }
        #endregion
        #endregion
        #region IDisposable
        /// 
        /// Dispose
        /// 
        public override void Dispose()
        {
            base.Dispose();
        }
        #endregion
    }
}