<?php

namespace App\Models;

use DB;

class Video extends Abstracted\Catalog
{
	// ID3 Object
	private $_o = NULL;

        public function getIDLinkAttribute()
        {
                return $this->HTMLLinkAttribute($this->id,url('v/info').'/');
        }

	/**
	 * Return an GetID3 object or attribute
	 *
	 */
	protected function o($attr=NULL)
	{
		if (! $this->filename OR ! file_exists($this->file_path()) OR ! is_readable($this->file_path()))
			return FALSE;

		if (is_null($this->_o))
		{
			$this->_o = new \getID3;
			$this->_o->analyze($this->file_path());
			$this->_o->getHashdata('sha1');
		}

		return is_null($attr) ? $this->_o : (array_key_exists($attr,$this->_o->info) ? $this->_o->info[$attr] : NULL);
	}

	public function property($property)
	{
		if (! $this->o())
			return NULL;

		switch ($property)
		{
			case 'creationdate':
				// Try creation_Data
				$x = array_get($this->_o->info,'quicktime.comments.creation_date.0');

				// Try creation_Data
				if (is_null($x))
					$x = array_get($this->_o->info,'quicktime.comments.creationdate.0');

				return $x;

			case 'make': return array_get($this->_o->info,'quicktime.comments.make.0');
			case 'model': return array_get($this->_o->info,'quicktime.comments.model.0');
			case 'software': return array_get($this->_o->info,'quicktime.comments.software.0');
			case 'signature': return array_get($this->_o->info,'sha1_data');
			#case 'height': return $this->subatomsearch('quicktime.moov.subatoms',['trak','tkhd'],'height'); break;
			#case 'width': return $this->subatomsearch('quicktime.moov.subatoms',['trak','tkhd'],'width'); break;
			case 'height': return array_get($this->_o->info,'video.resolution_y');
			case 'width': return array_get($this->_o->info,'video.resolution_x');
			case 'length': return array_get($this->_o->info,'playtime_seconds');
			case 'type': return array_get($this->_o->info,'video.dataformat');
			case 'codec': return array_get($this->_o->info,'audio.codec');
			case 'audiochannels': return array_get($this->_o->info,'audio.channels');
			case 'samplerate': return array_get($this->_o->info,'audio.sample_rate');
			case 'channelmode': return array_get($this->_o->info,'audio.channelmode');
			case 'gps_lat': return array_get($this->_o->info,'quicktime.comments.gps_latitude.0');
			case 'gps_lon': return array_get($this->_o->info,'quicktime.comments.gps_longitude.0');
			case 'gps_altitude': return array_get($this->_o->info,'quicktime.comments.gps_altitude.0');
			case 'identifier': if ($x=array_get($this->_o->info,'quicktime.comments')) {
					return array_get(array_flatten(array_only($x,'content.identifier')),0);
				}
				break;
			default:
				return NULL;
		}
	}

	public function properties()
	{
		return $this->o() ? $this->_o->info : [];
	}

	/**
	 * Navigate through ID3 data to find the value.
	 */
	private function subatomsearch($atom,array $paths,$key,array $data=[]) {
		if (! $data AND is_null($data = array_get($this->_o->info,$atom)))
		{
			// Didnt find it.
			return NULL;
		}

		foreach ($paths as $path)
		{
			$found = FALSE;
			foreach ($data as $array)
			{
				if ($array['name'] === $path)
				{
					$found = TRUE;
					if ($path != last($paths))
						$data = $array['subatoms'];
					else
						$data = $array;
					break;
				}
			}

			if (! $found)
				break;
		}

		return isset($data[$key]) ? $data[$key] : NULL;
	}

	public function setDateCreated()
	{
		if ($this->property('creationdate'))
			$this->date_created = $this->property('creationdate');
	}

	public function setDateCreatedAttribute($value)
	{
		$this->attributes['date_created'] = strtotime($value);
	}

	public function setLocation()
	{
		$this->gps_lat = $this->property('gps_lat');
		$this->gps_lon = $this->property('gps_lon');
		$this->gps_altitude = $this->property('gps_altitude');
	}

	public function setMakeModel()
	{
		$this->make = $this->property('make');
		$this->model = $this->property('model');
		$this->software = $this->property('software');
		$this->type = $this->property('type');
		$this->length = $this->property('length');
		$this->codec = $this->property('codec');
		$this->audiochannels = $this->property('audiochannels');
		$this->channelmode = $this->property('channelmode');
		$this->samplerate = $this->property('samplerate');
	}

	public function setSignature()
	{
		$this->signature = $this->property('signature');
		$this->file_signature = md5_file($this->file_path());
		$this->identifier = $this->property('identifier');
	}

	public function setSubSecTime()
	{
		// NOOP
	}

	public function setThumbnail()
	{
		// NOOP
	}

	/**
	 * Return the extension of the image
	 */
	public function type($mime=FALSE)
	{
		return strtolower($mime ? File::mime_by_ext(pathinfo($this->filename,PATHINFO_EXTENSION)) : pathinfo($this->filename,PATHINFO_EXTENSION));
	}

	public function view()
	{
		return sprintf('<video width="320" height="240" src="%s" controls></video>',url('/v/view/'.$this->id));
	}
}