<?php
/**
 * @package HaloMVC
 */
namespace HaloMVC;
 
	/**
	 * Name of the connection to use by default.
	 *
	 * <code>
	 * HaloMVC\Config::initialize(function($cfg) {
	 *   $cfg->set_model_directory('/your/app/models');
	 *   $cfg->set_connections(array(
	 *     'development' => 'mysql://user:pass@development.com/awesome_development',
	 *     'production' => 'mysql://user:pass@production.com/awesome_production'));
	 * });
	 * </code>
	 *
	 * This is a singleton class so you can retrieve the {@link Singleton} instance by doing:
	 *
	 * <code>
	 * $config = HaloMVC\Config::instance();
	 * </code>
	 *
	 * @var string
	 */

class Config extends Singleton
{


	/**
	 * Contains the list of options strings.
	 *
	 * @var array
	 */
	private static $options = array();

	private static $app_file;

	private static $routes = array();


	public static function initialize()
	{
		 
		parent::instance();
        Session::instance()->start();
		Request::instance()->initialize();
		self::initAppConfig();
		self::initAppRoutes();
	}
	
	/**
	* Set and returns options values
	*
	* If multiple values are provided, set $name option with an array of those values.
	* If there is only one value, set $name option with the provided $values
	*
	* @param string $name
	* @param mixed $values,...
	* @return mixed option value for $name if $name argument is provided, else return all options
	*/
	public static function option($name = null, $values = null)
	{
	   
	  $args = func_get_args();
	  $name = array_shift($args);
	  if(is_null($name)) return self::$options;
	  if(!empty($args))
	  {
		self::$options[$name] = count($args) > 1 ? $args : $args[0];
	  }
	  if(array_key_exists($name, self::$options)) return self::$options[$name];
	  return;
	}
	
	
	private static function initAppRoutes() {
		
		self::$routes[] = array('name' => 'home', 'route' => '/', 'class' => 'Default', 'method' => 'index',
								'element_tag' => false, 'element_pattern' => false);
		self::$routes[] = array('name' => 'todo_index', 'route' => '/todo/index/:user_id', 'class' => 'TodoList', 'method' => 'index',
								'element_tag' => ':user_id', 'element_pattern' => '^\d$');
		
		
	}
	
	
	public function getRoutes() {
		
		return self::$routes;
		
	}
 
	
			


	private static function initAppConfig() {

		
		 $root_dir = dirname(self::getAppFilePath()); 
		 $base_path = dirname(self::filePath($_SERVER['SCRIPT_NAME']));
		 $base_file = basename($_SERVER['SCRIPT_NAME']);
		 $base_uri = self::filePath($base_path, (($base_file == 'index.php') ? '?' : $base_file.'?'));
		 $halo_dir = dirname(__FILE__);
		 self::option('root_dir', $root_dir);
		 self::option('base_path', $base_path);
		 self::option('base_uri', $base_uri); // set it manually if you use url_rewriting

		 self::option('includes_dir', self::filePath($root_dir, 'includes'));
		 self::option('halo_lib_dir', self::filePath($root_dir, 'includes/HaloMVC'));
		 self::option('views_dir', self::filePath($root_dir, 'views'));
		 self::option('controllers_dir', self::filePath($root_dir, 'controllers'));
		 self::option('models_dir', self::filePath($root_dir, 'models'));
		 
		 self::option('public_dir', self::filePath($root_dir, 'public'));
		 self::option('js_dir', self::filePath($root_dir, 'public/js'));
		 self::option('css_dir', self::filePath($root_dir, 'public/css'));
		 self::option('images_dir', self::filePath($root_dir, 'public/images'));
		 
		 self::option('log_dir', self::filePath($root_dir, 'log'));
		 self::option('cache_dir', self::filePath($root_dir, 'cache'));
		 
		 
		 self::option('use_logger', true);
		 
		 self::option('app_paths', array(
						'base_folder' 	=> Config::option('base_uri'),
						'images_folder' => Config::option('images_dir'),
						'js_folder' 	=> Config::option('js_dir'),
						'css_folder' 	=> Config::option('css_dir')
					));
		 

	}

	public static function getConfigPaths()
	{

		return self::option('app_paths');

	}
	
	
	/**
	* Returns application root file path
	*
	* @return string
	*/
	private static function getAppFilePath()
	{
	 
	  if(empty(self::$app_file))
	  {
		$debug_backtrace = debug_backtrace();
		$stacktrace = array_pop($debug_backtrace);
		self::$app_file = $stacktrace['file'];
	  }
	  return self::filePath(self::$app_file);
	}


	/**
	* Create a file path by concatenation of given arguments.
	* Windows paths with backslash directory separators are normalized in *nix paths.
	*
	* @param string $path, ...
	* @return string normalized path
	*/
	private static function filePath($path)
	{
	  $args = func_get_args();
	  $ds = '/';
	  $win_ds = '\\';
	  $n_path = count($args) > 1 ? implode($ds, $args) : $path;
	  if(strpos($n_path, $win_ds) !== false) $n_path = str_replace( $win_ds, $ds, $n_path );
	  $n_path = preg_replace( "#$ds+#", $ds, $n_path);
	  
	  return $n_path;
	}
 
};
?>
