using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Biff8Excel.Interfaces;

namespace Biff8Excel.Records
{
    internal enum EnumBuiltInStles 
    {
          bsNormal = 0x0,
          bsComma = 0x3,
          bsCurrency = 0x4,
          bsPercent = 0x5,
          bsCommaZero = 0x6,
          bsCurrencyZero = 0x7
    }

    internal enum EnumStyleType 
    {
          sUserDefined = 0x0,
          sBuiltIn = 0x1
    }

    [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    struct RecordStyle
    {
        public ushort opcode;
        public ushort length;
        public ushort xfindex;
        public ushort nameLength;
    }

    [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    struct RecordStyle2
    {
        public ushort opcode;
        public ushort length;
        public ushort xfindex;
        [MarshalAs(UnmanagedType.U1, SizeConst = 1)]public byte builtinstyle;
    }

    internal class Style : IRecords 
    {
        RecordStyle style;
        EnumStyleType stype;
        string m_name;
        byte m_builtinstyle;

        public Style()
        {
            style.opcode = 0x293;
            style.xfindex = 0x0;
            stype = EnumStyleType.sBuiltIn;
        }

        public ushort ID
        {
            get { return style.opcode; }
        }

        public ushort RecordSize
        {
            get { return 22; }
        }

        public EnumStyleType StyleType
        {
            set 
            {
                stype = value;
                int i = (int)value;
                i <<= 15;               //Set the last bit

                style.xfindex = (ushort)((ushort)style.xfindex | (ushort)i); 
            }
        }

        public ushort XFIndex
        {
            set { style.xfindex |= value; }
        }

        public EnumBuiltInStles BuiltinStyle
        {
            set 
            {
                m_builtinstyle = (byte)value;
            }
        }

        public string UserStyleName
        {
            set 
            {
                style.nameLength = (ushort)Globals.GetDefaultBytesLength(value);
                m_name = value;
            }
        }

        #region IRecords ��Ա

        public byte[] GetByte()
        {
            byte[] b;

            int ln = (stype == EnumStyleType.sBuiltIn ? 8 : (Globals.GetDefaultBytesLength(m_name) + 8));

            b = new byte[ln];

            style.length = (ushort)(b.Length - 4);

            if (stype == EnumStyleType.sBuiltIn)
            {
                RecordStyle2 st;
                st.opcode = style.opcode;
                st.length = style.length;
                st.xfindex = style.xfindex;
                st.builtinstyle = m_builtinstyle;
                Globals.GetStructToBytes(st).CopyTo(b, 0);
            }
            else
            {
                Globals.GetStructToBytes(style).CopyTo(b, 0);
                Globals.GetDefaultBytes(m_name).CopyTo(b, 8);
            }

            return b;
        }

        #endregion
    }
}