<?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\PageRenderer;
use TYPO3\CMS\Core\Page\AssetCollector;

class SelectViewHelper extends IgSelectViewHelper
{
    use FormFieldTrait;

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

    // remove if v12 is dropped
    protected PageRenderer $pageRenderer;

    public function injectPageRenderer(PageRenderer $pageRenderer): void
    {
        $this->pageRenderer = $pageRenderer;
    }

    public function __construct(
        protected readonly AssetCollector $assetCollector,
    ) {
        parent::__construct();
    }

    /**
     * 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-css','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-js', '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');
            $this->pageRenderer->loadJavaScriptModule('@dp/elements/tomSelect.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();
    }
}
