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

// MediaBox media box struct
//
// 8.4.1 Media Box
//
// Box Type: ‘mdia’
// Container: Track Box (‘trak’)
// Mandatory: Yes
// Quantity: Exactly one
//
// The media declaration container contains all the objects that declare information about the media data
// within a track.
type MediaBox struct {
	*Box
	ChildBoxes []any
}

// BoxTypeMedia Media Box
const BoxTypeMedia = "mdia"

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

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

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