<?php

App::uses('AppShell', 'Console/Command');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
App::uses('Controller', 'Controller');
App::uses('AppController', 'Controller');
App::uses('BemmelhorPlugin', 'Extensions.Lib');
App::uses('BemmelhorTheme', 'Extensions.Lib');

/**
 * Ext Shell
 *
 * Activate/Deactivate Plugins/Themes
 *	./Console/bemmelhor ext activate plugin Example
 *	./Console/bemmelhor ext activate theme minimal
 *	./Console/bemmelhor ext deactivate plugin Example
 *	./Console/bemmelhor ext deactivate theme
 *
 * @category Shell
 * @package  Bemmelhor
 * @version  1.0
 * @author   Fabricio S Costa <fabriciojf@gmail.com>
 * 
 * @link     http://www.bemmelhor.org
 */
class ExtShell extends AppShell {

/**
 * Models we use
 *
 * @var array
 */
	public $uses = array('Setting');

/**
 * BemmelhorPlugin class
 *
 * @var BemmelhorPlugin
 */
	protected $_BemmelhorPlugin = null;

/**
 * BemmelhorTheme class
 *
 * @var BemmelhorTheme
 */
	protected $_BemmelhorTheme = null;

/**
 * Controller
 *
 * @var Controller
 * @todo Remove this when PluginActivation dont need controllers
 */
	protected $_Controller = null;

/**
 * Initialize
 *
 * @param type $stdout
 * @param type $stderr
 * @param type $stdin
 */
	public function __construct($stdout = null, $stderr = null, $stdin = null) {
		parent::__construct($stdout, $stderr, $stdin);
		$this->_BemmelhorPlugin = new BemmelhorPlugin();
		$this->_BemmelhorTheme = new BemmelhorTheme();
		$CakeRequest = new CakeRequest();
		$CakeResponse = new CakeResponse();
		$this->_Controller = new AppController($CakeRequest, $CakeResponse);
		$this->_Controller->constructClasses();
		$this->_Controller->startupProcess();
		$this->_BemmelhorPlugin->setController($this->_Controller);
		$this->initialize();
	}

/**
 * Call the appropriate command
 *
 * @return void
 */
	public function main() {
		$args = $this->args;
		$this->args = array_map('strtolower', $this->args);
		$activate = $this->args[0];
		$type = $this->args[1];
		$ext = isset($args[2]) ? $args[2] : null;
		if ($type == 'theme') {
			if ($activate == 'deactivate') {
				$this->_deactivateTheme();
				return true;
			}
			$extensions = $this->_BemmelhorTheme->getThemes();
		} elseif ($type == 'plugin') {
			$extensions = $this->_BemmelhorPlugin->getPlugins();
		}
		if (!in_array($ext, $extensions)) {
			$this->err(__('%s "%s" not found.', ucfirst($type), $ext));
			return false;
		}
		return $this->{'_' . $activate . ucfirst($type)}($ext);
	}

/**
 * Display help/options
 */
	public function getOptionParser() {
		return parent::getOptionParser()
			->description(__('Activate Plugins & Themes'))
			->addArguments(array(
				'method' => array(
					'help' => __('Method to perform'),
					'required' => true,
					'choices' => array('activate', 'deactivate'),
				),
				'type' => array(
					'help' => __('Extension type'),
					'required' => true,
					'choices' => array('plugin', 'theme'),
				),
				'extension' => array(
					'help' => __('Name of extension'),
				),
			));
	}

/**
 * Activate a plugin
 *
 * @param string $plugin
 * @return boolean
 */
	protected function _activatePlugin($plugin = null) {
		$result = $this->_BemmelhorPlugin->activate($plugin);
		if ($result === true) {
			$this->out(__('Plugin "%s" activated successfully.', $plugin));
			return true;
		} elseif (is_string($result)) {
			$this->err($result);
		} else {
			$this->err(__('Plugin "%s" could not be activated. Please, try again.', $plugin));
		}
		return false;
	}

/**
 * Deactivate a plugin
 *
 * @param string $plugin
 * @return boolean
 */
	protected function _deactivatePlugin($plugin = null) {
		$result = $this->_BemmelhorPlugin->deactivate($plugin);
		if ($result === true) {
			$this->out(__('Plugin "%s" deactivated successfully.', $plugin));
			return true;
		} elseif (is_string($result)) {
			$this->err($result);
		} else {
			$this->err(__('Plugin "%s" could not be deactivated. Please, try again.', $plugin));
		}
		return false;
	}

/**
 * Activate a theme
 *
 * @param string $theme Name of theme
 * @return boolean
 */
	protected function _activateTheme($theme = null) {
		if ($r = $this->_BemmelhorTheme->activate($theme)) {
			if (is_null($theme)) {
				$this->out(__('Theme deactivated successfully.'));
			} else {
				$this->out(__('Theme "%s" activated successfully.', $theme));
			}
		} else {
			$this->err(__('Theme "%s" activation failed.', $theme));
		}
		return true;
	}

/**
 * Deactivate a theme (just reverts to default)
 *
 * @return boolean
 */
	protected function _deactivateTheme() {
		return $this->_activateTheme();
	}
}