<?php

namespace App\Forms;

use Nette;
use Nette\Application\UI\Form;
use Nette\Security\User;
use App;


class FirstFormFactory extends Nette\Object
{
	public $results = array();
	
	/** @var User */
	private $user;

	private $GameM;

	public function __construct(User $user, App\Model\GameManager $f)
	{
		$this->user = $user;
		$this->GameM = $f;
	}


	/**
	 * @return Form
	 */
	public function create()
	{
		$form = new Form;

		$form->addSubmit('sendI', 'Not Guilty')->setAttribute('id','send-input');
        $form->addSubmit('sendG', 'Guilty')->setAttribute('id','send-input2');
        $form->onSuccess[] = array($this, 'formSucceeded');
        return $form;
	}


    public function formSucceeded(Form $form, $values)
    {
        $guilty = $this->GameM->GetGuilty($this->user->id);
        $submit = $form->isSubmitted()->getName();
        $params = array( 'START_TIME' => new \DateTime(), 'KEY' => $this->user->id, 'STAGE' => 1 );
        $this->GameM->setEndTime($params);

        $this->GameM->NextStage($this->user->id, 2);

        if ($submit == 'sendI') {
            if ($guilty == 1) {
                $this->GameM->InsertResult($this->user->id, 1);
            } else {
                $this->GameM->InsertResult($this->user->id, 2);
            }
        } else {
            if ($guilty == 1) {
                $this->GameM->InsertResult($this->user->id, 3);
            } else {
                $this->GameM->InsertResult($this->user->id, 4);
            }

        }

    }

}
