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

// VideoMediaHeaderBox video media header box struct
//
// 12.1.2 Video media header
//
// Box Types: ‘vmhd’
// Container: Media Information Box (‘minf’)
// Mandatory: Yes
// Quantity: Exactly one
// Video tracks use the VideoMediaHeaderbox in the media information box as defined in 8.4.5. The video
// media header contains general presentation information, independent of the coding, for video media.
// Note that the flags field has the value 1.
type VideoMediaHeaderBox struct {
	*FullBox
	// specifies a composition mode for this video track, from the following enumerated
	// set, which may be extended by derived specifications:
	// copy = 0 copy over the existing image
	GraphicsMode uint16
	// is a set of 3 colour values (red, green, blue) available for use by graphics modes
	CPColor []uint16
}

// BoxTypeVideoMediaHeader Video Media Header Box
const BoxTypeVideoMediaHeader = "vmhd"

func init() {
	BoxDefinitions = append(BoxDefinitions, BoxDefinition{
		Type:        BoxTypeVideoMediaHeader,
		ParentTypes: []string{BoxTypeMediaInformation},
		Parser:      ParseVideoMediaHeaderBox,
	})
}

// ParseVideoMediaHeaderBox creates a new Video Media Header Box struct
func ParseVideoMediaHeaderBox(parser *Parser, filePosition uint64, headerSize uint32, content []byte) (any, error) {
	box := &VideoMediaHeaderBox{
		FullBox: newFullBox(&Box{filePosition, headerSize}, content[0:4]),
	}

	position := 4
	box.GraphicsMode = binary.BigEndian.Uint16(content[position : position+2])
	box.CPColor = append(box.CPColor, binary.BigEndian.Uint16(content[position+2:position+4]))
	box.CPColor = append(box.CPColor, binary.BigEndian.Uint16(content[position+4:position+6]))
	box.CPColor = append(box.CPColor, binary.BigEndian.Uint16(content[position+6:position+8]))

	return box, nil
}
