<?php

namespace Signteer\Web\Front\Page\Sheet;

use Plainwork\Http\Request;
use Plainwork\Http\Redirect;
use Plainwork\Ui\Html;
use Signteer\Core\Criteria\TaskCriteria;
use Signteer\Core\Action\Signup\CreateSignup;
use Signteer\Core\Finder\SheetFinder;
use Signteer\Core\Finder\TaskFinder;

final class CreateSignupPage
{
    public $action = CreateSignup::class;
    public $taskFinder = TaskFinder::class;
    public $sheetFinder = SheetFinder::class;

    public function __invoke(Request $request)
    {
        $sheetId = $request->getQueryParam('sheet');
        if (!$sheetId) {
            return new Redirect('../../..');
        }

        $sheet = $this->sheetFinder->get($sheetId);
        if (!$sheet) {
            return new Redirect('../../..');
        }

        $taskId = $request->getQueryParam('task');

        $c = (new TaskCriteria())->whereSheetId('=', $sheetId)->whereId('=', $taskId);
        $task = current($this->taskFinder->find($c));
        if (!$task) {
            return new Redirect('../../..');
        }

        $html = new Html();

        $form = $html->form();
        $ret = $html->blockList();

        $ret->add($html->dl('__Task__', $task->getTitle())->asTable());

        $ret->add($html->el('p', '__Required fields are marked *__'));

        $form->add($html->textInput('title')->setRequired());
        $ret->add(
            $html->blockList(
                $html->dl('__Name__' . ' *', $form->input('title')),
                $html->dl(null, $html->el('small', '__Your name will be published on the sign-up sheet page.__')),
            )->setSpacing(1)
        );

        $form->add($html->textInput('email')->setRequired());
        $ret->add(
            $html->blockList(
                $html->dl('__Email__' . ' *', $form->input('email')),
                $html->dl(null, $html->el('small', '__Your email address will not be published.__')),
            )->setSpacing(1)
        );

        $form->add($html->textInput('phone')->setRequired());
        $ret->add(
            $html->blockList(
                $html->dl('__Phone__' . ' *', $form->input('phone')),
                $html->dl(null, $html->el('small', '__Your phone number will not be published.__')),
            )->setSpacing(1)
        );

        $values = $form->grab($request);
        if ($values) {
            if ($form->isValid()) {
                $res = ($this->action)($taskId, $values['title'], $values['email'], $values['phone']);
                if ($res->isOk()) {
                    $ret = $html->dl()->asTable()
                        ->add('__Task__', $task->getTitle())
                        ;
                    $ret = $html->main($ret);
                    $ret->title = $html->el('h2', '__You have been signed up!__');
                    $ret->afterContentMenu->add($html->link('../..', [], '__Go back to the sign-up sheet__'));

                    return $ret;
                }

                $form->addErrors($res->getErrors());
            }
        }

        $errors = $form->getErrors();
        if ($errors) {
            $ret->add($html->dl('__Error__', $html->formErrors($errors)));
        }

        $menu_ = $html->menu()
            ->add($html->button('__Sign up!__'))
            ->add($html->link('../..', [], '__Or go back to the sign-up sheet__'))
            ;
        $ret->add($html->dl(null, $menu_));

        $ret = $form->setContent($ret);

        $ret = $html->main($ret);
        $ret->title = $html->el('h2', '__Signing up__');

        return $ret;
    }
}
