<?php

namespace App\Media\QuickTime\Atoms\moov\trak;

// An atom that specifies the characteristics of a single track within a movie

use Illuminate\Support\Arr;

use App\Media\QuickTime\Atoms\SubAtom;

class tkhd extends SubAtom
{
	protected const unpack = [
		'version'=>['c',1],
		'flags'=>['a3',3],
		'create'=>['N',4],
		'modified'=>['N',4],
		'trakid'=>['N',4],	// The value 0 cannot be used
		'reserved1'=>['a4',4],
		'duration'=>['N',4],
		'reserved2'=>['a8',8],
		'layer'=>['n',2],
		'altgroup'=>['n',2],
		'volume'=>['a2',2],	// 16 bit fixed point
		'reserved3'=>['a2',2],
		'matrix'=>['a36',36],
		'twidth'=>['a4',4],	// 32 bit fixed point
		'theight'=>['a4',4], // 32 bit fixed point
	];

	public function __construct(int $offset,int $size,string $filename,?string $data)
	{
		parent::__construct($offset,$size,$filename,$data);

		$this->cache = $this->cache();

		// For debugging
		if (FALSE)
			$this->debug = hex_dump($data ?: $this->data());
	}

	public function __get($key): mixed
	{
		switch ($key) {
			case 'height':
				return fixed_point_int_to_float(Arr::get($this->cache(),'theight'));

			case 'width':
				return fixed_point_int_to_float(Arr::get($this->cache(),'twidth'));

			default:
				return parent::__get($key);
		}
	}
}