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

namespace Biff8Excel.Records
{
    [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    struct RecordRow
    {
        public ushort opcode;
        public ushort length;
        public ushort rownumber;
        public ushort firstcol;
        public ushort lastcol;
        public ushort height;
        public ushort zeronotused;
        public ushort notused;
        [MarshalAs(UnmanagedType.U1, SizeConst = 1)]public byte optionflags1;               // hidden, badFontHeight, xf flag        
        [MarshalAs(UnmanagedType.U1, SizeConst = 1)]public byte optionflags2;                //Alawys 0x1           
        public ushort optionflags3;                                                          //has xf style
    }

    internal class Row : IRecords 
    {
        RecordRow row;
        byte m_RowHidden;
        byte m_RowFontNoMatch;
        byte m_RowExplicitFormat;
        ushort m_XFIndex;
        ushort m_SpaceAbove;
        ushort m_SpaceBelow;

        const int TwipsPerPixelY = 15;

        public Row()
        {
            row.opcode = 0x208;
            row.length = 0x10;
            row.height = (ushort)(row.height | 0x8000);     //flag for default height
            row.zeronotused = 0x0;
            row.notused = 0x0;
        }

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

        public ushort RecordSize
        {
            get { return 20; }
        }

        public ushort RowNumber
        {
            set { row.rownumber = value; }
        }

        public ushort FirstColumnNumber
        {
            set { row.firstcol = value; }
            get { return row.firstcol;}
        }

        public ushort LastColumnNumber
        {
            set { row.lastcol = value; }
            get { return row.lastcol; }
        }

        public bool Hidden
        {
            set { m_RowHidden = (byte)(value ? 0x20 : 0x0); }
        }

        public ushort HeightInPoints
        {
            set 
            {   
                //  field_4_height = vData * Screen.TwipsPerPixelY
                row.height = (ushort)(value * TwipsPerPixelY);
                m_RowFontNoMatch = 0x40;
            }
            get { return (ushort)(row.height / TwipsPerPixelY); }
        }

        public ushort ExtendedFormatIndex
        {
            set {
                m_XFIndex = value;
                m_RowExplicitFormat = (byte)(value > 0 ? 0x80 : 0x0);
            }
        }

        public bool AdditionalSpaceAbove
        {
            set { m_SpaceAbove = (ushort)(value ? 0x1000 : 0x0);}
        }

        public bool AdditionalSpaceBelow
        {
            set {
                if (value)
                    m_SpaceBelow = 0x2000;
                else
                    m_SpaceBelow = 0;
            }
        }


        #region IRecords ��Ա

        public byte[] GetByte()
        {
            row.optionflags1 = (byte)(m_RowHidden | m_RowFontNoMatch | m_RowExplicitFormat);
            row.optionflags2 = 0x1;
            row.optionflags3 = (ushort)(m_XFIndex | m_SpaceAbove | m_SpaceBelow);
            return Globals.GetStructToBytes(row);
        }
        #endregion
    }
}