<?php

namespace Plainwork\Ui\Renderer\Form;

use Plainwork\Ui\Component\HtmlElement;
use Plainwork\Ui\Component\InlineList;
use Plainwork\Ui\Component\BlockList;
use Plainwork\Ui\Component\Form\RadioSetInput;
use Plainwork\Ui\Component\Form\RadioInput;

final class RadioSetInputRenderer
{
    public function __invoke(RadioSetInput $el)
    {
        $name = $el->getName();
        $value = $el->getValue();
        $isVertical = $el->isVertical();

        $ret = $isVertical ? (new BlockList()) : (new InlineList())->setSpacing(2);

        $options = $el->getOptions();
        foreach ($options as $k => $label) {
            $one = (new RadioInput($name))
                ->setLabel($label)
                // ->setHtmlValue($k)
                ->setValue($k)
                ;
            if ($k == $value) {
                // $one->setValue(true);
                $one->setOn(true);
            }
            $ret->add($one);
        }

        $ret = (new HtmlElement('fieldset', $ret));

        return $ret;
    }
}
