<?php

namespace Signteer\Web\Admin\Page\Sheet;

use Plainwork\Http\Request;
use Plainwork\Http\Redirect;
use Plainwork\Ui\Html;
use Signteer\Core\Action\Sheet\UpdateSheet;
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;

final class ViewSheetPage
{
    public $action = UpdateSheet::class;
    public $finder = SheetFinder::class;
    public $taskFinder = TaskFinder::class;
    public $signupFinder = SignupFinder::class;

    public function breadcrumbTitle(Request $request)
    {
        $id = $request->getQueryParam('sheet');
        $m = $this->finder->get($id);
        if ($m) {
            $ret = $m->getTitle();

            return $ret;
        }
    }

    public function __invoke(Request $request)
    {
        $id = $request->getQueryParam('sheet');
        $m = $this->finder->get($id);
        if (!$m) {
            return (new Redirect('..'));
        }

        $html = new Html();

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

        $wpPostId = $m->getId();
        $permalink = get_permalink($wpPostId);
        $permalink = $html->link()->setFull($permalink)->setLabel($permalink)
            // ->withAttr('target', '_blank')
            ;
        $v = $html->card($permalink);
        $ret->add($html->dl('__Permalink__', $v));

        $v = $m->getTitle();
        $form->add($html->textInput('title')->setRequired()->setValue($v));
        $ret->add($html->dl('__Title__', $form->input('title')));

        $v = $m->getDescription();
        $form->add($html->textareaInput('description')->setValue($v));
        $ret->add($html->dl('__Description__', $form->input('description')));

        $values = $form->grab($request);
        if ($values) {
            if ($form->isValid()) {
                $res = ($this->action)($id, $values['title'], $values['description']);
                if ($res->isOk()) {
                    $id = $res->getReturn();

                    return (new Redirect('../' . $id . '/task'));
                }

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

        if ($err = $form->getErrors()) {
            $ret->add($html->dl('__Error__', $html->formErrors($err)));
        }
        $ret->add($html->dl(null, $html->button('__Save changes__')));

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

        $c = (new TaskCriteria())->whereSheetId('=', $id);
        $countTask = $this->taskFinder->count($c);

        $c = (new SignupCriteria())->whereSheetId('=', $id);
        $countSignup = $this->signupFinder->count($c);

        $ret = $html->main($ret);
        $ret->title = $html->el('h1', '__Edit sign-up sheet__');
        $ret->menu->add($html->link('./task', [], '__Manage tasks__', '(' . $countTask . ')'));
        $ret->menu->add($html->link('./signup', [], '__Manage sign-ups__', '(' . $countSignup . ')'));

        return $ret;
    }
}
