<?php
/**
 * Created by PhpStorm.
 * User: Mottlik
 * Date: 16.02.2019
 * Time: 17:41
 */
namespace App\Model;

use Nette;
use Nette\Security\Passwords;
use Nette\Utils\DateTime;


/**
 * Game management.
 */
class GameManager extends Nette\Object
{

    /** @var Nette\Database\Context */
    private $database;

    public function __construct(Nette\Database\Context $database)
    {
        $this->database = $database;
    }

    public function GetStartofGame($key)
    {
        return $this->database->query('SELECT CASE WHEN EXISTS (SELECT 1 FROM d_status_of_town where`KEY`=?) THEN 1 ELSE 2 END', $key)->fetchField();

    }

    public function GenerateStart($key)
    {
        RETURN $this->database->query('INSERT INTO d_status_of_town', array('KEY' => $key, 'STAGE' => 1, 'PEOPLE' => 800, 'PLAGUE' => 0));
    }

    public function GetPopulation($key)
    {
        return $this->database->query('SELECT PEOPLE FROM d_status_of_town WHERE `KEY`=?', $key)->fetchField('PEOPLE');
    }

    public function GetPlague($key)
    {
        return $this->database->query('SELECT PLAGUE FROM d_status_of_town WHERE `KEY`=?', $key)->fetchField('PLAGUE');
    }

    public function MorePlague($key)
    {
        return $this->database->query('UPDATE d_status_of_town SET PLAGUE = PLAGUE +1 WHERE `KEY`=?', $key);
    }

    public function KillPeople($key, $death)
    {
        return $this->database->query('UPDATE d_status_of_town SET `PEOPLE` = PEOPLE -? WHERE `KEY`=?', $death, $key);
    }

    public function MoveNextStage($stage, $key)
    {
        return $this->database->query('UPDATE d_status_of_town SET `STAGE` = ? WHERE `KEY`=?', $stage, $key);
    }

    public function GetStage($key)
    {
        return $this->database->query('SELECT STAGE FROM d_status_of_town WHERE `KEY`=?', $key)->fetchField('STAGE');
    }

    public function InsertResult($key, $stage, $result)
    {

        $data = array('KEY' => $key, 'STAGE' => $stage, 'RESULT' => $result, 'TIME_STARTED' => new DateTime());

        $this->database->table('d_stage_results')->insert($data);
    }

    public function GetStageResult($key, $stage)
    {
        return $this->database->query('SELECT RESULT FROM d_stage_results WHERE `KEY`=? AND `STAGE`=?', $key, $stage)->fetchField("RESULT");
    }

    public function getStartTime($key)
    {
        return $this->database->query('SELECT START_TIME FROM d_timer WHERE `KEY`=?', $key)->fetch();
    }

    public function getStopTime($key)
    {
        return $this->database->query('SELECT START_TIME FROM d_timer WHERE `KEY`=? AND `STAGE`=1', $key)->fetch();
    }

    public function getStageTime ($key, $stage)
    {
        return $this->database->query('SELECT TIME_STARTED FROM d_stage_results WHERE `KEY`=? AND `STAGE`=?', $key, $stage)->fetch();
    }

    public function GetAvgPeople()
    {
        return $this->database->query('SELECT ROUND(AVG(PEOPLE)) FROM d_status_of_town WHERE Stage=23')->fetchField();
    }

    public function setEndTime($params)
    {
        return $this->database->query('INSERT INTO d_timer', $params);

    }
}