<?php

namespace App\Media\MSVideo\Containers\rlist;

use Illuminate\Support\Arr;

use App\Media\MSVideo\Container;

class movi extends Container
{
	public function __construct(int $offset,int $size,string $filename,bool $be)
	{
		parent::__construct($offset,$size,$filename,$be);

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

	public function __get(string $key): mixed
	{
		switch ($key) {
			case 'signature':
				return $this->signature();

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

	/**
	 * Calculate the signature of the data
	 *
	 * @param string $alg
	 * @return string
	 */
	private function signature(string $alg='sha1'): string
	{
		if (! Arr::has($this->cache,'signature')) {
			if ($this->size) {
				$this->fopen();

				$hash = hash_init($alg);

				while (!is_null($read = $this->fread(16384)))
					hash_update($hash, $read);

				$this->fclose();

				$this->cache['signature'] = hash_final($hash);

			} else {
				$this->cache['signature'] = NULL;
			}
		}

		return $this->cache['signature'];
	}
}