<?php
/**
 * Select.php
 *
 * @since      1.0.0
 * @package    WcElectronInvoice\Admin\Settings\Fields
 * @author     alfiopiccione <alfio.piccione@gmail.com>

 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2
 *
 * Copyright (C) 2025 POP
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace WcElectronInvoice\Admin\Settings\Fields;

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

use WcElectronInvoice\Admin\Settings\OptionFields;
use WcElectronInvoice\Admin\Settings\OptionPage;

/**
 * Class Radio
 *
 * @since  1.0.0
 * @author alfiopiccione <alfio.piccione@gmail.com>
 */
class Select extends Text
{
    /**
     * Allow Html
     *
     * @since 1.0.0
     *
     * @var array
     */
    private $allowHtml = array();

    /**
     * Radio constructor.
     *
     * @param              $args
     * @param OptionFields $fields
     * @param OptionPage   $page
     *
     * @since 1.0.0
     *
     */
    public function __construct($args, OptionFields $fields, OptionPage $page)
    {
        $args = wp_parse_args($args, array(
            'options' => array(),
            'type'    => 'select',
            'attrs'   => array(),
        ));

        $this->allowHtml = array(
            'label'  => array(
                'for' => true,
            ),
            'span'   => array(
                'class' => true,
            ),
            'code'   => true,
            'select' => array(
                'class'    => true,
                'title'    => true,
                'name'     => true,
                'id'       => true,
                'disabled' => true,
                'required' => true,
                'readonly' => true,
            ),
            'option' => array(
                'class'    => true,
                'value'    => true,
                'selected' => true,
                'disabled' => true,
            ),
            'p'      => true,
            'br'     => true,
            'small'  => true,
            'ul'     => true,
            'li'     => true,
        );

        parent::__construct($args, $fields, $page);
    }

    /**
     * Escape
     *
     * @param null $value
     *
     * @return string string The escaped value of this type
     * @since  1.0.0
     *
     */
    public function escape($value = null)
    {
        $value = $value ?: $this->getValue();

        return esc_html($value);
    }

    /**
     * Get Saved Value
     *
     * @return string
     * @since  1.0.0
     *
     */
    private function getValue()
    {
        $options  = $this->fields->options;
        $newValue = isset($options[$this->args['name']]) ? $options[$this->args['name']] : '';

        return esc_html($newValue);
    }

    /**
     * Get Attrs
     *
     * @param $attrs array The attributes Key|Value
     *
     * @return string
     * @since  1.0.0
     *
     */
    public function getAttr($attrs)
    {
        $att = '';
        if (! empty($attrs)) {
            foreach ($attrs as $key => $value) {
                $att .= "{$key}='{$value}' ";
            }
        }

        return $att;
    }

    /**
     * Output Field
     *
     * @since 1.0.0
     */
    public function field()
    {
        $labelBefore = '';
        $labelAfter  = '';
        $description = '';

        if (empty($this->args['options'])) {
            return '';
        }

        $options = array();
        $c       = 1;

        if (isset($this->args['description'])) {
            $description = sprintf('<small>%s</small>',
                esc_html__($this->args['description'], WC_EL_INV_TEXTDOMAIN));
        }

        if (isset($this->args['label'])) {
            $labelBefore = sprintf('<label for="%s"><span class="label-select">%s</span><br>',
                esc_attr($this->args['id']),
                esc_attr($this->args['label']));
            $labelAfter  = '</label>';
        }

        foreach ($this->args['options'] as $value => $name) {
            $optionName = esc_html__($name, WC_EL_INV_TEXTDOMAIN);
            $options[]  = sprintf(
                '<option value="%1$s" %2$s>%3$s</option>',
                $this->escape($value),
                selected((int)esc_attr($value), $this->escape(), false),
                $optionName
            );

            ++$c;
        }

        $output = sprintf('%1$s<select id="%2$s" name="%3$s[%4$s]" %5$s>%6$s</select>%7$s%8$s',
            $labelBefore,
            esc_attr($this->args['id']),
            $this->page->optionsName,
            esc_attr($this->args['name']),
            $this->getAttr($this->args['attrs']),
            implode("", $options),
            $labelAfter,
            $description,
        );

        /**
         * Output Filter
         *
         * @param string $output The output of the input type.
         * @param Select The instance class
         *
         * @since 1.0.0
         *
         */
        $output = apply_filters('wc_el_inv-input_select_output', $output, $this);

        /**
         * Before input hook
         *
         * @since 1.0.0
         */
        do_action('wc_el_inv-input_select_before_input_action', $this->args);

        echo wp_kses($output, $this->allowHtml);

        /**
         * After input hook
         *
         * @since 1.0.0
         */
        do_action('wc_el_inv-input_select_after_input_action', $this->args);
    }
}
