<?php  // vim:set et ts=4 sw=4 sts=4:
# Copyright (C) 2003-2014 hipplay Inc.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE OF hipplay Inc.
# The copyright notice above does not evidence any actual or intended
# publication of such source code. The contents of this file may not
# be disclosed to third parties, copied or duplicated in any form, in
# whole or in part, without the prior written permission of
# hipplay Inc.
#
namespace HipPlayConnect\FileSystem;



class AxImageFile extends AxRegularFile
{
    const DEBUG_THUMBS = TRUE;

	private $imageSize = null;
	private $EXIF = null;
	
	public function __construct( $fsPath, $sambaPath, $URIBuilder )
	{
		parent::__construct( $fsPath, $sambaPath, $URIBuilder );
				
		$this->xmlFields =
			array_merge
			(
				$this->xmlFields,
				array
				(
					'width',
					'height',
					'timeTaken'
				)
			);
			
	}
	
	//
	// performance issue, so called on demand
	//
	private function callImageSize() {
		switch ( $this->getField( 'mimeType' ) )
		{
			case 'image/gif':
			case 'image/jpeg':
			case 'image/bmp':
			case 'image/png':
				if ( !is_null( $this->imageSize ) )
					return;
				if ( $this->imageSize = getimagesize( $this->fsPath ) )
				{
					$this->data[ 'width'  ] = $this->imageSize[ 0 ];
        			$this->data[ 'height' ] = $this->imageSize[ 1 ];
				}
				break;
		}
	}
	
	//
	// performance issue, so called on demand
	//
	private function callEXIF() {
		switch ( $this->getField( 'mimeType' ) )
		{
			case 'image/jpeg':
				if ( !is_null( $this->EXIF ) )
					return;
				if ( $this->EXIF = exif_read_data( $this->fsPath ) )
				{
					if (
						array_key_exists( 'DateTimeOriginal', $this->EXIF ) &&
						$time = strtotime( $this->EXIF[ 'DateTimeOriginal' ] )
					)
						$this->data[ 'timeTaken'  ] = $time;
				}
				break;
		}
	}

	public function getField( $key )
	{
		//
		// check if it's already loaded or if the parent knows how to get the field
		//
		if ( !is_null( $output = parent::getField( $key ) ) )
			return $output;

		switch ( $key )
		{
			case 'height':
			case 'width':
				$this->callImageSize();
				break;
			case 'timeTaken':
				$this->callEXIF();
				break;	
		}

		return
			array_key_exists( $key, $this->data )
			? $this->data[ $key ]
			: null;
	}

    /**
     * Get fspath for the specified view
     *
     * $view - view (array) to use
     * $path - (optional) fspath of the file to find views for
     *
     * @param        $view
     * @param string $path
     *
     * @return string the view dir
     */
	public function getViewFSPath( $view, $path = null )
	{
	    if ( is_null( $path ) )
	        $path = $this->fsPath;
	    
		return self::getViewFSPathFor( $view, $path );
	}
	
	public static function getViewFSPathFor( $view, $path )
    {
	    return self::getViewDir($view, $path) . '/.' . self::getBasename( $path );
    }
	
	/**
	 * Generate a view or views for the current image
	 *
	 * @param string $view the view to generate. All are generated if null
     *
     * @return string The thumbnail file path
	 */
	public function generateView( $view )
    {

        $w = self::$views[$view]['width'];
        $h = self::$views[$view]['height'];
        $thumbPath = self::getViewFSPathFor( $view, $this->fsPath );

        if (self::DEBUG_THUMBS) {
            $info = array(
                    'path' => $this->fsPath,
                    'width' => $w,
                    'height' => $h,
                    'thumbPath' => $thumbPath,
            );

			error_log('Generating Thumbnail:');
			error_log(print_r($info, TRUE));
        }

        $destFile = AxThumbnails::makeThumbnail(
                        $this->fsPath,
                        $thumbPath,
                        $w,
                        $h,
                        AxRegularFile::getViewFSPathForType( $view, 'image' )
                    );

        if (self::DEBUG_THUMBS)
		    error_log("Thumbnail result: $destFile");

		if ($destFile == FALSE)
			return NULL;

		return $destFile;
    }
}
