<?php

namespace Internetgalerie\IgDatapoolFe\ViewHelpers\Form;

use Internetgalerie\IgDatapoolFe\Services\LangService;
use TYPO3\CMS\Core\Page\AssetCollector;
use Internetgalerie\IgCoreViewhelpers\ViewHelpers\Form\CheckboxViewHelper;

/**
 * View Helper which creates a checkbox (<input type="checkbox">) as a switch button (jQUery UI).
 *
 * = Examples =
 *
 * <code title="Examples">
 * <dp:form.switchButton name="mySwitchButton" value="someValue" />
 * <dp:form.switchButton name="mySwitchButton" value="someValue" checked="{object.value} == 5" />
 * <dp:form.switchButton property="interests" value="TYPO3" />
 * </code>
 */
class SwitchButtonViewHelper extends CheckboxViewHelper
{
    public function __construct(
        protected readonly AssetCollector $assetCollector
    ) {
        parent::__construct();
    }
 
    public function initializeArguments(): void
    {
        parent::initializeArguments();

        $this->registerArgument('onLabel', 'string', 'The Label for on', false, LangService::ll('field.switch.on'));
        $this->registerArgument('offLabel', 'string', 'The Label for off', false, LangService::ll('field.switch.off'));
    }

    public function render(): string
    {
        $this->assetCollector->addStyleSheet('ig-datapool-fe-switch-button', 'EXT:ig_datapool_fe/Resources/Public/Css/switchButton.css');
        $checkboxHtml = parent::render();
        return '
        <label class="dp-switch-wrapper">
            ' . $checkboxHtml . '
            ' . ' <span class="dp-switch-slider" 
                  data-on="' . htmlspecialchars((string)$this->arguments['onLabel']) . '" 
                  data-off="' . htmlspecialchars((string)$this->arguments['offLabel']) . '">
            </span>
        </label>';
    }
}
