<?php
ini_set('display_errors', 1);
date_default_timezone_set("Europe/Paris");

class Image {
	
	protected $oidentity;
	//protected $plugins;

	/**
	* Constructor 
	*
	* @param $openstack
	*		
	* @param $options
	*
	**/
	public function __construct($ostack, $options){ //, $apiP
		$this->oidentity = $ostack->imagesV2($options);
		//$this->plugins = $apiP;
	}


	/**
	* Details about an image
	*
	* @param array $opt
	*	options for the image creation
	*
	**/
	public function create_image(array $opt){
		// VOIR SI MAUVAIS TYPE
		$options = Array();
		if(isset($opt['name'])){ // string, rendre le nom obligatoire, vérifier nom pas déjà pris
		}
		else{
			//ERROR
		}
		if(isset($opt['id'])){ // UUID : nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn
			$options['id'] = $opt['id'];
		}
		if(isset($opt['visibility'])){ // public, private
			$options['visibility'] = $opt['visibility'];
		}
		if(isset($opt['tags'])){ // list
			$options['tags'] = $opt['tags'];
		}
		if(isset($opt['containerFormat'])){ // string : ami, ari, aki, bare, ovf, ova, docker
			$options['containerFormat'] = $opt['containerFormat'];
		}
		if(isset($opt['diskFormat'])){ // string : ami, ari, aki, vhd, vmdk, raw, qcow2, vdi, iso
			$options['diskFormat'] = $opt['diskFormat'];
		}
		if(isset($opt['minDisk'])){ //int
			$options['minDisk'] = $opt['minDisk'];
		}
		if(isset($opt['minRam'])){ // int
			$options['minRam'] = $opt['minRam'];
		}
		if(isset($opt['protected'])){ // boolean
			$options['protected'] = $opt['protected'];
		}
		if(isset($opt['properties'])){ // type dict ?
			$options['properties'] = $opt['properties'];
		}	

		$image = $this->oidentity->createImage($options);

		return $image;
	}

	/*
	* List images
	*/
	public function list_images(){
		// vérifier si au moins une image
		$service = $this->oidentity;
		$images = $service->listImages();
		return $images;
	}

	/**
	* Details about an image
	*
	* @param string $id
	*	identifier of the image
	*
	**/
	public function image_details($id){
		//vérifier existence image
		$service = $this->oidentity;
		$image = $service->getImage($id);
		return $image;
	}

	/**
	* Details about an image
	*
	* @param string $id
	*	id of the image
	*
	* @param array $opt
	*	options for the image creation
	**/
	public function update_image($id, array $opt){
		//vérifier existence image
		$service = $this->oidentity;
		$image = $service->getImage($id);
		$options = Array();

		// Voir vérification des types
		if(isset($opt['name'])){ //string
			$options['name'] = $opt['name'];
		}
		if(isset($opt['minDisk'])){ //int
			$options['minDisk'] = $opt['minDisk'];
		}
		if(isset($opt['minRam'])){ // int
			$options['minRam'] = $opt['minRam'];
		}
		if(isset($opt['protected'])){ // boolean
			$options['protected'] = $opt['protected'];
		}
		if(isset($opt['visibility'])){ // public, private
			$options['visibility'] = $opt['visibility'];
		}
		if(isset($opt['tags'])){ // list
			$options['tags'] = $opt['tags'];
		}
		$image->update($options);

		return $image;
	}

	/**
	* Delete an image
	*
	* @param string $id
	*	identifier of the image
	**/
	public function delete_image($id){
		// si protected = true, demander de le mettre a false
		// vérifier existence image
		$service = $this->oidentity;
		$service->getImage($id)->delete();
	}

	/**
	* Resactive an image
	*
	* @param string $id
	*	identifier of the image
	**/
	public function reactivate_image($id){
		// vérifier existence image
		$service = $this->oidentity;
		$image = $service->getImage($id);
		$image->reactivate();
	}

	/**
	* Desactive an image
	*
	* @param string $id
	*	identifier of the image
	**/
	public function desactivate_image($id){
		// vérifier existence image
		$service = $this->oidentity;
		$image = $service->getImage($id);
		$image->deactivate();
	}

	/**
	* Upload an image
	*
	* @param string $id
	*	identifier of the image
	* 
	* @param string $file_name
	*	path of the image
	**/
	public function upload_image($id, $file_name){
		// vérifier existence image
		$service = $this->oidentity;
		$image  = $service->getImage($id);
		$stream = \GuzzleHttp\Psr7\stream_for(fopen($file_name, 'r')); // A VOIR
		$image->uploadData($stream);
	}

	/**
	* Download an image
	*
	* @param string $id
	*	identifier of the image
	**/
	public function download_image($id){
		// vérifier existence image 
		$service = $this->oidentity;
		$image  = $service->getImage($id);
		$stream = $image->downloadData();
		return $stream;
	}

	/**
	* Add a member to image
	*
	* @param string $image_id
	*	identifier of the image
	*
	* @param string $member_id
	*	identifier of the member
	**/
	public function add_member($image_id, $member_id){
		// vérifier existence image
		// on doit être le proprio de l'image
		// vérifier membre existe
		$service = $this->oidentity;		
		$member_id = $service>getImage($image_id)->addMember($member_id);
	}

	
	/**
	* List members of an image
	*
	* @param string $image_id
	*	identifier of the image
	**/
	public function list_member($image_id, $member_id){
		// vérifier existence image
		$service = $this->oidentity;
		$image = $service->getImage($image_id);
		$members = $image->listMembers();
		return $members;
	}

	/**
	* Show details of a member of an image
	*
	* @param string $image_id
	*	identifier of the image
	*
	* @param string $member_id
	*	identifier of the member
	**/
	public function detail_member($image_id, $member_id){
		// vérifier existence image
		// on doit être le proprio de l'image
		// vérifier membre existe
		$service = $this->oidentity;		
		$member = $service>getImage($image_id)->getMember($member_id);
		return $member;
	}
	
	/**
	* Remove a member of an image
	*
	* @param string $image_id
	*	identifier of the image
	*
	* @param string $member_id
	*	identifier of the member
	**/
	public function remove_member($image_id, $member_id){
		// vérifier existence image
		// on doit être le proprio de l'image
		// vérifier membre existe
		$service = $this->oidentity;		
		$service>getImage($image_id)->getMember($member_id)->delete();
	}

	/**
	* Update a member of an image
	*
	* @param string $image_id
	*	identifier of the image
	*
	* @param string $member_id
	*	identifier of the member
	*
	* @param string $status
	*	new status for the member
	**/
	public function update_member($image_id, $member_id, $status){
		// vérifier existence image
		// on doit être le proprio de l'image
		// vérifier membre existe
		$service = $this->oidentity;		
		$member = $service>getImage($image_id)->getMember($member_id)->updateStatus($status);
	}

}
?>