<?php

/**
 * BemmelhorTheme class
 *
 * PHP version 5
 *
 * @category Component
 * @package  Bemmelhor
 * @version  1.4
 * @since    1.4
 * @author   Fabricio S Costa <fabriciojf@gmail.com>
 * 
 * @link     http://www.bemmelhor.org
 */
class BemmelhorTheme extends Object {

/**
 * Constructor
 */
	public function __construct() {
		$this->Setting = ClassRegistry::init('Setting');
	}

/**
 * Get theme aliases (folder names)
 *
 * @return array
 */
	public function getThemes() {
		$themes = array(
			'default' => 'default',
		);
		$this->folder = new Folder;
		$viewPaths = App::path('views');
		foreach ($viewPaths as $viewPath) {
			$this->folder->path = $viewPath . 'Themed';
			$themeFolders = $this->folder->read();
			foreach ($themeFolders['0'] as $themeFolder) {
				$this->folder->path = $viewPath . 'Themed' . DS . $themeFolder . DS . 'webroot';
				$themeFolderContent = $this->folder->read();
				if (in_array('theme.json', $themeFolderContent['1'])) {
					$themes[$themeFolder] = $themeFolder;
				}
			}
		}
		return $themes;
	}

/**
 * Get the content of theme.json file from a theme
 *
 * @param string $alias theme folder name
 * @return array
 */
	public function getData($alias = null) {
		if ($alias == null || $alias == 'default') {
			$manifestFile = WWW_ROOT . 'theme.json';
		} else {
			$viewPaths = App::path('views');
			foreach ($viewPaths as $viewPath) {
				if (file_exists($viewPath . 'Themed' . DS . $alias . DS . 'webroot' . DS . 'theme.json')) {
					$manifestFile = $viewPath . 'Themed' . DS . $alias . DS . 'webroot' . DS . 'theme.json';
					continue;
				}
			}
			if (!isset($manifestFile)) {
				$manifestFile = WWW_ROOT . 'theme.json';
			}
		}
		if (isset($manifestFile) && file_exists($manifestFile)) {
			$themeData = json_decode(file_get_contents($manifestFile), true);
			if ($themeData == null) {
				$themeData = array();
			}
		} else {
			$themeData = array();
		}
		return $themeData;
	}

/**
 * Get the content of theme.json file from a theme
 *
 * @param string $alias theme folder name
 * @return array
 * @deprecated use getData()
 */
	public function getThemeData($alias = null) {
		return $this->getData($alias);
	}

/**
 * Activate theme $alias
 * @param $alias theme alias
 * @return mixed On success Setting::$data or true, false on failure
 */
	public function activate($alias) {
		if ($alias == 'default' || $alias == null) {
			$alias = '';
		}
		return $this->Setting->write('Site.theme', $alias);
	}

}
