<?php
require_once 'Intense-photo.php';

// https://api.smugmug.com/api/v2/doc

class Intense_SmugMug {
	public $api_key, $photos;

	// TODO: get dimensions from smugmug.  These are still zenfolio dimensions
	private $native_sizes = array(
		"Tiny"      => array( 'width' => 60, 'height' => 60 ),
		"Thumb"     => array( 'width' => 120, 'height' => 120 ),
		"Small"     => array( 'width' => 200, 'height' => 200 ),
		"Medium"    => array( 'width' => 400, 'height' => 400 ),
		"Large"     => array( 'width' => 580, 'height' => 450 ),
		"XLarge"    => array( 'width' => 800, 'height' => 630 ),
		"X2Large"   => array( 'width' => 1100, 'height' => 960 ),
		"X3Large"   => array( 'width' => 1550, 'height' => 960 ),
		"Original"  => array( 'width' => 1550, 'height' => 960 ),
	);

	function Intense_SmugMug(  ) {
		global $intense_visions_options;
		$this->consumer_key = ( isset( $intense_visions_options['intense_smugmug_consumer_key'] ) ? $intense_visions_options['intense_smugmug_consumer_key'] : null );
		$this->photos = array();
		$this->endpoints = array(
			'albums'    => 'https://api.smugmug.com/api/v2/user/%2$s!albums?APIKey=%1$s',
			'album_images' => 'https://api.smugmug.com/api/v2/album/%2$s!images?APIKey=%1$s'
		);
	}

	function get_photos( $userid, $album, $page, $pagesize ) {
		$images = array();
		$api_url  = sprintf( $this->endpoints['albums'], $this->consumer_key, $userid, $page, $pagesize );		

		$response = wp_remote_get( $api_url . '&_accept=application%2Fjson',
			array(
				'sslverify' => apply_filters( 'https_local_ssl_verify', false )
			)
		);

		if ( !is_wp_error( $response ) && $response['response']['code'] < 400 && $response['response']['code'] >= 200 ) {
			$data = json_decode( $response['body'] );
			$response = null;

			$albums = $data->Response->Album;

			foreach ($albums as $key => $smug_album) {				
				if ( ! empty( $album ) && $smug_album->Title == $album ) {
					$album_key = str_replace('/api/v2/album/', '', $smug_album->Uri);
					$api_url  = sprintf( $this->endpoints['album_images'], $this->consumer_key, $album_key, $page, $pagesize );		

					$response = wp_remote_get( $api_url . '&_accept=application%2Fjson',
						array(
							'sslverify' => apply_filters( 'https_local_ssl_verify', false )
						)
					);
				} else if ( count( $images ) < $pagesize ) {					
					$album_key = str_replace('/api/v2/album/', '', $smug_album->Uri);
					$api_url  = sprintf( $this->endpoints['album_images'], $this->consumer_key, $album_key, $page, $pagesize );		

					$response = wp_remote_get( $api_url . '&_accept=application%2Fjson',
						array(
							'sslverify' => apply_filters( 'https_local_ssl_verify', false )
						)
					);
				}

				if ( !empty( $response) && !is_wp_error( $response ) && $response['response']['code'] < 400 && $response['response']['code'] >= 200 ) {
					$data = json_decode( $response['body'] );
					$response = null;

					$album_images = $data->Response->AlbumImage;
					
					$images = array_merge( $images, $album_images );
				}
			}			
		}

		$i = 0;
		foreach( $images as $key => $smug_image ) {			
			if ( $i < $pagesize ) {
				$api_url  = 'https://api.smugmug.com' . $smug_image->Uris->ImageSizes->Uri;
				
				$response = wp_remote_get( $api_url . '?_accept=application%2Fjson&APIKey=' . $this->consumer_key,
					array(
						'sslverify' => apply_filters( 'https_local_ssl_verify', false )
					)
				);

				if ( !is_wp_error( $response ) && $response['response']['code'] < 400 && $response['response']['code'] >= 200 ) {
					$data = json_decode( $response['body'], true );
					$sizes = $data['Response']['ImageSizes'];

					//echo '<pre>'; print_r($sizes); echo '</pre>';
					$photo = new Intense_Photo( "smugmug", $smug_image->WebUri, $smug_image->FileName, $smug_image->Caption );

					foreach ( $this->native_sizes as $key => $size ) {
						//echo '<pre>'; print_r($sizes[ $key . "ImageUrl" ]); echo '</pre>';
						if ( isset( $sizes[ $key . "ImageUrl" ] ) ) {

							$photo->add_size(
								$key,
								$size["width"],
								$size["height"],
								$sizes[ $key . "ImageUrl" ] );
			 			}
					}

					$this->photos[] = $photo;
				} 
			}

			$i++;
		}		

		return $this->photos;
	}
}
