<?php

namespace Internetgalerie\IgDatapoolFe\ViewHelpers\Form\Tom;

use Internetgalerie\IgCoreViewhelpers\ViewHelpers\Form\SelectViewHelper as IgSelectViewHelper;
use Internetgalerie\IgDatapoolFe\ViewHelpers\Form\FormFieldTrait;
use TYPO3\CMS\Core\Page\AssetCollector;
use TYPO3\CMS\Core\Page\PageRenderer;

class SelectViewHelper extends IgSelectViewHelper
{
    use FormFieldTrait;

    protected static $jsLoaded = false;
    protected static $class = 'dp-tom-select';
    protected static $style = '';
    protected AssetCollector $assetCollector;

    public function injectAssetCollector(AssetCollector $assetCollector): void
    {
        $this->assetCollector = $assetCollector;
    }

    /**
     * Initialize arguments.
     *
     * @return void
     * @api
     */
    public function initializeArguments(): void
    {
        parent::initializeArguments();
        $this->registerArgument('default', 'mixed', 'Default Value');
        $this->registerArgument('placeholder', 'string', 'placeholder String', false, '');
        $this->registerArgument('closeOnSelect', 'boolean', 'placeholder String', false, true);
        $this->registerArgument('dropdownCssClass', 'string', 'placeholder String', false, '');
        $this->registerArgument('allowClear', 'boolean', 'display a clear button ("x" icon) to appear on the select box when a value is selected.', false, false);
    }

    /**
     * adds default JS/CSS for tom-select
     */
    protected function loadTomSelectAssets(): void
    {
        if (!static::$jsLoaded) {
            static::$jsLoaded = true;
            // Use pageRenderer for CSS
            $this->assetCollector->addStyleSheet('tom-select','EXT:ig_datapool_fe/Resources/Public/Css/tom-select.css');
            $this->assetCollector->addStyleSheet('tom-select-custom', 'EXT:ig_datapool_fe/Resources/Public/Css/tom-select-custom.css');
            // Use assetCollector for JS (avoids JS initialization errors)
            $this->assetCollector->addJavaScript('tom-select', 'EXT:ig_datapool_fe/Resources/Public/JavaScript/tom-select/tom-select.complete.min.js');
            $this->assetCollector->addJavaScript('tom-select-init', 'EXT:ig_datapool_fe/Resources/Public/JavaScript/tom-select/datapool-init.js');
        }
    }

    public function render(): string
    {
        $this->loadTomSelectAssets();

        if (isset($this->arguments['placeholder'])) {
            $this->tag->addAttribute('data-placeholder', $this->arguments['placeholder']);
        }

        if (isset($this->arguments['closeOnSelect'])) {
            $this->tag->addAttribute('data-close-after-select', $this->arguments['closeOnSelect'] ? 'true' : 'false');
        }

        if (isset($this->arguments['dropdownCssClass'])) {
            $this->tag->addAttribute('data-dropdown-class', $this->arguments['dropdownCssClass']);
        }

        if ($this->tag->hasAttribute('class')) {
            $this->tag->addAttribute('class', static::$class . ' ' . $this->tag->getAttribute('class'));
            $this->arguments['class'] = static::$class . ' ' . $this->tag->getAttribute('class');
        } else {
            $this->tag->addAttribute('class', static::$class);
            $this->arguments['class'] = static::$class;
        }

        if ($this->hasArgument('placeholder') && $this->arguments['allowClear']) {
            $this->tag->addAttribute('data-clear-button', 'true');
        }
        if (static::$style) {
            $this->tag->addAttribute('style', static::$style);
        }
        return parent::render();
    }
}
