// 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

// TrackBox track box struct
//
// 8.3.1 Track Box
// Box Type: ‘trak’
// Container: Movie Box (‘moov’)
// Mandatory: Yes
// Quantity: One or more
//
// This is a container box for a single track of a presentation. A presentation consists of one or more tracks.
// Each track is independent of the other tracks in the presentation and carries its own temporal and
// spatial information. Each track will contain its associated Media Box.
//
// Tracks are used for two purposes: (a) to contain media data (media tracks) and (b) to contain
// packetization information for streaming protocols (hint tracks).
//
// There shall be at least one media track within an ISO file, and all the media tracks that contributed to
// the hint tracks shall remain in the file, even if the media data within them is not referenced by the hint
// tracks; after deleting all hint tracks, the entire un‐hinted presentation shall remain.
type TrackBox struct {
	*Box
	ChildBoxes []any
}

// BoxTypeTrack Track Box
const BoxTypeTrack = "trak"

func init() {
	BoxDefinitions = append(BoxDefinitions, BoxDefinition{
		Type:        BoxTypeTrack,
		ParentTypes: []string{BoxTypeMovie},
		Parser:      ParseTrackBox,
	})
}

// ParseTrackBox creates a new track box struct based on bytes
func ParseTrackBox(parser *Parser, filePosition uint64, headerSize uint32, content []byte) (any, error) {
	box := &TrackBox{Box: &Box{filePosition, headerSize}}

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