<?php

class Setting extends PhpObject {
	
	const SETTING_LAYOUT = 'layout';
	const LAYOUT_ONE_PAGE = '1';
	const LAYOUT_TWO_PAGES = '2';
	const SETTING_FONT = 'font';
	const FONT_THOUGHTS = 'thoughts';
	const FONT_ARIAL = 'Arial';
	const FONT_VERDANA = 'Verdana';
	const SETTING_SIZE = 'Size';
	const SIZE_16 = '16';
	const SIZE_18 = '18';
	const SIZE_20 = '20';
	
	/**
	 * Database Handle
	 * @var Db
	 */
	private $oDb;
	
	public function __construct(&$oDb)
	{
		parent::__construct(__CLASS__, Settings::DEBUG);
		$this->oDb = $oDb;
	}
	
	public static function getSettingsList()
	{
		//TODO Save on database (param table)
		return array(self::SETTING_FONT, self::SETTING_SIZE, self::SETTING_LAYOUT);
	}
	
	private static function getDefaultSetting($sSettingName)
	{
		switch($sSettingName)
		{
			case self::SETTING_FONT:
				return self::FONT_THOUGHTS;
			case self::SETTING_LAYOUT:
				return self::LAYOUT_ONE_PAGE;
		}
		return false;
	}
	
	private function getSetting($sSettingName)
	{
		if(!array_key_exists($sSettingName, $this->asSettings))
		{
			$asConstraint = array(Db::getText(Db::SETTINGS_TABLE)=>$sSettingName, Db::getId(Db::USERS_TABLE)=>$this->oSession->getUserId());
			$oValue = $this->oMySql->selectValue(Db::SETTINGS_TABLE, 'value', $asConstraint);
			$this->asSettings[$sSettingName] = (!$oValue)?self::getDefaultSetting($sSettingName):$oValue;
		}
		return $this->asSettings[$sSettingName];
	}
	
	private function setSetting($sValue, $sSettingName)
	{
		$this->oMySql->insertUpdateRow(Db::SETTINGS_TABLE, array('setting'=>$sSettingName, Db::getId(Db::USERS_TABLE)=>$this->oSession->getUserId()), array('value'=>$sValue));
	}
	
	public function setSettings($asSettings)
	{
		array_walk($asSettings, array($this, 'setSetting'));
	}
	
	public function settingsPage()
	{
		$this->setPage('settings');
		$this->setPageTitle('Settings');
		$asSettingsOptions = array(	self::SETTING_LAYOUT => array(
			'One extensible page' => self::LAYOUT_ONE_PAGE,
			'Two Pages, Diary like' => self::LAYOUT_TWO_PAGES),
			self::SETTING_FONT => array(
				'AES Crawl' => self::FONT_THOUGHTS,
				'Arial' => self::FONT_ARIAL,
				'Verdana' => self::FONT_VERDANA),
			self::SETTING_SIZE => array(
				'16pt' => self::SIZE_16,
				'18pt' => self::SIZE_18,
				'20pt' => self::SIZE_20));
			
		foreach(self::getSettingsList() as $sSettingName)
		{
			$this->oPageMask->newInstance('SETTING');
			$this->oPageMask->setInstanceTag('SETTING', 'setting_name', $sSettingName);
			$sUserSetting = $this->getSetting($sSettingName);
			foreach($asSettingsOptions[$sSettingName] as $sOptionName=>$sOptionValue)
			{
				if($sOptionValue == self::getDefaultSetting($sSettingName))
				{
					$sOptionName .= ' (Default)';
				}
				$sSelectedOption = ($sUserSetting==$sOptionValue)?'selected':'';
				$asSettingOptions = array(	'setting_option_value'=>$sOptionValue,
					'setting_option_selected'=>$sSelectedOption,
					'setting_option_name'=>$sOptionName);
				$this->oPageMask->addInstance('SETTING_OPTION', $asSettingOptions);
			}
		}
	}
}