<?php

namespace Signteer\Web\Admin\Page\Sheet;

use Plainwork\Http\Request;
use Plainwork\Http\Redirect;
use Plainwork\Ui\Html;
use Signteer\Core\Criteria\TaskCriteria;
use Signteer\Core\Criteria\SignupCriteria;
use Signteer\Core\Action\Task\UpdateTask;
use Signteer\Core\Finder\SignupFinder;
use Signteer\Core\Finder\SheetFinder;
use Signteer\Core\Finder\TaskFinder;

final class EditTaskPage
{
    public $actionUpdateTask = UpdateTask::class;
    public $sheetFinder = SheetFinder::class;
    public $taskFinder = TaskFinder::class;
    public $signupFinder = SignupFinder::class;

    public function __invoke(Request $request)
    {
        $sheetId = $request->getQueryParam('sheet');
        $taskId = $request->getQueryParam('task');
        if ((null === $sheetId) or (null === $taskId)) {
            return new Redirect('admin/sheet');
        }

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

        $c = (new SignupCriteria())->whereTaskId('=', $taskId);
        $countSignups = $this->signupFinder->count($c);

        $html = new Html();

        $form = $html->form();

        $v = $m->getTitle();
        $form->add($html->textInput('title')->setRequired()->setValue($v));

        $v = $m->getSpots();
        $min = max(1, $countSignups);
        $form->add($html->numberInput('spots')->setRequired()->setValue($v)->setMin($min));

        $values = $form->grab($request);
        if ($values) {
            if ($form->isValid()) {
                $res = ($this->actionUpdateTask)($taskId, $values['title'], $values['spots']);
                if ($res->isOk()) {
                    $taskId = $res->getReturn();

                    $ret = '__Task updated__';
                    $ret = $html->main($ret);
                    $ret->title = $html->el('h1', '__Action result__');
                    $ret->afterContentMenu->add($html->link('admin/sheet/' . $sheetId, [], '__View sheet__'));
                    $ret->afterContentMenu->add($html->link('admin/sheet', [], '__View all sheets__'));
                    $ret->afterContentMenu->add($html->link('admin/sheet/' . $sheetId . '/task', [], '__Manage sheet\'s tasks__'));
                    $ret->afterContentMenu->add($html->link('admin/sheet/' . $sheetId . '/task/create', [], '__Add new task__'));

                    return $ret;
                }

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

        $ret = $html->blockList()
            ->add($html->dl('__What to do__', $form->input('title')))
            ->add($html->dl('__Number of spots__', $form->input('spots')))
            ->add($html->dl('__Signed up volunteers__', $html->card($countSignups)))
            ;

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

        $ret->add($html->dl(null, $html->button('__Save changes__')));
        $ret = $form->setContent($ret);

        $ret = $html->main($ret);
        $ret->title = $html->el('h1', '__Edit task__');

        return $ret;
    }
}
