<?php

namespace Signteer\Web\Admin\Page;

use Plainwork\Http\Request;
use Plainwork\Http\Redirect;
use Plainwork\DateTime\DateTimeFormatter;
use Plainwork\Ui\Html;
use Signteer\Core\Repo\SettingRepo;
use Signteer\Core\Action\Setting\UpdateSettings;

final class GeneralSettingPage
{
    public $repo = SettingRepo::class;
    public $action = UpdateSettings::class;
    public $dtf = DateTimeFormatter::class;

    public function __invoke(Request $request)
    {
        $html = new Html();

        $form = $this->form();

        $props = [
            'datetime_date_format', 'datetime_time_format', 'datetime_week_starts',
            // 'volunteer_reuse_if'
            ];
        $values = array_combine($props, array_map(fn($k) => $this->repo->get($k), $props));
        foreach ($values as $k => $v) {
            $form->input($k)->setValue($v);
        }

        $ret = $html->blockList()->setSpacing(3);

        $ret_ = $html->blockList();
        $ret_->add($html->el('h3', '__Date and time__'));
        $ret_->add($html->dl('__Date format__', $form->input('datetime_date_format')));
        $ret_->add($html->dl('__Time format__', $form->input('datetime_time_format')));
        $ret_->add($html->dl('__Week starts on__', $form->input('datetime_week_starts')));
        $ret->add($ret_);

        $values = $form->grab($request);
        if ($form->isValid()) {
            $values = array_intersect_key($values, array_combine($props, $props));

            $res = ($this->action)($values);

            if ($res->isOk()){
                $ret = '__Settings updated__';

                $ret = $html->main($ret);
                $ret->title = $html->el('h1', '__Action result__');
                $ret->afterContentMenu->add($html->link('.', [], '__View updated settings__'));
                $ret->afterContentMenu->add($html->link('..', [], '__View all settings__'));

                return $ret;
            }

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

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

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

        $ret = $html->main($ret);
        $ret->title = $html->el('h1', '__General settings__');

        return $ret;
    }

    private function form()
    {
        $html = new Html();

        $form = $html->form();

    /* time format */
        $option = [
            'j M Y', 'F j, Y', 'n/j/Y', 'm/d/Y', 'm-d-Y', 'm.d.Y', 'j/n/Y',
            'd/m/Y', 'd-m-Y', 'd.m.Y', 'Y/m/d', 'Y-m-d', 'Y.m.d'
            ];
        $option = array_flip($option);

        $try = new \DateTimeImmutable();
        foreach (array_keys($option) as $k) {
            $thisView = $try->format($k);
            $option[$k] = $thisView;
        }
        $form->add($html->selectInput('datetime_date_format')->setOptions($option));

    /* time format */
        $tries = ['2022-06-10 08:00', '2022-06-10 14:00', '2022-06-10 15:15'];
        $option = ['g:ia', 'g:i A', 'H:i', '12short', '12xshort', '24short'];
        $option = array_flip($option);
        foreach (array_keys($option) as $k) {
            $thisView = [];
            foreach ($tries as $try) {
                $try = new \DateTimeImmutable($try);
                $thisView[] = $this->dtf->formatTime($try, $k);
            }
            $thisView = join(', ', $thisView);
            $option[$k] = $thisView;
        }
        $form->add($html->selectInput('datetime_time_format')->setOptions($option));

    /* week starts */
        $option = [0,1,2,3,4,5,6];
        $option = array_flip($option);
        foreach (array_keys($option) as $k) {
            $option[$k] = $this->dtf->formatDayOfWeek($k);
        }
        $form->add($html->selectInput('datetime_week_starts')->setOptions($option));

    /* reuse volunteer */
        $options = [
            'email' => '__Email matches__',
            'phone' => '__Phone matches__',
            'none' => '__Do not reuse, always create new volunteer account__',
        ];
        // $form->add((new SelectInput('volunteer_reuse_if'))->setOptions($options));

        return $form;
    }
}
