<?php

/**
 * @package         Tassos Code Snippets
 * @version         1.0.1-RC2 Free
 *
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            https://www.tassos.gr
 * @copyright       Copyright © 2026 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
 */

namespace Tassos\CodeSnippets\Administrator\Field;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Form\Field\ListField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Tassos\CodeSnippets\Administrator\Helper\SnippetsHelper;

/**
 * Snippet Type field with refresh support.
 */
class SnippetTypesField extends ListField
{
    /**
     * The form field type.
     *
     * @var    string
     */
    protected $type = 'SnippetTypes';

    /**
     * Method to get the field input markup for a generic list.
     *
     * @return  string  The field input markup.
     */
    protected function getInput()
    {
        $html = [];
        $attr = '';

        // Initialize some field attributes.
        $attr .= !empty($this->size) ? ' size="' . $this->size . '"' : '';
        $attr .= $this->multiple ? ' multiple' : '';
        $attr .= $this->autofocus ? ' autofocus' : '';

        // Check if refresh is enabled
        $refreshPage    = (bool) $this->element['refresh-enabled'];
        $refreshCatId   = (string) $this->element['refresh-cat-id'];
        $refreshSection = (string) $this->element['refresh-section'];

        // Handle refresh page functionality
        if ($refreshPage) {
            $attr .= ' data-refresh-catid="' . $refreshCatId . '"';
            $attr .= ' data-refresh-section="' . $refreshSection . '"';
            $attr .= ' onchange="Joomla.categoryHasChanged(this)"';

            Factory::getApplication()->getDocument()->getWebAssetManager()
                ->registerAndUseScript('field.category-change', 'layouts/joomla/form/field/category-change.min.js', [], ['defer' => true], ['core'])
                ->useScript('webcomponent.core-loader');

            // Pass the element id to the javascript
            Factory::getApplication()->getDocument()->addScriptOptions('category-change', $this->id);
        } elseif ($this->onchange) {
            $attr .= ' onchange="' . $this->onchange . '"';
        }

        // Add class attribute
        $attr .= !empty($this->class) ? ' class="' . $this->class . '"' : ' class="form-select"';

        if ($this->required)
        {
            $attr .= ' required';
        }

        if ($this->readonly)
        {
            $attr .= ' disabled';
            $html[] = '<input type="hidden" name="' . $this->name . '" value="' . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '">';
        }

        // Create a regular list
        $html[] = HTMLHelper::_('select.genericlist', $this->getOptions(), $this->name, trim($attr), 'value', 'text', $this->value, $this->id);

        return implode("\n", $html);
    }

    /**
     * Method to get the field options.
     *
     * @return  array  The field option objects.
     */
    protected function getOptions()
    {
        $options = [];

        foreach (SnippetsHelper::getSnippetTypes() as $type)
        {
            $options[] = HTMLHelper::_('select.option', $type, Text::_(SnippetsHelper::getTypeLabelKey($type)));
        }

        // Merge any additional options in the XML definition.
        return array_merge(parent::getOptions(), $options);
    }
}