<?php


namespace App\Components\Forms;


use App\Model\Facades\GameFacade;
use App\Model\Translator;

class GameFormFactory extends BaseFormFactory
{
    /** @var GameFacade */
    protected $gameFacade;

    public function __construct(Translator $translator, GameFacade $gameFacade)
    {
        parent::__construct($translator);
        $this->gameFacade = $gameFacade;
    }


    public function create() : BootstrapForm
    {
        $form = parent::create();
        $form->addText('name', 'Game name:')
            ->addRule(BootstrapForm::FILLED, 'Please fill game name.');
        $form->addSelect('publisher', $this->translate('Publisher:'), $this->gameFacade->getPublisherReader()->getSelectBoxPairs())
            ->setPrompt($this->translate('Publisher'))
            ->addRule(BootstrapForm::FILLED, 'Please select publisher.')
            ->setTranslator(null);
        $form->addSelect('studio', $this->translate('Studio:'), $this->gameFacade->getStudioReader()->getSelectBoxPairs())
            ->setPrompt($this->translate('Studio'))
            ->addRule(BootstrapForm::FILLED, 'Please select studio.')
            ->setTranslator(null);
        $form->addText('version', 'Version:')
            ->addRule(BootstrapForm::FILLED, 'Please fill game version');
        $form->addSubmit('submit', 'Submit');

        return $form;
    }


    public function formSucceeded(BootstrapForm $form, $values)
    {
        $this->gameFacade->getGameWriter()->createMmoGame($values->name, $values->publisher, $values->studio, $values->version);
    }

    protected function translate($msg)
    {
        return $this->translator->translate($msg);
    }
}