<?php
/**
 * OptionFields.php
 *
 * @since      1.0.0
 * @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;

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

use WcElectronInvoice\Plugin;

/**
 * Class OptionFields
 *
 * @since  1.0.0
 * @author alfiopiccione <alfio.piccione@gmail.com>
 */
abstract class OptionFields
{
    /**
     * Holds the values to be used in the fields callbacks
     *
     * @since 1.0.0
     *
     * @var array
     */
    public $options;

    /**
     * Contains the arguments for add fields settings
     *
     * @since 1.0.0
     *
     * @var array
     */
    public $sectionArgs = array();

    /**
     * Contains the arguments for add fields settings
     *
     * @since 1.0.0
     *
     * @var array
     */
    public $fieldsArgs = array();

    /**
     * Set section and fields
     *
     * @since 1.0.0
     */
    protected function sectionFields()
    {
        // Fire hook for woocommerce integration
        include_once Plugin::getPluginDirPath('/inc/wc/settings/pageSettingsFields.php');
        // Default page settings fields
        include_once Plugin::getPluginDirPath('/inc/settings/pageSettingsFields.php');
    }

    /**
     * Add settings section and setting fields in an array
     *
     * @return array|bool $settings_and_fields The settings section and setting fields in an array
     * @since 1.0.0
     *
     */
    public function sectionAndFieldsOptions()
    {
        $sectionsAndFields = array();

        // Settings section arguments
        if (! empty($this->sectionArgs)) {
            foreach ($this->sectionArgs as $key => $section) {
                $sectionsAndFields['section'][$key] = $section;
            }

            // Settings fields arguments
            $sectionsAndFields['fields'] = $this->fieldsArgs;
            if (empty($sectionsAndFields)) {
                return false;
            }
        }

        return $sectionsAndFields;
    }

    /**
     * Print the Section text settings
     *
     * @since 1.0.0
     */
    public function sectionSettingsGeneralDescription()
    {
        $key = OptionPage::getK();
        if (\WcElectronInvoice\Functions\noKey($key)) {
            return '';
        }
    }

    /**
     * Print the Section text settings
     *
     * @since 1.0.0
     */
    public function fieldsVatRules()
    {
        if('IT' === \WcElectronInvoice\WooCommerce\Fields\GeneralFields::getGeneralInvoiceOptionCountryState()) {
            echo sprintf('<div class="pop-notice notice-info"><p><span class="dashicons dashicons-info"></span> <b>%s</b> %s</p><ol><li>%s</li><li>%s</li></ol></div>',
                esc_html__('Note:'),
                esc_html__('Per i clienti con residenza in Italia (IT), i seguenti campi sono sempre obbligatori:'),
                esc_html__('Azienda o Persona Fisica titolare di Partita IVA → Partita IVA, Codice Fiscale, PEC, Codice Destinatario.'),
                esc_html__('Privato → Codice Fiscale.'));
        }
    }

    /**
     * changelog page
     *
     * @return void
     */
    public function changelog()
    {
        // Close current table
        $output = '</tr></tbody></table>';
        $output .= '<style>.form-table{display:none;}</style>';
        $output .= '<!-- changelog_content --><div class="changelog_content">';

        // Current version
        $output .= '<ul>';
        $output .= sprintf('<li><strong>%s:</strong> %s</li>',
            esc_html__('Current version', WC_EL_INV_TEXTDOMAIN),
            esc_attr(WC_EL_INV_VERSION)
        );

        $output .= '<hr>';
        $output .= '<p><strong>Changelog completo consultabile <a target="_blank" href="' . esc_url(WC_EL_INV_URL) . 'changelog.txt">qui</a></strong></p>';
        // Current version

        $output .= '</div><!--/ changelog_content -->';

        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        echo $output;
    }
}
