<?php

namespace App\Media\QuickTime\Atoms\moov\trak\mdia\minf\stbl;

use Illuminate\Support\Arr;

use App\Media\QuickTime\Atoms\SubAtom;

class stsd extends SubAtom
{
	protected const unpack = [
		'size'=>['N',4],
		'format'=>['a4',4],
		'reserved'=>['a6',6],
		'index'=>['n',2],
		'encoder_version'=>['n',2],
		'encoder_revision'=>['n',2],
		'encoder_vendor'=>['a4',4],
	];

	protected const audio_record = [
		'audio_channels'=>['n',2],
		'audio_bit_depth'=>['n',2],
		'audio_compression_id'=>['n',2],
		'audio_packet_size'=>['n',2],
		'audio_sample_rate'=>['a4',4],
	];

	protected const video_record = [
		'temporal_quality'=>['N',4],
		'spatial_quality'=>['N',4],
		'width'=>['n',2],
		'height'=>['n',2],
		'resolution_x'=>['a4',4],
		'resolution_y'=>['a4',4],
		'data_size'=>['N',4],
		'frame_count'=>['n',2],
		//'codec_name'=>['a4',4],	// pascal string
	];

	public function __construct(int $offset,int $size,string $filename,?string $data,string $arg=NULL)
	{
		parent::__construct($offset,$size,$filename);
		$this->type = $arg;

		$read = unpack($this->unpack(self::atom_record),substr($data,0,$ptr=$this->unpack_size(self::atom_record)));

		for ($i=0; $i<$read['count']; $i++) {
			$this->cache = collect(unpack($this->unpack(),substr($data,$ptr,$x=$this->unpack_size())));

			$ptr += $x;

			switch ($this->type) {
				case 'soun':
					// Audio Track
					$this->audio = unpack($this->unpack(self::audio_record),substr($data,$ptr,$x=$this->unpack_size(self::audio_record)));
					$ptr += $x;

					break;

				case 'vide':
					// Video Track
					$this->video = unpack($this->unpack(self::video_record),substr($data,$ptr,$x=$this->unpack_size(self::video_record)));
					$ptr += $x;

					// codec - pascal string
					$this->video['codec'] = pascal_string(substr($data,$ptr));

					$ptr += strlen($this->video['codec'])+1;
			}

			$this->extra = substr($data,$ptr);
		}
	}

	public function __get(string $key):mixed
	{
		switch ($key) {
			case 'audio_channels':
				return Arr::get($this->audio,$key);

			case 'audio_codec':
				switch ($this->cache->get('format')) {
					case 'mp4a': return 'ISO/IEC 14496-3 AAC';

					default:
						return $this->cache->get('format');
				}

			case 'audio_samplerate':
				return fixed_point_int_to_float(Arr::get($this->audio,'audio_sample_rate',0));

			case 'video_codec':
				return Arr::get($this->video,'codec');

			case 'video_framerate':
				dd($this);
			//	return Arr::get($this->video,'codec');

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