<?php


namespace App\Components\Forms;


use App\Model\Translator;
use Nette\Application\UI\Control;

abstract class BaseForm extends Control
{
    protected static $templateName = 'autoRender.latte';

    /** @var Translator */
    protected $translator;

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

    protected function setupForm() : BootstrapForm
    {
        $form = new BootstrapForm();
        $form->setTranslator($this->translator);
        $form->addProtection('Form expired, try it again please.');
        $form->onSuccess[] = [$this, 'formSucceeded'];

        return $form;
    }

    /**
     * @return BootstrapForm
     */
    protected abstract function createComponentForm() : BootstrapForm;

    public abstract function formSucceeded(BootstrapForm $form, $values);

    protected function getForm() : BootstrapForm
    {
        return $this['form'];
    }

    public function render()
    {
        $this->template->setFile(__DIR__ . '/templates/' . static::$templateName);
        $this->template->render();
    }
}