<?php

namespace Internetgalerie\IgDatapoolFe\ViewHelpers\Form\Select;

use Internetgalerie\IgDatapoolFe\ViewHelpers\Form\SelectViewHelper;
use TYPO3\CMS\Core\Page\AssetCollector;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Core\Page\PageRenderer;

class CheckboxViewHelper extends SelectViewHelper
{
    // 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();
    }

    public function initializeArguments(): void
    {
        parent::initializeArguments();
        $this->registerArgument('columns', 'int', 'columns', false, 1);
        $this->registerArgument('classes', 'string', 'classes', false, '');
        $this->registerArgument('selectAll', 'int', 'selectAll', false, false);

        //$this->registerArgument('search', 'int', 'search', false, false);
        //$this->registerArgument('textsSearch', 'string', 'textsSearch', false, '');

        $this->registerArgument('filter', 'int', 'filter', false, false);
        $this->registerArgument('textsFilterPlaceholder', 'string', 'textsFilterPlaceholder', false, '');
        $this->registerArgument('textsFilterLabel', 'string', 'textsFilterLabel', false, '');

        $this->registerArgument('textsPlaceholder', 'string', 'textsPlaceholder', false, '');
        $this->registerArgument('textsSelectAll', 'string', 'textsSelectAll', false, '');
        $this->registerArgument('textsUnselectAll', 'string', 'textsUnselectAll', false, '');
        $this->registerArgument('textsClose', 'string', 'textsClose', false);

        $this->registerArgument('buttonWidth', 'string', 'buttonWidth', false, null);
        //$this->registerArgument('selectedText', 'string', 'selectedText', false, '%s of %s selected');
        $this->registerArgument('textCountSelected', 'string', 'textCountSelected', false, '#o of #t selected');
        $this->registerArgument('textAllSelected', 'string', 'textAllSelected', false, null);
        $this->registerArgument('selectedList', 'int', 'selectedList', false, 0);
        $this->registerArgument('selectedListSeparator', 'string', 'selectedListSeparator', false, ', ');
        $this->registerArgument('maxSelected', 'int', 'maxSelected', false, null);
        $this->registerArgument('textsMaxSelected', 'string', 'textMaxSelected', false, null);
        $this->registerArgument('menuWidth', 'string', 'menuWidth', false, null);
        $this->registerArgument('menuHeight', 'string', 'menuWidth', false, null);
        $this->registerArgument('header', 'string', 'headers: checkAll, uncheckAll, flipAll, collapseAll, or expandAll', false, 'checkAll,uncheckAll');
    }


    /**
     * Render the tag.
     *
     * @return string rendered tag.
     * @api
     */
    public function render(): string
    {

        
        //$this->assetCollector->addJavaScript('ig-datapool-fe-multiple-select-vanilla', 'EXT:ig_datapool_fe/Resources/Public/JavaScript/multiple-select-vanilla/index.min.js', ['type' => 'module']);
        // $importMap = [
        //     'imports' => [
        //         'multiple-select-vanilla' => PathUtility::getPublicResourceWebPath('EXT:ig_datapool_fe/Resources/Public/JavaScript/multiple-select-vanilla/index.min.js')
        //     ]
        // ];
        // $json = json_encode($importMap, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
        // $html = '<script type="importmap">' . $json . '</script>';

        //$pageRenderer->loadJavaScriptModule('@dp/multiple-select-vanilla');
        //$pageRenderer->addHeaderData($html);
        // fuegt am ende ein ; an
        //            $assetCollector->addInlineJavaScript('multiple-select-vanilla-importmap', $json .'</script><script>', ['type' => 'importmap'], ['priority' => true]);
        /*
        $relativePath = 'EXT:ig_datapool_fe/Resources/Public/JavaScript/multiple-select-vanilla/init.js';
        $absolutePath = GeneralUtility::getFileAbsFileName($relativePath);
        if (file_exists($absolutePath)) {
            $jsContent = file_get_contents($absolutePath);
            $this->assetCollector->addInlineJavaScript('ig-datapool-fe-multiple-select-vanilla-init', $jsContent, ['type' => 'module']);
        }
        */
        $this->pageRenderer->loadJavaScriptModule('@dp/elements/multiple-select-vanilla.js');
        //        $this->assetCollector->addJavaScript('ig-datapool-fe-multiple-select-vanilla-init', 'EXT:ig_datapool_fe/Resources/Public/JavaScript/multiple-select-vanilla/init.js', ['type' => 'module', 'footer' => true]);
        $this->assetCollector->addStyleSheet('ig-datapool-fe-multiple-select-vanilla', 'EXT:ig_datapool_fe/Resources/Public/JavaScript/multiple-select-vanilla/multiple-select.min.css');
        $this->assetCollector->addStyleSheet('ig-datapool-fe-multiple-select-vanilla-custom', 'EXT:ig_datapool_fe/Resources/Public/JavaScript/multiple-select-vanilla/multiple-select-custom.css');
        $class= 'dp-select-checkbox';


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

        //$this->tag->addAttribute('style', 'width: 100%;');
        //$this->tag->addAttribute('data-columns', (int)$this->arguments['columns']);

        //$this->tag->addAttribute('data-search', (int)$this->arguments['search']);
        //$this->tag->addAttribute('data-texts-search', $this->arguments['textsSearch']);
        $classes =$this->arguments['classes'];
        if ($this->arguments['columns']>1) {
            $classes = trim($classes . ' ' . 'columns-multi columns-' . intval($this->arguments['columns']));
        }
        $selectedValues = $this->getSelectedValue();
        if (!empty($selectedValues)) {
            $classes = trim((string) $classes) . ' selected';
        }
        $this->tag->addAttribute('data-classes', $classes);


        $this->tag->addAttribute('data-texts-placeholder', $this->arguments['textsPlaceholder']);
        $this->tag->addAttribute('data-select-all', (int)$this->arguments['selectAll']);
        $this->tag->addAttribute('data-max-selected', $this->arguments['maxSelected']);
        $this->tag->addAttribute('data-texts-max-selected', $this->arguments['textsMaxSelected']);
        $this->tag->addAttribute('data-texts-select-all', $this->arguments['textsSelectAll']);
        $this->tag->addAttribute('data-texts-unselect-all', $this->arguments['textsUnselectAll']);
        $this->tag->addAttribute('data-texts-close', $this->arguments['textsClose']);

        $this->tag->addAttribute('data-header', json_encode(explode(',', (string) $this->arguments['header'])));
        $this->tag->addAttribute('data-text-count-selected', $this->arguments['textCountSelected']);
        $this->tag->addAttribute('data-text-all-selected', $this->arguments['textAllSelected']);
        $this->tag->addAttribute('data-selected-list', $this->arguments['selectedList']);
        $this->tag->addAttribute('data-selected-list-separator', $this->arguments['selectedListSeparator']);
        $this->tag->addAttribute('data-button-width', $this->arguments['buttonWidth']);
        $this->tag->addAttribute('data-menu-width', $this->arguments['menuWidth']);
        $this->tag->addAttribute('data-menu-height', $this->arguments['menuHeight']);
        if ($this->arguments['filter']) {
            $this->tag->addAttribute('data-filter', $this->arguments['filter']);
            $this->tag->addAttribute('data-texts-filter-placeholder', $this->arguments['textsFilterPlaceholder']);
            $this->tag->addAttribute('data-texts-filter-label', $this->arguments['textsFilterLabel']);
        }
        return parent::render();
    }
}
