<?php
/**
 * Radio.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 Radio 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'    => 'radio',
        ));

        $this->allowHtml = array(
            'label' => array(
                'for' => true,
            ),
            'span'  => array(
                'class' => true,
            ),
            'img'   => array(
                'src'    => true,
                'id'     => true,
                'title'  => true,
                'alt'    => true,
                'width'  => true,
                'height' => true,
            ),
            'code'  => true,
            'input' => array(
                'class'    => true,
                'title'    => true,
                'type'     => true,
                'id'       => true,
                'name'     => true,
                'value'    => true,
                'checked'  => true,
                'disabled' => true,
                'required' => 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);
    }

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

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

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

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

        foreach ($this->args['options'] as $value => $label) {
            $labelString = __($label, WC_EL_INV_TEXTDOMAIN);
            if (isset($this->args['label'])) {
                $labelBefore = sprintf('<label for="%s">', esc_attr($this->args['id'] . '-' . $c));
                $labelAfter  = sprintf('<span class="label-radio">%s</span></label>', $labelString);
            }

            // Set default value
            if (! $this->getValue() && $this->args['default'] === $value) {
                $currentValue = $this->args['default'];
            } else {
                $currentValue = $this->escape();
            }

            $radios[] = sprintf(
                '<li>%1$s <input type="%2$s" name="%3$s[%4$s]" id="%5$s" value="%6$s" %7$s class="%8$s"/>%9$s</li>',
                $labelBefore,
                sanitize_key($this->args['type']),
                $this->page->optionsName,
                esc_attr($this->args['name']),
                esc_attr($this->args['id'] . '-' . $c),
                $this->escape($value),
                checked($currentValue, $this->escape($value), false),
                esc_attr($this->args['class']),
                $labelAfter
            );

            ++$c;

            $output = '<ul>' . implode("\n", $radios) . '</ul>' . $description;
        }

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

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

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

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