module Sound.MED.Raw.BlockCmdPageTable where

import qualified Sound.MED.Raw.CmdPageData as CmdPageData
import Sound.MED.Raw.CmdPageData(CmdPageData)

import Sound.MED.Basic.Amiga

data BlockCmdPageTable = BlockCmdPageTable
  { num_pages :: UWORD
  , reserved  :: UWORD
  , pages     :: [ Maybe [ [ CmdPageData ] ] ]
  }
  deriving (Show)

peek :: UWORD -> UWORD -> MEM -> PTR -> IO BlockCmdPageTable
peek tracks numlines m p = do
  num_pages' <- peekUWORD m p
  reserved'  <- peekUWORD m (p+2)
  pages''    <- mapM (peekPTR m) $ pointerRangeGen (p+4) 4 num_pages'
  pages'     <- mapM (peekPage tracks numlines m) pages''
  return $ BlockCmdPageTable
    num_pages' reserved' pages'

peekPage :: UWORD -> UWORD -> MEM -> PTR -> IO (Maybe [ [ CmdPageData ] ])
peekPage tracks numlines m p =
  skipIf (p == 0) $ fmap (chunk tracks) $
    mapM (CmdPageData.peek m) $ pointerRangeGen2 p 2 tracks numlines