-- | Default values for duration, size and other metadata of track fragments.
-- This might allow a more efficient transfer and decoding of media.
module Data.ByteString.IsoBaseFileFormat.Boxes.TrackExtends where

import Data.ByteString.IsoBaseFileFormat.Box
import Data.ByteString.IsoBaseFileFormat.ReExports
import Data.ByteString.IsoBaseFileFormat.Util.BoxContent
import Data.ByteString.IsoBaseFileFormat.Util.FullBox

-- * @trex@ Box

-- | Construct a 'TrackExtends' box, with all durations and sizes set to @0@.
-- Only the track index and the index into the sample description table is
-- passed set.
trackExtendsUnknownDuration :: Word32 -> Word32 -> Box (FullBox TrackExtends 0)
trackExtendsUnknownDuration trackId descrIndex =
  trackExtends
    trackId
    descrIndex
    0
    0
    0
    0
    0
    0
    0
    False
    0

newtype TrackExtends where
  MkTrackExtends :: BuilderWithSize -> TrackExtends
  deriving (IsBoxContent)

-- | Construct a 'TrackExtends' box from all its parameters
trackExtends ::
  -- | Track ID
  Word32 ->
  -- | Sample Description Index
  Word32 ->
  -- | Sample Duration
  Word32 ->
  -- | Sample Size
  Word32 ->
  -- | is leading
  B 2 ->
  -- | sample depends on
  B 2 ->
  -- | sample is depended on
  B 2 ->
  -- | sample has redundancy
  B 2 ->
  -- | padding value
  B 3 ->
  -- | is non sync sample
  Bool ->
  -- | sample degradation priority
  Word16 ->
  Box (FullBox TrackExtends 0)
trackExtends =
  toFunction
    ( mapAccumulator
        (fullBox 0 . MkTrackExtends . execBitBuilder)
        (toFunctionBuilder (Proxy @TrackExtendsBody))
    )

instance IsBox TrackExtends

type instance BoxTypeSymbol TrackExtends = "trex"

-- | Defaults for movie fragment - the content
type TrackExtendsBody =
  "track_ID" @: FieldU32
    .+: "default_sample_description_index" @: FieldU32
    .+: "default_sample_duration" @: FieldU32
    .+: "default_sample_size" @: FieldU32
    .+: TrackExtendsDefaultSampleFlags

type TrackExtendsDefaultSampleFlags =
  "reserved" @: Field 4 := 0
    .+: "is_leading" @: Field 2
    .+: "sample_depends_on" @: Field 2
    .+: "sample_is_depended_on" @: Field 2
    .+: "sample_has_redundancy" @: Field 2
    .+: "sample_padding_value" @: Field 3
    .+: "sample_is_non_sync_sample" @: Flag
    .+: "sample_degradation_priority" @: FieldU16
    .+: 'EmptyBitRecord
