<?php

uses( 'cloudfiles/cloudfiles' );


class SongHandlerComponent extends Object {
	
	private $controller;

	/**
	 * array of all allowed image file extentions used to 
	 * verify uploaded images
	 */
	private $allowed_ext = array('mp3');
	
	/**
	 * once verified this holds a reference to the image file that will be 
	 * manipulated
	 */
	private $song = null;

	/**
	 * once an image is set this holds the original file name minus the extention
	 * if nothing is set or there is no name?? it defualts to untitled
	 */
	private $original_name = 'untitled';
	
	private $fileInfo = null;
	
	/**
	 *
	 */
	private $auth = null;
	
	/**
	 *
	 */
	private $conn = null;
	
	
	/**
	 * class initilization method called when loaded by a controller
	 *
	 * @param Object - $controller - a reference to the controller that has loaded this component
	 */
	public function startup(&$controller) {
		
		vendor('getid3');
		$this->controller = $controller;
		
		if( defined( 'CF_USERNAME') && defined('CF_API_KEY') ) { 
			
			$this->auth = new CF_Authentication(CF_USERNAME, CF_API_KEY);
			$this->auth->authenticate();
			$this->conn = new CF_Connection( $this->auth );
		}
	}
	
	/**
	 * checks an uploaded image file for errors and verifies its safe to use
	 * this must be called before using any other method in this class
	 * every method will act on the image verified here.
	 *
	 * @param array $img - the $_FILE[image_name] array created by the file upload
	 * @return boolean success 
	 */
	public function verifyUpload( $song ) {
		
		//check for upload errors
		if($song['error'] > 0 ) {
			switch( $song['error'] ) {
				case 1:
				case 2:
					$this->controller->returnError( array('file' => 'song_max_upload_size'), true);
				break;
				case 3:
					$this->controller->returnError( array('file' => 'song_partial_upload'), true);
				break;
				case 4:
					$this->controller->returnError( array('file' => 'no_song_uploaded'), true);
				break;
			}
			return false;
		}
		
		//confirm its an uploaded file
		if( !is_uploaded_file( $song['tmp_name'] ) ) {
			$this->controller->returnError( array('file' => 'song_not_uploaded'), true);
			return false;
		}
		
		//confirm file extention
		$ext = extention( $song['name'] );
		if( !in_array( $ext, $this->allowed_ext ) ) {
			$this->controller->returnError( array('file' => 'song_wrong_filetype'), true);
			return false;
		}
		
		//every thing is good set image
		return $this->setSong( $song['tmp_name'], $song['name'] );
	}
	
	public function setSong( $song, $name=null ) {
		
		$getID3 = new getID3;
		$this->fileInfo = $getID3->analyze( $song );
		$this->song = $song;
		$this->ext = '.'.extention( $name );
		$this->original_name = str_replace($this->ext, '', $name);
		return true;
	}
	
	/**
	 * moves the set song to the destination container and gives it a unique name
	 *
	 * @param string $container - the name of the Cloud Files Container you want the file moved to
	 * @return 
	 *		- success the new name of the song file
	 *		- failure boolean false
	 */
	public function moveFileTo( $container ) {
		
		$NewName = time().$this->ext;
		$container = $this->conn->get_container( $container );
		$object = $container->create_object( $NewName );
		$object->load_from_filename( $this->song );
		return $NewName;
	}
	
	/**
	 * removes and song file from Cloud File Storage
	 *
	 * @param string $container - the Cloud File container the song file is stored in
	 * @param string $file - the name of the file
	 * @return boolean success
	 */	
	public function delete( $container, $file ) {
		
		$con = $this->conn->get_container( $container );
		$con->delete_object( $file );
		return true;
	}
	
	public function getGenre() {
		
		return (!empty($this->fileInfo['tags']['id3v1']['genre'][0])) ? $this->fileInfo['tags']['id3v1']['genre'][0] : 'unknown';	
	}
	
	public function getOrigName() {
		
		return $this->original_name;
	}
	
	public function getAlbum() {
		
		return ( isset($this->fileInfo['tags']['id3v1']['album'][0]) ? $this->fileInfo['tags']['id3v1']['album'][0] : 'unknown' );
	}
	
	public function getYear() {
		
		return ( isset($this->fileInfo['tags']['id3v1']['year'][0]) ? $this->fileInfo['tags']['id3v1']['year'][0] : null );
	}
	
	public function getTitle() {
		
		return ( isset($this->fileInfo['tags']['id3v1']['title'][0]) ? $this->fileInfo['tags']['id3v1']['title'][0] : $this->original_name );
	}
	
	public function getLength() {
		
		$this->controller->log( $this->fileInfo['playtime_seconds'] );
		
		return isset($this->fileInfo['playtime_seconds']) ? ceil($this->fileInfo['playtime_seconds']) : 0;
	}
	
	public function getArtist() {
		
		return ( isset($this->fileInfo['tags']['id3v1']['artist'][0]) ? $this->fileInfo['tags']['id3v1']['artist'][0] : 'unknown' );
	}
}
?>