<?php

class CATC extends Main
{
	/**
	 * Auth Object
	 * @var Auth
	 */
	private $oAuth;	
	
	public function __construct($oClassManagement, $sProcessPage)
	{
		$asClasses = array(
			array('name'=>'auth', 'project'=>true),
			array('name'=>'course', 'project'=>true)
		);
		parent::__construct($oClassManagement, $sProcessPage, $asClasses);
		
		//if($this->oDb->sDbState == Db::DB_PEACHY) $this->oAuth = new Auth($this->oDb, Settings::API_KEY);
		$this->oAuth = new Auth($this->oDb, Settings::API_KEY);
	}
	
	protected function install()
	{
		//Install DB
		$this->oDb->install();
	}
	
	protected function getSqlOptions()
	{
		return array
		(
			'tables' 			=>	array
			(
				Auth::USER_TABLE	=> array(Db::getText(Auth::USER_TABLE), 'nickname', 'pass', 'cookie'),
				'workshops'			=> array('dates'),
				'courses'			=> array(Db::getId('workshops'), 'description', 'timeslot', 'notes'),
				'docs'				=> array(Db::getId('courses'), 'type', 'path'),
				'todos'				=> array(Db::getId('courses'), 'description')
			),
			'types' 			=>	array
			(
				Db::getText(Auth::USER_TABLE)	=> "VARCHAR(32) NOT NULL",
				'nickname'						=> "VARCHAR(60) NOT NULL",
				'pass'							=> "VARCHAR(256) NOT NULL",
				'cookie'						=> "VARCHAR(255)",
				'dates'							=> "VARCHAR(50)",
				'description'					=> "VARCHAR(200)",
				'timeslot'						=> "ENUM('SAT-M', 'SAT-A', 'SUN-M', 'SUN-A')",
				'notes'							=> "LONGTEXT",
				'type'							=> "VARCHAR(10)",
				'path'							=> "VARCHAR(100)"
			),
			'constraints'		=>	array
			(
				'docs'	=> "UNIQUE KEY `uni_path` (`path`)"
			)
		);
	}
	
	public function getPage()
	{
		//Constants
		$asGlobalVars = array(
			'consts'	=> array(
				'token_sep'	=> Auth::TOKEN_SEP,
				'error'		=> self::ERROR,
				'success'	=> self::SUCCESS,
				'context'	=> $this->asContext,
				'cookie'	=> Auth::USER_COOKIE_PASS
			),
			'vars'		=> $this->getVars()
		);
		
		//Pages
		$asPages = array('logon', 'logoff', 'template', 'workshops');
		foreach($asPages as $sPage) $asGlobalVars['consts']['pages'][$sPage] = $this->getPageContent($sPage);
		
		//Main Page
		//TODO use getMainPage parent function
		$sPage = $this->getPageContent('index');
		$sPage = str_replace('asGlobalVars', json_encode($asGlobalVars), $sPage);
		return $sPage;
	}
	
	private function getVars() {
		return array(
			'id'		=> $this->oAuth->getUserId(),
			'log_in'	=> $this->isLoggedIn()
		);
	}
	
	public function getMainPage($asGlobalVars = array(), $sMainPage = 'index', $asMainPageTags=array())
	{
		return parent::getMainPage(
			array(
				'consts'	=> array(
					'token_sep'	=> Auth::TOKEN_SEP,
					'error'		=> self::ERROR,
					'success'	=> self::SUCCESS,
					'context'	=> $this->asContext,
					'cookie'	=> Auth::USER_COOKIE_PASS
				),
				'vars'		=> $this->getVars()
			),
			'index',
			array(
				'host_url'					=> $this->asContext['serv_name']
			)
		);
	}
	
	/* Authorizations handling */
	
	public function register($sToken, $sNickname)
	{
		$asResult = $this->oAuth->register($sToken, $sNickname);
		
		if($asResult['success']) return $this->logMeIn($sToken);
		else return self::getJsonResult($asResult['success'], $asResult['desc']);
	}
	
	public function isLoggedIn()
	{
		return $this->oAuth->isLoggedIn();
	}
	
	public function logMeIn($sToken)
	{
		$asLogResult = $this->oAuth->logMeIn($sToken);
		return self::getJsonResult($asLogResult['success'], $asLogResult['desc'], $this->getVars());
	}
	
	public function checkApiKey($sApiKey)
	{
		return $this->oAuth->checkApiKey($sApiKey);
	}
	
	/* Managing projects */
	/*
	public function upload()
	{
		$this->oClassManagement->incClass('uploader', true);
		$oUploader = new Uploader($this->oMedia);
		
		return $oUploader->sBody;
	}
	*/
}