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

import "encoding/binary"

// TrackFragmentDecodeTimeBox Track Fragment Decode Time Box struct
//
// 8.8.12 Track fragment decode time
//
// Box Type: `tfdt’
// Container: Track Fragment box (‘traf’)
// Mandatory: No
// Quantity: Zero or one
//
// The Track Fragment Base Media Decode Time Box provides the absolute decode time, measured on the
// media timeline, of the first sample in decode order in the track fragment. This can be useful, for
// example, when performing random access in a file; it is not necessary to sum the sample durations of all
// preceding samples in previous fragments to find this value (where the sample durations are the deltas
// in the Decoding Time to Sample Box and the sample_durations in the preceding track runs).
//
// The Track Fragment Base Media Decode Time Box, if present, shall be positioned after the Track
// Fragment Header Box and before the first Track Fragment Run box.
//
// If the time expressed in the track fragment decode time (‘tfdt’) box exceeds the sum of the durations of
// the samples in the preceding movie and movie fragments, then the duration of the last sample
// preceding this track fragment is extended such that the sum now equals the time given in this box. In
// this way, it is possible to generate a fragment containing a sample when the time of the next sample is
// not yet known.
//
// In particular, an empty track fragment (with no samples, but with a track fragment decode time box)
// may be used to establish the duration of the last sample.
type TrackFragmentDecodeTimeBox struct {
	*FullBox
	// is an integer equal to the sum of the decode durations of all earlier
	// samples in the media, expressed in the media's timescale. It does not include the samples added
	// in the enclosing track fragment.
	BaseMediaDecodeTimeV0 uint32
	// is an integer equal to the sum of the decode durations of all earlier
	// samples in the media, expressed in the media's timescale. It does not include the samples added
	// in the enclosing track fragment.
	BaseMediaDecodeTimeV1 uint64
}

const BoxTypeTrackFragmentDecodeTime = "tfdt"

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

// ParseTrackFragmentDecodeTimeBox creates a new track fragment decode time box struct based on bytes
func ParseTrackFragmentDecodeTimeBox(parser *Parser, filePosition uint64, headerSize uint32, content []byte) (any, error) {
	box := &TrackFragmentDecodeTimeBox{
		FullBox: newFullBox(&Box{filePosition, headerSize}, content[0:4]),
	}

	if box.Version == 0 {
		box.BaseMediaDecodeTimeV0 = binary.BigEndian.Uint32(content[4:8])
	} else {
		box.BaseMediaDecodeTimeV1 = binary.BigEndian.Uint64(content[4:12])
	}

	return box, nil
}
