{- |
This module provides parsers for Header.

module ID3.Parser.Header
    ( parseHeader
    , parseFooter

import Text.ParserCombinators.Poly.State
import ID3.Parser.General
import ID3.Type.Header
import ID3.Type.Flags
import Data.Accessor

-- | Parses id3v2 'Header'
parseHeader :: TagParser ID3Header
parseHeader = do
    string "ID3"              `err` "ID3"

parseHeader_ = do
    v <- parseVersion         `err` "tag version"
    f <- parseFlags           `err` "tag flags"
    s <- parseSize            `err` "tag size"
    return $ initID3Header [tagVersion^=v, tagFlags^=f, tagSize^=s]
    -- i.e. return ([major version, revision number], [a, b, c, d], size)

parseVersion :: TagParser TagVersion
parseVersion = count 2 anyWord8

parseFlags :: TagParser TagFlags
parseFlags = do
    fs <- parseFlags_ [1..4]
    return $ mkFlags [1..4] fs
    -- i.e. %abcd0000

parseSize :: TagParser TagSize
parseSize = parseSize_ 4

{- | /ID3v2 FOOTER/ (optional)

   To speed up the process of locating an ID3v2 tag when searching from
   the end of a file, a footer can be added to the tag. It is REQUIRED
   to add a footer to an appended tag, i.e. a tag located after all
   audio data. The footer is a copy of the header, but with a different

     ID3v2 identifier           \"3DI\"
     ID3v2 version              $04 00
     ID3v2 flags                %abcd0000
     ID3v2 size             4 * %0xxxxxxx

parseFooter :: TagParser ID3Header
parseFooter = do
    string "3DI"