using System;
using System.Collections.Generic;
using System.Text;

namespace Biff8Excel.Excel
{
    //'---------------------------------------------------------------------------------------
    //' Module    : ExcelFont
    //' Author    : Alan Haigh   alan.haigh@salvesen.com
    //' Purpose   : represents a font in a row, column or cell in a worksheet
    //'---------------------------------------------------------------------------------------
    public class ExcelFont : IDisposable 
    {
        Biff8Excel.Records.Font m_font;

        string m_fontname;
        ushort m_size;
        EnumFontUnderlineType m_underline;
        EnumColours m_colour;
        EnumFontEscapement m_esc;
        EnumFontFamily m_family;
        EnumFontCharacterSet m_charset;

        bool m_bold;
        bool m_italic;
        bool m_structout;
        bool m_fontUpdated;

        internal ExcelFont Clone()
        {
            ExcelFont fnt = new ExcelFont();
            fnt.Bold = m_bold;
            fnt.CharacterSet = m_charset;
            fnt.Colour = m_colour;
            fnt.Escapement = m_esc;
            fnt.Family = m_family;
            fnt.Italic = m_italic;
            fnt.Name = m_fontname;
            fnt.Size = m_size;
            fnt.Struckout = m_structout;
            fnt.Underline = m_underline;

            return fnt;
        }

        internal void Init()
        {
            m_fontname = "Arial";            
            m_size = 10;
            m_colour = EnumColours.SystemText;
        }

        public string Name
        {
            set 
            {
                m_fontname = value;
                m_font.FontName = m_fontname;
                m_fontUpdated = true;
            }
            get { return m_fontname;}
        }

        public ushort Size
        {
            set
            {
                m_size = value;
                m_font.Size = value;
                m_fontUpdated = true;
            }
            get { return m_size; }
        }

        public bool Italic
        {
            set
            {
                m_italic = value;
                if (value)
                {
                    if (m_structout)
                        m_font.Attributes = (ushort)(EnumFontAttributes.Italic | EnumFontAttributes.StruckOut);
                    else
                        m_font.Attributes = (ushort)EnumFontAttributes.Italic;
                }
                else
                {
                    if (m_structout)
                        m_font.Attributes = (ushort)EnumFontAttributes.StruckOut;
                }
                m_fontUpdated = true;
            }
            get{return m_italic;}
        }

        public bool Struckout
        {
            set
            {
                m_structout = value;
                if (value)
                {
                    if (m_italic)
                        m_font.Attributes = (ushort)(EnumFontAttributes.Italic | EnumFontAttributes.StruckOut);
                    else
                        m_font.Attributes = (ushort)EnumFontAttributes.StruckOut;
                }
                else
                {
                    if (m_italic)
                        m_font.Attributes = (ushort)EnumFontAttributes.Italic;
                }
                m_fontUpdated = true;
            }
            get{return m_structout;}
        }

        public bool Bold
        {
            set 
            {
                m_bold = value;
                if (m_bold)
                    m_font.BoldWeight = (ushort)EnumFontBoldWeight.Bold700;
                else
                    m_font.BoldWeight = (ushort)EnumFontBoldWeight.Normal400;
                m_fontUpdated = true;
            }
            get 
            {
                return m_bold;
            }
        }

        public EnumFontUnderlineType Underline
        {
            set 
            {
                m_underline = value;
                m_font.Underline = (byte)m_underline;
                m_fontUpdated = true;
            }
            get { return m_underline; }
        }

        public EnumColours Colour
        {
            set
            {
                m_colour = value;
                m_font.ColourIndex = (ushort)m_colour;
                m_fontUpdated = true;
            }
            get { return m_colour; }
        }

        public EnumFontEscapement Escapement
        {
            set 
            {
                m_esc = value;
                m_font.Escapement = (ushort)m_esc;
                m_fontUpdated = true;
            }
            get { return m_esc; }
        }

        public EnumFontFamily Family
        {
            set
            {
                m_family = value;
                m_font.Family = (byte)m_family;
                m_fontUpdated = true;
            }
            get { return m_family; }
        }

        public EnumFontCharacterSet CharacterSet
        {
            set 
            {
                m_charset = value;
                m_font.Charset = (byte)m_charset;
                m_fontUpdated = true;
            }
            get { return m_charset; }
        }

        internal bool FontUpdated
        {
            get { return m_fontUpdated; }
        }

        public byte[] WriteRecord()
        {
            return m_font.GetByte(); 
        }

        public bool Match(ExcelFont rhs)
        {
            if (m_fontname == rhs.Name &&
                m_italic == rhs.Italic &&
                m_underline == rhs.Underline &&
                m_bold == rhs.Bold &&
                m_charset == rhs.CharacterSet &&
                m_colour == rhs.Colour &&
                m_esc == rhs.Escapement &&
                m_family == rhs.Family &&
                m_size == rhs.Size &&
                m_underline == rhs.Underline)
                return true;
            else
                return false;
        }

        public ExcelFont()
        {
            m_font = new Biff8Excel.Records.Font();
            m_font.BoldWeight = (ushort)EnumFontBoldWeight.Normal400;
            m_font.Charset = (byte)EnumFontCharacterSet.AnsiChineseSimplified;
            m_charset = EnumFontCharacterSet.AnsiChineseSimplified;
        }

        #region IDisposable ��Ա

        public void Dispose()
        {
            m_font = null;
        }

        #endregion
    }
}