<?php
/**
 *  (c) CloudMunch Inc.
 *  All Rights Reserved
 *  Un-authorized copying of this file, via any medium is strictly prohibited
 *  Proprietary and confidential
 *
 *  Rosmi Chandy rosmi@cloudmunch.com
 */
namespace CloudMunch;
use CloudMunch\cmDataManager;
//require_once ("cmDataManager.php");
require_once ("AppErrorLogHandler.php");



/**
 * Class CloudmunchService
 * @package CloudMunch
 * @author Rosmi
 * This class provides the service methods for the apps to invoke action on cloudmunch
 */

class CloudmunchService {
	private $appContext = null;
	private $cmDataManager;
	public function __construct($appContext) {
		$this->appContext = $appContext;
		$this->cmDataManager = new cmDataManager();
	}
/**
 * This method is to invoke notification on cloudmunch.
 * @param  string $message : Notification message.
 * @param  string $context : Context for which user is notified.
 * @param  string $id : Name of the object.
 */
	public function notifyUsers($message, $context, $id) {
		$dataarray = array (

			"project" => $this->appContext->getProject(),
			"job" => $this->appContext->getJob(),
			"context" => $context,
			"id" => $id
			);
		return $this->cmDataManager->notifyUsersInCloudmunch($this->appContext->getMasterURL(), $message, $dataarray, $this->appContext->getDomainName());
	}
	
	/**
	 * Updates data in cloudmunch for the context.
	 * @param  string $context : Context for which data is to be updated.
	 * @param  array $dataArray : Array of data to be updated.
	 */
	public function updateDataContext( $context, $dataArray){
		return $this->cmDataManager->updateContext($this->appContext->getMasterURL(), $context, $this->appContext->getDomainName(), $dataArray);
	}
	
	/**
	 * Returns context object.
	 * @param  string $context : Context for which data is to be retreived.
	 */
	public function getDataFromContext($context){
		return $this->cmDataManager->getDataForContext($this->appContext->getMasterURL(), $context, $this->appContext->getDomainName());
		
	}
	
	/**
	 * Method to update custom context.
	 * @param string $context : custom context name.
	 * @param array $dataArray
	 * @param string $id
	 */
	public function updateCustomContext($context, $dataArray,$id){
		return $this->cmDataManager->updateCustomContext($this->appContext->getMasterURL(), $context, $this->appContext->getDomainName(), $dataArray,$id);
	}

	/**
	* This function accepts data in array format and converts to url string
	*
	* Example : 
	*
	* 	array(
	*		'action' => 'listcustomcontext',
	*		'domain' => 'test',
	*		'project' => 'projectname',
	*		'customcontext' => 'projectname_stories',
	*		'fields' => 'sum(story_points)',
	*		'username' => 'CI',
	*		'group_by' => 'fix_versions',
	*		'count' => '*',
	*		'filter' => "{\"fix_versions\":\"10\"}"
	*	);
	* @param $context Data to be passed.
	*/ 
	public function getDataFromCustomContext($context) {
		return $this->cmDataManager->getDataForCustomContext($this->appContext->getMasterURL(), $context);
	}
}
?>

