<?php
/***********************************************************************
| Cerb(tm) developed by Webgroup Media, LLC.
|-----------------------------------------------------------------------
| All source code & content (c) Copyright 2002-2019, Webgroup Media LLC
|   unless specifically noted otherwise.
|
| This source code is released under the Devblocks Public License.
| The latest version of this license can be found here:
| http://cerb.ai/license
|
| By using this software, you acknowledge having read this license
| and agree to be bound thereby.
| ______________________________________________________________________
|	http://cerb.ai	    http://webgroup.media
***********************************************************************/

class Event_DashboardWidgetGetMetric extends Extension_DevblocksEvent {
	const ID = 'event.dashboard.widget.get_metric';
	
	/**
	 *
	 * @param Model_TriggerEvent $trigger
	 * @return Model_DevblocksEvent
	 */
	function generateSampleEventModel(Model_TriggerEvent $trigger) {
		$actions = [];
		
		// [TODO] Load random widget of type bot
		
		return new Model_DevblocksEvent(
			self::ID,
			array(
				'widget' => null,
				'actions' => &$actions,
			)
		);
	}
	
	function setEvent(Model_DevblocksEvent $event_model=null, Model_TriggerEvent $trigger=null) {
		$labels = [];
		$values = [];
		
		/**
		 * Behavior
		 */
		
		$merge_labels = [];
		$merge_values = [];
		CerberusContexts::getContext(CerberusContexts::CONTEXT_BEHAVIOR, $trigger, $merge_labels, $merge_values, null, true);

			// Merge
			CerberusContexts::merge(
				'behavior_',
				'',
				$merge_labels,
				$merge_values,
				$labels,
				$values
			);
		
		// Widget
		$widget = $event_model->params['widget'] ?? null;
		$labels['widget'] = 'Widget';
		$values['widget'] = $widget;
		
		// Actions
		if($event_model && array_key_exists('actions', $event_model->params)) {
			$values['_actions'] =& $event_model->params['actions'];
		} else {
			$values['_actions'] = [];
		}
		
		/**
		 * Return
		 */

		$this->setLabels($labels);
		$this->setValues($values);
	}
	
	function getValuesContexts($trigger) {
		$vals = array(
			'behavior_id' => array(
				'label' => 'Behavior',
				'context' => CerberusContexts::CONTEXT_BEHAVIOR,
			),
			'behavior_bot_id' => array(
				'label' => 'Bot',
				'context' => CerberusContexts::CONTEXT_BOT,
			),
		);
		
		$vars = parent::getValuesContexts($trigger);
		
		$vals_to_ctx = array_merge($vals, $vars);
		DevblocksPlatform::sortObjects($vals_to_ctx, '[label]');
		
		return $vals_to_ctx;
	}
	
	function getConditionExtensions(Model_TriggerEvent $trigger) {
		$labels = $this->getLabels($trigger);
		$types = $this->getTypes();
		
		$labels['widget'] = 'Widget';
		$types['widget'] = Model_CustomField::TYPE_SINGLE_LINE;
		
		$conditions = $this->_importLabelsTypesAsConditions($labels, $types);
		
		return $conditions;
	}
	
	function renderConditionExtension($token, $as_token, $trigger, $params=array(), $seq=null) {
		$tpl = DevblocksPlatform::services()->template();
		$tpl->assign('params', $params);

		if(!is_null($seq))
			$tpl->assign('namePrefix','condition'.$seq);
		
		switch($as_token) {
		}

		$tpl->clearAssign('namePrefix');
		$tpl->clearAssign('params');
	}
	
	function runConditionExtension($token, $as_token, $trigger, $params, DevblocksDictionaryDelegate $dict) {
		$pass = true;
		
		switch($as_token) {
			default:
				$pass = false;
				break;
		}
		
		return $pass;
	}
	
	function getActionExtensions(Model_TriggerEvent $trigger) {
		$actions =
			[
				'return_value' => [
					'label' => 'Return metric value',
					'notes' => '',
					'params' => [
						'value' => [
							'type' => 'text',
							'required' => true,
							'notes' => 'The metric value to return to the widget',
						]
					],
				],
			]
		;
		
		return $actions;
	}
	
	function renderActionExtension($token, $trigger, $params=array(), $seq=null) {
		$tpl = DevblocksPlatform::services()->template();
		$tpl->assign('params', $params);

		if(!is_null($seq))
			$tpl->assign('namePrefix','action'.$seq);

		$labels = $this->getLabels($trigger);
		$tpl->assign('token_labels', $labels);
			
		switch($token) {
			case 'return_value':
				$tpl->assign('label', DevblocksPlatform::translateCapitalized('common.value'));
				$tpl->assign('key', 'value');
				$tpl->display('devblocks:cerberusweb.core::events/dashboard/action_return_value.tpl');
				break;
		}
		
		$tpl->clearAssign('params');
		$tpl->clearAssign('namePrefix');
		$tpl->clearAssign('token_labels');
	}
	
	function simulateActionExtension($token, $trigger, $params, DevblocksDictionaryDelegate $dict) {
		switch($token) {
			case 'return_value':
				$tpl_builder = DevblocksPlatform::services()->templateBuilder();
				
				$return_value = $params['value'] ?? null;
				$return_value = $tpl_builder->build($return_value, $dict);
				
				$out = sprintf(">>> Returning value\n".
					"%s\n",
					$return_value
				);
				break;
		}
		
		return $out;
	}
	
	function runActionExtension($token, $trigger, $params, DevblocksDictionaryDelegate $dict) {
		switch($token) {
			case 'return_value':
				$tpl_builder = DevblocksPlatform::services()->templateBuilder();
				
				$return_value = $params['value'] ?? null;
				$return_value = $tpl_builder->build($return_value, $dict);
				
				$actions =& $dict->_actions;
				
				if(!is_array($actions))
					$actions = [];
				
				$actions[] = [
					'_action' => 'return_value',
					'_trigger_id' => $trigger->id,
					'value' => $return_value,
				];
				break;
		}
	}
};