<?php
class Intense_Photo {
	public $source, $link, $title, $description, $sizes, $id;

	function Intense_Photo( $source, $link, $title, $description ) {
		$this->source = $source;
		$this->link = $link;
		$this->title = $title;
		$this->description = $description;
		$this->sizes = array();
	}

	function compare_sizes( $a, $b ) {
		$a_area = $a['width'] * $a['height'];
		$b_area = $b['width'] * $b['height'];

		if ($a_area == $b_area) 
			return 0;

		return ( $a_area > $b_area ) ? +1 : -1;
	}

	function add_size( $name, $width, $height, $sourceurl, $crop = null ) {
		//$this->sizes[ $width . "X" . $height ] = $sourceurl;
		$this->sizes[ $name ]['width'] = $width;
		$this->sizes[ $name ]['height'] = $height;
		$this->sizes[ $name ]['sourceurl'] = $sourceurl;
		$this->sizes[ $name ]['crop'] = $crop;

		usort( $this->sizes, array($this, 'compare_sizes'));
	}

	/**
	 * Gets the photo that is closest in size.  
	 * It will try to find one just bigger if 
	 * it can't find an exact match.  If there 
	 * isn't one bigger, it will use the largest
	 * possible.
	 *
	 * Assumes sizes are in order of smallest to largest
	 * this is safe to assume because they are sorted
	 * as they are added
	 */
	function get_closest_size( $width, $height, $crop = null ) {
		if ( $height == 0 ) $height = $width; //make sure the height isn't 0
		if ( $width == 0 ) $width = $height;//make sure the width isn't 0

		$photo_area = $width * $height;
		$closest_size = null;
		$found = false;

		usort( $this->sizes, array($this, 'compare_sizes') );		

		foreach ( $this->sizes as $size ) {			
			$size_area = $size["width"] * $size["height"];

			if ( $size_area < $photo_area ) {
				$closest_size = $size;
			} else if ( $size_area == $photo_area ) {
				$closest_size = $size;
				$found = true;
			} else if ( $size_area > $photo_area ) {
				$closest_size = $size;
				$found = true;
			}

			// early termination
			if ( $found ) {
				break;
			}
		}

		// early termination for WordPress photos
		if ( $this->source == 'wordpress' ) {
			//create lost size
			if ( !$found ) {
				$closest_size = array(			
				 	'sourceurl' => '', 
				 	'width' => $width,
				 	'height' => $height, 
				 	'crop' => $crop
				);
			}

			$crop = intense_coalesce_isset( $closest_size['crop'], $crop, false);

			return $this->create_wp_size( $closest_size['width'], $closest_size['height'], $crop );
		}

		return $closest_size;
	}

	private function create_wp_size( $width, $height, $crop ) {
		global $intense_visions_options;

		require_once INTENSE_PLUGIN_FOLDER . '/inc/plugins/WP-Image-Resizer/aq_resizer.php';
		
		$url = wp_get_attachment_url( $this->id );

		if ( empty( $url ) || !isset( $url ) ) {
			return array(
				'sourceurl' => $intense_visions_options['intense_missing_image']['url'],
				'width' => '352',
				'height' => '259'
				);
		}

		$home_url_parts = parse_url( home_url() );
		$image_url_parts = parse_url( $url );

		if ( $home_url_parts['host'] !==  $image_url_parts['host'] ) {
			$size = array(			
				'sourceurl' => $url
			);
		} else {
			$photo = wp_img_resizer_src( array( 
				'url' => $url, 
				'width' => $width,
				'height' => $height,
				'crop' => $crop,
				'retina' => false, //todo add option to generate retina images
				'single' => false
			));		

			$size = array(			
				'sourceurl' => $photo[0], 
				'width' => $photo[1],
				'height' => $photo[2]
			);				
		}

		return $size;
	}

	function get_size( $name ) {
		return $this->sizes[ $name ];
	}
}
