<?php

namespace App\Presenters;

use Nette;
use App\Model;


/**
 * Base presenter for all application presenters.
 */
abstract class SecureBasePresenter extends BasePresenter
{
	
	public function startup()
	{
		parent::startup();
		
		if(!$this->user->isLoggedIn())
		{	
			$this->flashMessage('Enter your key first!');
			$this->redirect('Homepage:');
		}
		
	}
	
	/**
	 * @inject
	 * @var \App\Model\GameManager
	 */
	public $GameM;
	
	public function beforeRender()
	{
		$start_time = '';
		$stop_time = '';
		$stage_time = '';

		$row = $this->GameM->getStartTime($this->user->id);
		$start_time = isset($row->START_TIME) ? /*$row->START_TIME*/strtotime($row->START_TIME) * 1000 : '';
		
		$row = $this->GameM->getStopTime($this->user->id);
		$stop_time = isset($row->START_TIME) ? /*$row->START_TIME*/strtotime($row->START_TIME) * 1000 : '';

        $newstage = $this->GameM->GetStage($this->user->id);
        $stageslibary = array("", 0, 20, 1, 20, 2, 20, 3, 20, 4, 20, 5, 20, 6, 20, 7, 20, 8, 20, 9, 20, 10, 20, 11);
        $stage = $stageslibary[$newstage];
        if ($stage == 0)
        {
            $row = $this->GameM->getStartTime($this->user->id);
            $stage_time = isset($row->START_TIME) ? /*$row->START_TIME*/strtotime($row->START_TIME) * 1000 : '';
        }
        else {
        $row = $this->GameM->getStageTime($this->user->id, $stage);
        $stage_time = isset($row->TIME_STARTED) ? /*$row->START_TIME*/strtotime($row->TIME_STARTED) * 1000 : '';}

        if ($stage == 20)
        {
            $show = 0;
        }
        else {
            $show = 1;
        }
        if ($stage == 11)
        {
            $row = $this->GameM->getStartTime($this->user->id);
            $stage_time = isset($row->START_TIME) ? /*$row->START_TIME*/strtotime($row->START_TIME) * 1000 : '';
            $showstage = 0;
        } else {
            $showstage = 1;
        }

        $this->template->showstage = $showstage;
        $this->template->show = $show;
		$this->template->start_time = $start_time;
        $this->template->stage_time = $stage_time;
        $this->template->stop_time = $stop_time;

        $avgpeople = $this->GameM->GetAvgPeople();

        $this->template->avgpeople = $avgpeople;
		$this->getMenu();
	}
	
	function isClass($class) {
		
		return strpos( strtolower( get_class($this->presenter) ), strtolower( $class) ) !== false;
		
	}
	
	
	
}
