<?php
/**
 * ADDON - POP to Aruba Fatturazione Elettronica
 */

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

// Mode (dev or prod)
if (! defined('WP_FATT_ARUBA')) {
    define('WP_FATT_ARUBA', 'prod');
}

// Active Log
if (! defined('WP_FATT_ARUBA_LOG')) {
    define('WP_FATT_ARUBA_LOG', false);
}

// Define global plugin constant.
define('WP_FATT_ARUBA_NAME', 'POP to Aruba');
define('WP_FATT_ARUBA_TEXTDOMAIN', 'waf');
define('WP_FATT_ARUBA_VERSION', '1.4.10');
define('WP_FATT_ARUBA_API', get_option('waf_api_url'));

if (! defined('WP_FATT_ARUBA_API_TYPE')) {
    // demo or live
    define('WP_FATT_ARUBA_API_TYPE', 'live');
}

// Plugin DIR
define('WP_FATT_ARUBA_PLUGIN_DIR', basename(plugin_dir_path(__FILE__)));
// Dirs
define('WP_FATT_ARUBA_DIR', plugin_dir_path(__FILE__));
// Uri
define('WP_FATT_ARUBA_URL', plugin_dir_url(__FILE__));

if ('demo' === WP_FATT_ARUBA_API_TYPE) {
    // used for development only
    define('OAUTH_URL', 'https://demoauth.fatturazioneelettronica.aruba.it');
    define('WS_URL', 'https://demows.fatturazioneelettronica.aruba.it');
} else {
    define('OAUTH_URL', 'https://auth.fatturazioneelettronica.aruba.it');
    define('WS_URL', 'https://ws.fatturazioneelettronica.aruba.it');
}

// Base Requirements.
require_once untrailingslashit(WP_FATT_ARUBA_DIR . 'src/Plugin.php');
require_once untrailingslashit(WP_FATT_ARUBA_DIR . 'requires.php');

// Register the activation and deactivation hook.
register_activation_hook(__FILE__, array('WooPoPToFattureAruba\\Activate', 'activate'));
register_deactivation_hook(__FILE__, array('WooPoPToFattureAruba\\Deactivate', 'deactivate'));

// Init
$bootstrap = function () {

    // Prevent init plugin in other ajax action
    if (! is_admin() && defined('DOING_AJAX') && DOING_AJAX) {
        return;
    }

    if (method_exists(\WooPoPToFattureAruba\Plugin::class, 'load_plugin_textdomain')) {
        // Load Localisation files.
        \WooPoPToFattureAruba\Plugin::load_plugin_textdomain();
    }

    // Check for the dependency.
    if (\WooPoPToFattureAruba\Functions\isWooCommerceActive() &&
        class_exists('WcElectronInvoice\\Plugin')
    ) :
        $filters = array();

        // Global filter
        $filters = array_merge(
            $filters,
            include \WooPoPToFattureAruba\Plugin::getPluginDirPath('/inc/filters.php')
        );

        // Admin or Front filter
        if (is_admin()) {
            $filters = array_merge(
                $filters,
                include \WooPoPToFattureAruba\Plugin::getPluginDirPath('/inc/filtersAdmin.php')
            );
        } else {
            $filters = array_merge(
                $filters,
                include \WooPoPToFattureAruba\Plugin::getPluginDirPath('/inc/filtersFront.php')
            );
        }

        if (\WooPoPToFattureAruba\Functions\isAjaxRequest()) {
            $filters = array_merge(
                $filters,
                include \WooPoPToFattureAruba\Plugin::getPluginDirPath('/inc/filtersAjax.php')
            );
        }

        // Loader init.
        $init = new WooPoPToFattureAruba\Init(new WooPoPToFattureAruba\Loader(), $filters);
        $init->init();
    endif;
};

if (did_action('init')) {
    $bootstrap();
} else {
    add_action('init', $bootstrap, 20);
}
