using System.Linq;
using System.Xml.Linq;

namespace C1.Silverlight.FlexGridExcel
{
    /// <summary>
    /// Represents an MS Excel font.
    /// </summary>
    public class XLFont
    {
        //-------------------------------------------------------------------------
        #region ** ctors

        /// <summary>
        /// Initializes a new instance of the <see cref="XLFont"/> class.
        /// </summary>
        public XLFont()
        {
            Element = new XElement(XLConstants.FONT);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="XLFont"/> class.
        /// </summary>
        /// <param name="element">The xml element to load the XLCell from.</param>
        public XLFont(XElement element)
        {
            Element = element ?? new XElement(XLConstants.FONT);
        }

        #endregion

        //-------------------------------------------------------------------------
        #region ** object model

        /// <summary>
        /// Gets the <see cref="XElement"/> that represents this font.
        /// </summary>
        public XElement Element { get; private set; }
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="XLFont"/> is bold.
        /// </summary>
        /// <value><c>true</c> if bold; otherwise, <c>false</c>.</value>
        public bool Bold
        {
            get
            {
                return Element.Descendants(XLWorkbook.SheetNS + XLConstants.BOLD).FirstOrDefault() != null;
            }
            set
            {
                XElement bold = Element.Descendants(XLWorkbook.SheetNS + XLConstants.BOLD).FirstOrDefault();
                if (value)
                {
                    if (bold == null)
                    {
                        Element.Add(new XElement(XLWorkbook.SheetNS + XLConstants.BOLD));
                    }
                }
                else
                {
                    if (bold != null)
                    {
                        bold.Remove();
                    }
                }
            }
        }
        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            XLFont font = obj as XLFont;
            if (font != null)
            {
                return this.Bold.Equals(font.Bold);
            }
            return false;
        }

        #endregion
    }
}
