<?php

namespace App\Presenters;

use Nette;
use App\Model;
use App\Forms;


class FourthPresenter extends SecureBasePresenter
{

    public function renderDefault()
    {
        $this->template->anyVariable = 'any value';
    }

    public $playerID;

    /**
     * @inject
     * @var \App\Forms\FourthFormFactory
     */
    public $factory;

    /**
     * @inject
     * @var \App\Forms\SubmitForm
     */
    public $Sfactory;

    /**
     * @inject
     * @var \App\Model\GameManager
     */
    public $GameM;

    /**
     * Faze form factory.
     * @return Nette\Application\UI\Form
     */
    public function createComponentFourthForm()
    {
        $form = $this->factory->create();
        $form->onSuccess[] = function ($form) {
            $form->getPresenter()->redirect('Fourth:outcome');
        };
        return $form;
    }

    public function actionDefault()
    {
        $stage = $this->GameM->GetStage($this->user->id);

        if ($stage != 7) {
            $stageslibary = array("", "First:default", "First:outcome", "Second:default", "Second:outcome", "Third:default",
                "Third:outcome", "Fourth:default", "Fourth:outcome", "Fifth:default", "Fifth:outcome", "Sixth:default", "Sixth:outcome",
                "Seventh:default", "Seventh:outcome", "Eighth:default", "Eighth:outcome", "Ninth:default", "Ninth:outcome",
                "Tenth:default", "Tenth:outcome", "Eleventh:default", "Eleventh:outcome", "Result:default");
            $newstage = $stageslibary[$stage];

            if ($newstage == "Result:default") {
                $this->flashMessage("The game has already ended. You have been redirected to the summary page.");
                $this->redirect('Result:default');
            } else {
                $this->flashMessage("The game is already in a different phase. You've been redirected to the right part.");
                $this->redirect($newstage);
            }
        }
    }


    public function actionOutcome()
    {
        $this->template->results = $this->GameM->GetStageResult($this->user->id, 4);

        $stage = $this->GameM->GetStage($this->user->id);

        if ($stage != 8) {
            $stageslibary = array("", "First:default", "First:outcome", "Second:default", "Second:outcome", "Third:default",
                "Third:outcome", "Fourth:default", "Fourth:outcome", "Fifth:default", "Fifth:outcome", "Sixth:default", "Sixth:outcome",
                "Seventh:default", "Seventh:outcome", "Eighth:default", "Eighth:outcome", "Ninth:default", "Ninth:outcome",
                "Tenth:default", "Tenth:outcome", "Eleventh:default", "Eleventh:outcome", "Result:default");
            $newstage = $stageslibary[$stage];

            if ($newstage == "Result:default") {
                $this->flashMessage("The game has already ended. You have been redirected to the summary page.");
                $this->redirect('Result:default');
            } else {
                $this->flashMessage("The game is already in a different phase. You've been redirected to the right part.");
                $this->redirect($newstage);
            }
        }

    }

    public function createComponentSubmitForm()
    {
        $form = $this->Sfactory->create();
        $form->onSuccess[] = function ($form) {
            $form->getPresenter()->redirect('Fifth:Default');
        };
        return $form;
    }


}
