<?php
/**
 * License.php
 *
 * @since      1.1.0
 * @package    WcElectronInvoice
 * @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;

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

use WcElectronInvoice\Admin\Settings\OptionPage;

/**
 * Class License
 *
 * @since  1.1.0
 * @author alfiopiccione <alfio.piccione@gmail.com>
 */
final class Connection
{
    /**
     * Plugin License check on "admin_init"
     *
     * @since  1.0.0
     */
    public static function check() {
      $page      = \WcElectronInvoice\Functions\filterInput( $_GET, 'page', FILTER_UNSAFE_RAW );
      $tab       = \WcElectronInvoice\Functions\filterInput( $_GET, 'tab', FILTER_UNSAFE_RAW );
      $uidOption = (int) get_option( 'pop_account_user_id', false );
  
      if ( ! is_admin() ) {
        return;
      }
  
      $isPluginSettingsPage = ( 'wc_el_inv-options-page' === $page );
      $isGeneralContext     = $isPluginSettingsPage && ( ! $tab || 'general' === $tab );

      // Maybe show a "configure account" message, except on the settings general/wizard views
      if ( ! $uidOption && 'wizard' !== $tab && ! $isGeneralContext) {
        add_action( 'wc_el_inv-plugin_header_notice', function () {
          // Not allowed
          self::configurationAccountMessage();
        } );
      }
  
      // Set the top-right license status to "not connected"
      if ( ! $uidOption || ! OptionPage::getK() ) {
        add_action( 'wc_el_inv-plugin_header_license_info', function () {
          ?>
                <div class="license_status_info invalid">
                  <span class="dashicons dashicons-no"></span>
            <?php esc_html_e(
              'Not connected',
              WC_EL_INV_TEXTDOMAIN
            ); ?>
                </div>
          <?php
        } );
      } else {
        // Set the top-right license status to the user plan
        $level = \WcElectronInvoice\Wizard::popGetUserLevel();
        add_action( 'wc_el_inv-plugin_header_license_info', function () use ( $level ) {
          ?>
                <div class="license_status_info valid">
                  <span class="dashicons dashicons-yes"></span>
                  <a href="https://popapi.io/mio-account/pop-account/">
            <?php echo sprintf( esc_html__( '%s Plan', WC_EL_INV_TEXTDOMAIN ), ucfirst( $level ) ); ?>
                  </a>
                </div>
          <?php
        } );
      }
    }

    /**
     * Configure Account message
     *
     * @since 1.1.0
     *
     */
    private static function configurationAccountMessage()
    { ?>
        <div class="pop-notice notice-error">
            <?php echo sprintf('<p><span class="dashicons dashicons-no"></span> %1$s</p><p><a class="button button-primary" href="%2$s">%3$s</a></p>',
                esc_html__('You need an account to begin using POP. Click the button to start the account configuration wizard.',
                    WC_EL_INV_TEXTDOMAIN),
                esc_url(admin_url("admin.php?page=wc_el_inv-options-page&tab=wizard&wizard=init")),
                esc_html__('Configure Account', WC_EL_INV_TEXTDOMAIN)
            ); ?>
        </div>
        <?php
    }
}
