<?php
/**
 * KeyFields.php
 *
 * @since      1.0.0
 * @package    WcElectronInvoice\Admin\Settings
 * @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 KeyField
 *
 * @since  1.0.0
 * @author alfiopiccione <alfio.piccione@gmail.com>
 */
final class KeyField extends Text
{
    /**
     * Key args
     *
     * @since 1.0.0
     *
     * @var array
     */
    public static $keyArgs;

    /**
     * Key option
     *
     * @since 1.0.0
     *
     * @var
     */
    private static $keyOptions;

    /**
     * KeyField constructor.
     *
     * @param              $args
     * @param OptionFields $fields
     * @param OptionPage   $page
     *
     * @since 1.0.0
     *
     */
    public function __construct($args, OptionFields $fields, OptionPage $page)
    {
        self::$keyArgs    = $args;
        self::$keyOptions = $fields;

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

    /**
     * Secret Key
     *
     * @since 1.0.0
     */
    public static function secretKey()
    {
        $output       = '';
        $secretApiKey = OptionPage::getK();
        $data         = get_option('wc_el_inv-license_check');

        if ($secretApiKey &&
            isset(self::$keyOptions->options['wc_el_inv-settings-key']) &&
            '' !== self::$keyOptions->options['wc_el_inv-settings-key']
        ) {
            $value = $secretApiKey;
        } else {
            $value = esc_html__('No key');
        }

        if(! is_array($data) && ! empty($data)) {
            $output .= "<div class='license_status'>";
            $output .= sprintf('<code>%s</code>', $value);
            $output .= "</div>";
        } else {
            $output .= "<div class='license_status'>";
            $output .= sprintf('<code>%s</code>', $value);
            if (! empty($data) && $secretApiKey) {
                $output .= sprintf("<b>%s</b>", esc_html__('License valid for:', WC_EL_INV_TEXTDOMAIN));
                $output .= '<ul>';
                foreach ($data as $items) {
                    foreach ($items as $siteURL => $item) {
                        $exp    = date_i18n('d/m/Y H:i:s', $item->license_expired_timestamp);
                        $output .= sprintf("<li><b>%s:</b> %s<br><b>%s:</b> %s<br><b>%s:</b> %s</li>",
                            __('Website', WC_EL_INV_TEXTDOMAIN),
                            esc_url($siteURL),
                            __('License expiration', WC_EL_INV_TEXTDOMAIN),
                            esc_attr($exp),
                            __('Support expiration', WC_EL_INV_TEXTDOMAIN),
                            esc_attr($exp)
                        );
                    }
                }
                $output .= '</ul>';
            }
            $output .= "</div>";
        }

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