<?php

namespace Signteer\Web\Admin\Page\Sheet;

use Plainwork\Http\Request;
use Plainwork\Http\Redirect;
use Plainwork\Ui\Html;
use Signteer\Core\Action\Signup\UnassignVolunteer;
use Signteer\Core\Criteria\TaskCriteria;
use Signteer\Core\Criteria\SignupCriteria;
use Signteer\Core\Finder\SheetFinder;
use Signteer\Core\Finder\TaskFinder;
use Signteer\Core\Finder\SignupFinder;
use Signteer\Core\Model\SignupFieldFinder;

final class UnassignVolunteerPage
{
    public $actionUnassignVolunteer = UnassignVolunteer::class;
    public $sheetFinder = SheetFinder::class;
    public $taskFinder = TaskFinder::class;
    public $signupFinder = SignupFinder::class;

    public function breadcrumbTitle(Request $request)
    {
        $signupId = $request->getQueryParam('signup');
        $signup = $this->signupFinder->get($signupId);
        if (!$signup) {
            return;
        }

        $ret = $signup->getTitle();

        return $ret;
    }

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

        $c = (new SignupCriteria())
            ->whereSheetId('=', $sheetId)
            ->whereId('=', $signupId)
            ;
        $signup = current($this->signupFinder->findWithVolunteer($c));
        if (!$signup) {
            return (new Redirect('admin/sheet/' . $sheetId));
        }

        $volunteer = $signup->getVolunteer();

        $taskId = $signup->getTaskId();
        $task = $this->taskFinder->get($taskId);

        $html = new Html();

        $ret = $html->dl()->asTable()
            ->add('__Task__', $task)
            ->add('__Volunteer__', $volunteer)
            ;

        $form = $html->form();
        $v = true;
        // $form->add((new CheckboxInput('notify'))->setLabel('__Send email notification__')->setValue($v));

        $values = $form->grab($request);
        if ($values) {
            if ($form->isValid()) {
                $res = ($this->actionUnassignVolunteer)($task->getId(), $volunteer->getId());
                if ($res->isOk()) {
                    $ret = '__Volunteer unassigned__';

                    $ret = $html->main($ret);
                    $ret->title = $html->el('h1', '__Volunteer unassigned__');
                    $ret->afterContentMenu->add($html->link('admin/sheet/' . $sheetId, [], '__View sheet__'));

                    $signupCriteria = (new SignupCriteria())->whereTaskId('=', $taskId);
                    $countSignups = $this->signupFinder->count($signupCriteria);
                    if ($countSignups < $task->getSpots()){
                        $ret->afterContentMenu
                            ->add($html->link('admin/sheet/' . $sheetId . '/signup/task/' . $taskId . '/assign', [], '__Assign another volunteer__'), 10)
                            ;
                    }

                    return $ret;
                }

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

        $ret = $html->blockList()
            ->add($ret)
            ->add($html->dl(null, $html->button('__Unassign volunteer__')))
            ;

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

        $ret = $html->main($ret);
        $ret->title = $html->el('h1', '__Unassign volunteer__');

        return $ret;
    }
}
