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

namespace C1.Silverlight.FlexGridExcel
{
    /// <summary>
    /// Represents a MS Excel cell format.
    /// </summary>
    public class XLCellFormat
    {
        //-------------------------------------------------------------------------
        #region ** ctors

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

        #endregion

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

        /// <summary>
        /// Gets the xml element that represents this cell format.
        /// </summary>
        public XElement Element { get; private set; }
        /// <summary>
        /// Gets or sets the number format id.
        /// </summary>
        /// <value>The number format id.</value>
        public int? NumberFormatId
        {
            get
            {
                if (Element.Attribute(XLConstants.NUMBER_FORMAT_ID) != null)
                {
                    return int.Parse(Element.Attribute(XLConstants.NUMBER_FORMAT_ID).Value, CultureInfo.InvariantCulture);
                }
                return null;
            }
            set
            {
                Element.SetAttributeValue(XLConstants.NUMBER_FORMAT_ID, value);
            }
        }
        /// <summary>
        /// Gets or sets the font id.
        /// </summary>
        /// <value>The font id.</value>
        public int FontId
        {
            get
            {
                if (Element.Attribute(XLConstants.FONT_ID) != null)
                {
                    return int.Parse(Element.Attribute(XLConstants.FONT_ID).Value, CultureInfo.InvariantCulture);
                }
                return -1;
            }
            set
            {
                Element.SetAttributeValue(XLConstants.FONT_ID, value);
            }
        }
        /// <summary>
        /// Gets or sets a value indicating whether the number format will be applied.
        /// </summary>
        /// <value><c>true</c> if the number format will be applied; otherwise, <c>false</c>.</value>
        public bool ApplyNumberFormat
        {
            get
            {
                if (Element.Attribute(XLConstants.APPLY_NUMBER_FORMAT) != null)
                {
                    return Element.Attribute(XLConstants.APPLY_NUMBER_FORMAT).Value.Equals("1");
                }
                return false;
            }
            set
            {
                Element.SetAttributeValue(XLConstants.APPLY_NUMBER_FORMAT, value ? "1" : "0");
            }
        }
        /// <summary>
        /// Gets or sets a value indicating whether the font will be applied.
        /// </summary>
        /// <value><c>true</c> if the font will be applied; otherwise, <c>false</c>.</value>
        public bool ApplyFont
        {
            get
            {
                if (Element.Attribute(XLConstants.APPLY_FONT) != null)
                {
                    return Element.Attribute(XLConstants.APPLY_FONT).Value.Equals("1");
                }
                return false;
            }
            set
            {
                Element.SetAttributeValue(XLConstants.APPLY_FONT, value ? "1" : "0");
            }
        }
        /// <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)
        {
            XLCellFormat cellFmt = obj as XLCellFormat;
            if (cellFmt != null)
            {
                return this.ApplyFont.Equals(cellFmt.ApplyFont)
                    && this.ApplyNumberFormat.Equals(cellFmt.ApplyNumberFormat)
                    && this.FontId.Equals(cellFmt.FontId)
                    && this.NumberFormatId.Equals(cellFmt.NumberFormatId);
            }
            return false;
        }

        #endregion
    }
}
