using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
namespace DevComponents.DotNetBar.Charts.Style
{
    /// 
    /// Represents a ReferenceLineVisualStyle.
    /// 
    [TypeConverter(typeof(VisualStylesConverter))]
    public class ReferenceLineVisualStyle : ChartLineVisualStyle
    {
        #region Private variables
        private Alignment _TextAlignment = Alignment.NotSet;
        private Tbool _AllowWrap = Tbool.NotSet;
        private Color _TextColor = Color.Empty;
        private Padding _TextPadding;
        private Font _Font;
        #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.Render);
                }
            }
        }
        #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 TextAlignment
        ///
        /// Gets or sets the Text alignment.
        ///
        [DefaultValue(Alignment.NotSet), Category("Appearance")]
        [Description("Indicates the Text alignment.")]
        public Alignment TextAlignment
        {
            get { return (_TextAlignment); }
            set
            {
                if (value != _TextAlignment)
                {
                    _TextAlignment = value;
                    OnPropertyChangedEx("TextAlignment", VisualChangeType.Render);
                }
            }
        }
        #endregion
        #region TextColor
        ///
        /// Gets or sets the Reference Line Text Color.
        ///
        [Category("Appearance")]
        [Description("Indicates the Reference Line Text Color.")]
        public Color TextColor
        {
            get { return (_TextColor); }
            set
            {
                if (value != _TextColor)
                {
                    _TextColor = value;
                    OnPropertyChangedEx("TextColor", Style.VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeTextColor()
        {
            return (_TextColor.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        public void ResetTextColor()
        {
            _TextColor = Color.Empty;
        }
        #endregion
        #region TextPadding
        /// 
        /// Gets or sets spacing between the content and edges of the Text.
        /// 
        [Description("Indicates the spacing between the content and edges of the Text")]
        public Padding TextPadding
        {
            get
            {
                if (_TextPadding == null)
                {
                    _TextPadding = Padding.Empty;
                    UpdateChangeHandler(null, _TextPadding);
                }
                return (_TextPadding);
            }
            set
            {
                if (_TextPadding != value)
                {
                    UpdateChangeHandler(_TextPadding, value);
                    _TextPadding = value;
                    OnPropertyChangedEx("TextPadding", VisualChangeType.Render);
                }
            }
        }
        /// 
        /// Gets whether property should be serialized.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool ShouldSerializeTextPadding()
        {
            return (_TextPadding != null && _TextPadding.IsEmpty == false);
        }
        /// 
        /// Resets property to its default value.
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        private void ResetTextPadding()
        {
            TextPadding = null;
        }
        #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) &&
                    (_TextAlignment == Alignment.NotSet) &&
                    (_TextColor.IsEmpty == true) &&
                    (_TextPadding == null || _TextPadding.IsEmpty == true) &&
                    (base.IsEmpty == true));
            }
        }
        #endregion
        #endregion
        #region GetStringFormatFlags
        internal void GetStringFormatFlags(StringFormat sf)
        {
            if (AllowWrap == Tbool.False)
            {
                sf.FormatFlags |= StringFormatFlags.NoWrap;
                sf.Trimming = StringTrimming.None;
            }
            switch (TextAlignment)
            {
                case Alignment.TopLeft:
                    sf.Alignment = StringAlignment.Near;
                    break;
                case Alignment.TopCenter:
                    sf.Alignment = StringAlignment.Center;
                    break;
                case Alignment.TopRight:
                    sf.Alignment = StringAlignment.Far;
                    break;
                case Alignment.MiddleLeft:
                    sf.Alignment = StringAlignment.Near;
                    break;
                case Alignment.MiddleCenter:
                    sf.Alignment = StringAlignment.Center;
                    break;
                case Alignment.MiddleRight:
                    sf.Alignment = StringAlignment.Far;
                    break;
                case Alignment.BottomLeft:
                    sf.Alignment = StringAlignment.Near;
                    break;
                case Alignment.BottomCenter:
                    sf.Alignment = StringAlignment.Center;
                    break;
                case Alignment.BottomRight:
                    sf.Alignment = StringAlignment.Far;
                    break;
            }
        }
        #endregion
        #region ApplyStyle
        /// 
        /// Applies the style to instance of this style.
        /// 
        /// Style to apply.
        public void ApplyStyle(ReferenceLineVisualStyle style)
        {
            if (style != null)
            {
                base.ApplyStyle(style);
                if (style.AllowWrap != Tbool.NotSet)
                    _AllowWrap = style.AllowWrap;
                if (style.Font != null)
                    _Font = (Font)style.Font.Clone();
                if (style.TextAlignment != Alignment.NotSet)
                    _TextAlignment = style.TextAlignment;
                if (style.TextColor.IsEmpty == false)
                    _TextColor = style.TextColor;
                if (style._TextPadding != null && style._TextPadding.IsEmpty == false)
                    TextPadding = style.TextPadding.Copy();
            }
        }
        #endregion
        #region Copy
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public new ReferenceLineVisualStyle Copy()
        {
            ReferenceLineVisualStyle style = new ReferenceLineVisualStyle();
            CopyTo(style);
            return (style);
        }
        #endregion
        #region CopyTo
        /// 
        /// Returns the copy of the style.
        /// 
        /// Copy of the style.
        public void CopyTo(ReferenceLineVisualStyle style)
        {
            base.CopyTo(style);
            style.AllowWrap = _AllowWrap;
            style.Font = (_Font != null) ? (Font)_Font.Clone() : null;
            style.TextAlignment = _TextAlignment;
            style.TextColor = _TextColor;
            style.TextPadding = (_TextPadding != null) ? _TextPadding.Copy() : null;
        }
        #endregion
        #region GetSerialData
        internal override SerialElementCollection GetSerialData(string serialName)
        {
            SerialElementCollection sec = new SerialElementCollection();
            if (serialName != null)
            {
                if (serialName.Equals("") == true)
                    serialName = "ReferenceLineVisualStyle";
                sec.AddStartElement(serialName);
            }
            sec.AddValue("AllowWrap", AllowWrap, Tbool.NotSet);
            if (_Font != null)
                sec.AddValue("Font", XmlSerializableFont.ConvertToString(Font));
            sec.AddValue("TextAlignment", TextAlignment, Alignment.NotSet);
            sec.AddValue("TextColor", TextColor, Color.Empty);
            if (_TextPadding != null && _TextPadding.IsEmpty == false)
                sec.AddElement(_TextPadding.GetSerialData("TextPadding"));
            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 "TextAlignment":
                    TextAlignment = (Alignment)se.GetValueEnum(typeof(Alignment));
                    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 "TextPadding":
                    sec.PutSerialData(TextPadding);
                    break;
                default:
                    base.ProcessCollection(se);
                    break;
            }
        }
        #endregion
        #endregion
        #region IDisposable
        /// 
        /// Dispose
        /// 
        public override void Dispose()
        {
            TextPadding = null;
            base.Dispose();
        }
        #endregion
    }
    #region enums
    #region NearAlignment
    public enum NearAlignment
    {
        NotSet = -1,
        Near,
        Far,
    }
    #endregion
    #endregion
}