/* This file is part of AHD ID3 Tag Editor (AITE)
   A program can create and edit ID3 Tag for mp3 files

   Copyright © Ala I.Hadid 2012

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace AHD.ID3Tag
{
    /// <summary>
    /// A class that presents ID3v2 extended header
    /// </summary>
    public class ExtendedHeader
    {
        int size = 0;
        byte[] buffer;
        /// <summary>
        /// Read the extended header
        /// </summary>
        /// <param name="stream">The opened file stream that will be used to read this header, the stream MUST be seeked to 10 (after ID3 header)</param>
        public void Read(Stream stream)
        {
            byte[] header = new byte[4];//read size
            stream.Read(header, 0, 4);

            size = SynchsafeConvertor.FromSynchsafe(header);

            //now we know the size, read and store this header into buffer, since it's optional
            buffer = new byte[size];
            stream.Read(buffer, 0, size);
        }
        /// <summary>
        /// Save the extended header
        /// </summary>
        /// <param name="stream">The opened file stream that will be used to save this header, the stream MUST be seeked to 10 (after ID3 header)</param>
        public void Save(Stream stream)
        {
            //size
            stream.Write(SynchsafeConvertor.ToSynchsafeBytes(size), 0, 4);
            //data
            stream.Write(buffer, 0, buffer.Length);
        }
        /// <summary>
        /// Save the extended header
        /// </summary>
        /// <param name="buffer">Dynamic buffer used to save the ID3 Tag, this buffer should be filled with ID3v2 Tag header</param>
        public void Save(List<byte> buffer)
        {
            //size
            buffer.AddRange(SynchsafeConvertor.ToSynchsafeBytes(size));
            //data
            buffer.AddRange(buffer);
        }
    }
}
