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

// SampleSizeBox sample size box struct
//
// Box Type: ‘stsz’, ‘stz2’
// Container: Sample Table Box (‘stbl’)
// Mandatory: Yes
// Quantity: Exactly one variant must be present
//
// This box contains the sample count and a table giving the size in bytes of each sample. This allows the
// media data itself to be unframed. The total number of samples in the media is always indicated in the
// sample count.
//
// There are two variants of the sample size box. The first variant has a fixed size 32‐bit field for
// representing the sample sizes; it permits defining a constant size for all samples in a track. The second
// variant permits smaller size fields, to save space when the sizes are varying but small. One of these
// boxes must be present; the first version is preferred for maximum compatibility.
type SampleSizeBox struct {
	*FullBox
	// is integer specifying the default sample size. If all the samples are the same size,
	// this field contains that size value. If this field is set to 0, then the samples have different sizes,
	// and those sizes are stored in the sample size table. If this field is not 0, it specifies the constant
	// sample size, and no array follows.
	SampleSize uint32
	// is an integer that gives the number of samples in the track; if sample‐size is 0, then
	// it is also the number of entries in the following table.
	SampleCount uint32
	// is an integer specifying the size of a sample, indexed by its number.
	SampleSizeList []SampleSizeEntry
}

type SampleSizeEntry struct {
	EntrySize uint32
}

// BoxTypeSampleSizeBox Sample Size Box
const BoxTypeSampleSizeBox = "stsz"

func init() {
	BoxDefinitions = append(BoxDefinitions, BoxDefinition{
		Type:        BoxTypeSampleSizeBox,
		ParentTypes: []string{BoxTypeSampleTable},
		Parser:      ParseSampleSizeBox,
	})
}

// ParseSampleSizeBox creates a new sample size box struct based on bytes
func ParseSampleSizeBox(parser *Parser, filePosition uint64, headerSize uint32, content []byte) (any, error) {
	box := &SampleSizeBox{
		FullBox: newFullBox(&Box{filePosition, headerSize}, content[0:4]),
	}

	// parse fields
	box.SampleSize = binary.BigEndian.Uint32(content[4:8])
	box.SampleCount = binary.BigEndian.Uint32(content[8:12])
	position := 12

	// parse sample sizes
	if box.SampleSize == 0 {
		for i := 0; i < int(box.SampleCount); i++ {
			sampleEntry := SampleSizeEntry{
				EntrySize: binary.BigEndian.Uint32(content[position+(i*4) : position+4+(i*4)]),
			}
			box.SampleSizeList = append(box.SampleSizeList, sampleEntry)
		}
	}

	return box, nil
}
