<?php
/**
 * Class ProfileWidget_BotBehavior
 * @deprecated 
 */
class ProfileWidget_BotBehavior extends Extension_ProfileWidget {
	const ID = 'cerb.profile.tab.widget.bot';

	function __construct($manifest=null) {
		parent::__construct($manifest);
	}
	
	function invoke(string $action, Model_ProfileWidget $model) {
		$active_worker = CerberusApplication::getActiveWorker();
		
		if(!Context_ProfileWidget::isReadableByActor($model, $active_worker))
			DevblocksPlatform::dieWithHttpError(null, 403);
		
		return false;
	}
	
	function render(Model_ProfileWidget $model, $context, $context_id) {
		$tpl_builder = DevblocksPlatform::services()->templateBuilder();
		
		$active_worker = CerberusApplication::getActiveWorker();
		
		$dict = DevblocksDictionaryDelegate::instance([
			'current_worker__context' => CerberusContexts::CONTEXT_WORKER,
			'current_worker_id' => $active_worker->id,
			'record__context' => $context,
			'record_id' => $context_id,
			'widget__context' => CerberusContexts::CONTEXT_PROFILE_WIDGET,
			'widget_id' => $model->id,
		]);
		
		$behavior_id = DevblocksPlatform::importVar($model->extension_params['behavior_id'] ?? 0, 'int', 0);
		
		if(!$behavior_id 
			|| !($behavior = DAO_TriggerEvent::get($behavior_id))
			|| $behavior->event_point != Event_DashboardWidgetRender::ID
			) {
			echo "A bot behavior isn't configured.";
			return;
		}
		
		@$behavior_params_json = DevblocksPlatform::importVar($model->extension_params['behavior_params_json'], 'string', '');
		@$behavior_params = json_decode($tpl_builder->build($behavior_params_json, $dict), true) ?: [];
		
		// Event model
		
		$actions = [];
		
		$event_model = new Model_DevblocksEvent(
			Event_DashboardWidgetRender::ID,
			[
				'widget' => $model,
				'worker' => $active_worker,
				'_variables' => $behavior_params,
				'actions' => &$actions,
			]
		);
		
		if(false == ($event = $behavior->getEvent()))
			return;
			
		$event->setEvent($event_model, $behavior);
		
		$values = $event->getValues();
		
		$dict = DevblocksDictionaryDelegate::instance($values);
		
		// Format behavior vars
		
		if(is_array($behavior_params))
		foreach($behavior_params as $k => &$v) {
			if(DevblocksPlatform::strStartsWith($k, 'var_')) {
				if(!isset($behavior->variables[$k]))
					continue;
				
				$value = $behavior->formatVariable($behavior->variables[$k], $v, $dict);
				$dict->set($k, $value);
			}
		}
		
		// Run tree
		
		$behavior->runDecisionTree($dict, false, $event);
		
		foreach($actions as $action) {
			switch($action['_action']) {
				case 'render_html':
					$html = @$action['html'];
					echo $html;
					break;
			}
		}
	}
	
	function renderConfig(Model_ProfileWidget $model) {
		$tpl = DevblocksPlatform::services()->template();
		$tpl->assign('widget', $model);
		$tpl->display('devblocks:cerberusweb.core::internal/profiles/widgets/bot/config.tpl');
	}
	
	function invokeConfig($action, Model_ProfileWidget $model) {
		return false;
	}
}
