<?php

namespace App\Media\MSVideo\Containers\rlist;

// https://cdn.hackaday.io/files/274271173436768/avi.pdf

use Illuminate\Support\Arr;

use App\Media\MSVideo\Container;

class strh extends Container
{
	protected const unpack = [
		'type'=>['a4',4],		// FCC type
		'handler'=>['a4',4],	// FourCC of codec to be used
		'flags'=>['a4',4],
		'priority'=>['v',2],
		'language'=>['v',2],
		'init_frames'=>['V',4],	// Number of the First block of the stream that is present in the file.
		'scale'=>['V',4],
		'rate'=>['V',4],
		'start'=>['V',4],		// Start time of stream.
		'length'=>['V',4],		// Size of stream in units as defined in dwRate and dwScale
		'buffer_size'=>['V',4],	// Size of Buffer necessary to store blocks of that stream. Can be 0 (in that case the application has to guess)
		'quality'=>['V',4],
		'sample_size'=>['V',4],	// number of bytes of one stream atom
		'frame'=>['V',4],
	];

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

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

		$this->type = Arr::get($this->cache,'type');

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

	public function __get(string $key): mixed
	{
		switch ($key) {
			case 'audio_samplerate':
			case 'video_framerate':
				return Arr::get($this->cache,'rate') / Arr::get($this->cache,'scale',1);

			case 'audio_codec':
			case 'video_codec':
				return Arr::get($this->cache,'handler');

			case 'duration':
				return Arr::get($this->cache,'length');

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