// Copyright 2024 Martin Riedl
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gomp4

// TrackFragmentBox Track Fragment Box struct
//
// 8.8.6 Track Fragment Box
// Box Type: ‘traf’
// Container: Movie Fragment Box ('moof')
// Mandatory: No
// Quantity: Zero or more
//
// Within the movie fragment there is a set of track fragments, zero or more per track. The track fragments
// in turn contain zero or more track runs, each of which document a contiguous run of samples for that
// track. Within these structures, many fields are optional and can be defaulted.
//
// It is possible to add 'empty time' to a track using these structures, as well as adding samples. Empty
// inserts can be used in audio tracks doing silence suppression, for example.
type TrackFragmentBox struct {
	*Box
	ChildBoxes []any
}

// BoxTypeTrackFragment Track Fragment Box
const BoxTypeTrackFragment = "traf"

func init() {
	BoxDefinitions = append(BoxDefinitions, BoxDefinition{
		Type:        BoxTypeTrackFragment,
		ParentTypes: []string{BoxTypeMovieFragment},
		Parser:      ParseTrackFragmentBox,
	})
}

// ParseTrackFragmentBox creates a new Track Fragment Box struct
func ParseTrackFragmentBox(parser *Parser, filePosition uint64, headerSize uint32, content []byte) (any, error) {
	box := &TrackFragmentBox{Box: &Box{filePosition, headerSize}}

	// parse child boxes
	var err error
	box.ChildBoxes, err = box.parseChildBoxes(parser, BoxTypeTrackFragment, filePosition, content)
	return box, err
}
