<?php

namespace App\Media\QuickTime\Atoms\moov;

// Specifies the characteristics of an entire QuickTime movie.

use Carbon\Carbon;
use Illuminate\Support\Arr;

use App\Media\QuickTime\Atoms\SubAtom;

class mvhd extends SubAtom
{
	protected const unpack = [
		'version'=>['c',1],
		'flags'=>['a3',3],
		'create'=>['N',4],
		'modified'=>['N',4],
		'time_scale'=>['N',4],
		'duration'=>['N',4],
		'prate'=>['a4',4],
		'pvolume'=>['a2',2],
		'matrix'=>['a36',36],
		'prevtime'=>['N',4],
		'prevdur'=>['N',4],
		'postertime'=>['N',4],
		'selecttime'=>['N',4],
		'selectdur'=>['N',4],
		'curtime'=>['N',4],
		'nexttrack'=>['N',4],
	];

	public function __get($key): mixed
	{
		switch ($key) {
			case 'creation_date':
				// We need to convert from MAC time 1904-01-01 to epoch
				return Carbon::createFromTimestamp(Arr::get($this->cache(),'create')-2082844800);

			case 'duration':
				return ($x=$this->time_scale) ? round(Arr::get($this->cache(),'duration')/$x,3) : NULL;

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

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

			case 'time_scale':
				return round(Arr::get($this->cache(),$key));

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