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

// MovieFragmentBox Movie Fragment Box struct
//
// 8.8.4 Movie Fragment Box
// Box Type: ‘moof’
// Container: File
// Mandatory: No
// Quantity: Zero or more
//
// The movie fragments extend the presentation in time. They provide the information that would
// previously have been in the Movie Box. The actual samples are in Media Data Boxes, as usual, if they are
// in the same file. The data reference index is in the sample description, so it is possible to build
// incremental presentations where the media data is in files other than the file containing the Movie Box.
//
// The Movie Fragment Box is a top‐level box, (i.e. a peer to the Movie Box and Media Data boxes). It
// contains a Movie Fragment Header Box, and then one or more Track Fragment Boxes.
//
// NOTE There is no requirement that any particular movie fragment extend all tracks present in the movie
// header, and there is no restriction on the location of the media data referred to by the movie fragments.
// However, derived specifications may make such restrictions.
type MovieFragmentBox struct {
	*Box
	ChildBoxes []any
}

// BoxTypeMovieFragment Movie Fragment Box
const BoxTypeMovieFragment = "moof"

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

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

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