<?php

/**
 * Provides an access point for Config settings 
 * belonging to the "folders" section
 * 
 * @category   Atmail
 * @package    Atmail_Config
 * @subpackage Atmail_Config_Folders
 * @author     Brad Kowalczyk <brad@staff.atmail.com>
 */
 
class Atmail_Config_Folders extends Atmail_Config_Abstract
{

	const section = Atmail_Enum::CONFIG_SECTION_FOLDERS;
	
	/**
	 * Gets the default name for a folder
	 * 
	 * @param String $folder The folder we want the default name for
	 * @return String
	 */
	static public function getDefaultFolderName($folder)
	{
		switch ($folder)
		{
			case Atmail_Enum::FOLDER_INBOX  : return Atmail_Enum::DEFAULT_FOLDER_INBOX;
			case Atmail_Enum::FOLDER_DRAFTS : return Atmail_Enum::DEFAULT_FOLDER_DRAFTS;
			case Atmail_Enum::FOLDER_SENT   : return Atmail_Enum::DEFAULT_FOLDER_SENT;
			case Atmail_Enum::FOLDER_TRASH  : return Atmail_Enum::DEFAULT_FOLDER_TRASH;
			case Atmail_Enum::FOLDER_SPAM   : return Atmail_Enum::DEFAULT_FOLDER_SPAM;
		}
		
		throw new Exception("Invalid folder name requested: $folder");
	}
	
	/**
	 * Returns folder section or individual folder name
	 * 
	 * If $folder specified then return individual folder name, if not then return
	 * entire folders section as an array. Try to return default values if there are errors.
	 * 
	 * @param String $folder The name of the folder being requested from the folders section
	 * @return Array|String
	 */
	public static function get($folder="")
	{
		try
		{
			$section = parent::getSection(self::section);
			
			if (empty($folder))
			{
				return $section;
			}
		}
		catch (Exception $e)
		{
			// Catch the exception for now, but if it was the section requested
			// and not an individual folder name then re-throw the exception
			if (empty($folder))
			{
				throw $e;
			}
			
			// Individual folder requested so return a default value
			return self::getDefaultFolderName($folder);
		}
		
		if (isset($section[$folder]))
		{
			// Found folder name so return it
			return $section[$folder];
		}
		
		// No folder name found in Config so return a default value if possible
		return self::getDefaultFolderName($folder);
	}
	
	public static function sent()
	{
		return self::get(Atmail_Enum::FOLDER_SENT);
	}
	
	public static function inbox()
	{
		return self::get(Atmail_Enum::FOLDER_INBOX);
	}
	
	public static function drafts()
	{
		return self::get(Atmail_Enum::FOLDER_DRAFTS);
	}
	
	public static function trash()
	{
		return self::get(Atmail_Enum::FOLDER_TRASH);
	}
	
	public static function spam()
	{
		return self::get(Atmail_Enum::FOLDER_SPAM);
	}
	
	/**
	 * Magic method to allow requesting of config settings
	 * using the setting name as the method name.
	 * 
	 * e.g.
	 * 
	 * Atmail_Config_Folders::inbox();
	 * 
	 * would return the inbox setting from the folders section
	 * 
	 */
	public static function __callStatic($name, $args)
	{
		return parent::get($name, self::section); 
	}
	
	public static function getDefaultFolderCssId($name)
	{
		$folderNames = self::get();
		
		switch ($name) 
		{
			case $folderNames[Atmail_Enum::FOLDER_INBOX] 			 : return "folder_inbox";
			case strtoupper($folderNames[Atmail_Enum::FOLDER_INBOX]) : return "folder_inbox";
			case $folderNames[Atmail_Enum::FOLDER_DRAFTS] 			 : return "folder_drafts";
			case $folderNames[Atmail_Enum::FOLDER_SENT] 			 : return "folder_sent";
			case $folderNames[Atmail_Enum::FOLDER_SPAM] 			 : return "folder_spam";
			case $folderNames[Atmail_Enum::FOLDER_TRASH] 			 : return "folder_trash";
		}
	}
}
