<?php
/**
 * TextArea.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 TextArea
 *
 * @since  1.0.0
 * @author alfiopiccione <alfio.piccione@gmail.com>
 */
class TextArea extends Text
{
    /**
     * Allow Html
     *
     * @since 1.0.0
     *
     * @var array
     */
    private $allowHtml = array();

    /**
     * TextArea 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(
            'type'       => 'textarea',
            'label_html' => '',
        ));

        $this->allowHtml = array(
            'label'    => array(
                'for' => true,
            ),
            'img'      => array(
                'src'    => true,
                'class'  => true,
                'id'     => true,
                'title'  => true,
                'alt'    => true,
                'width'  => true,
                'height' => true,
            ),
            'span'     => array(
                'class' => true,
            ),
            'code'     => true,
            'textarea' => array(
                'class'       => true,
                'title'       => true,
                'placeholder' => true,
                'name'        => true,
                'id'          => true,
                'disabled'    => true,
                'required'    => true,
                'readonly'    => true,
            ),
            'p'        => true,
            'br'       => true,
            'small'    => true,
            'ul'       => true,
            'li'       => true,
            'strong'   => true,
        );

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

    /**
     * Escape
     *
     * @param $defaultValue
     *
     * @return string The escaped value of this type
     * @since  1.0.0
     */
    public function escape($defaultValue = '')
    {
        if (isset($this->args['value']) && ! empty($this->args['value'])) {
            $defaultValue = $this->args['value'];
        }

        $value = isset($this->fields->options[$this->args['name']]) ? $this->fields->options[$this->args['name']] : '';

        // Init value default
        if (! $value || '' === $value) {
            $value = $defaultValue;
        }

        $allowedHtmlActive = $this->args['allowed_html'] ?? true;
        if ($allowedHtmlActive) {
            $allowedHtml = array(
                'p'      => true,
                'br'     => true,
                'small'  => true,
                'ul'     => true,
                'li'     => true,
                'strong' => true,
            );
        } else {
            $allowedHtml = array();
        }

        return wp_kses($value, $allowedHtml);
    }

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

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

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

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

        $output .= sprintf(
            '%1$s<textarea class="%2$s" placeholder="%3$s" name="%4$s[%5$s]" id="%6$s" %7$s>%8$s</textarea>%9$s %10$s',
            $labelBefore,
            esc_attr($this->args['class']),
            esc_attr($this->args['placeholder']),
            $this->page->optionsName,
            esc_attr($this->args['name']),
            esc_attr($this->args['id']),
            $this->getAttr($this->args['attrs']),
            $this->escape(),
            $labelAfter,
            $description
        );

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

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

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

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