<?php
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

if (defined('DOING_AJAX')) {
    if (isset($_REQUEST['action'])) {
//do not recalculate refund amounts when we are in order backend
        if ($_REQUEST['action'] == 'woocommerce_refund_line_items') {
            return;
        }

        if (isset($_REQUEST['order_id']) AND $_REQUEST['action'] == 'woocommerce_load_order_items') {
            return;
        }
    }
}

define('NBTWCCS_VERSION', '2.2.3');
define('NBTWCCS_MIN_WOOCOMMERCE', '2.6');
define('NBTWCCS_PATH', plugin_dir_path(__FILE__));
define('NBTWCCS_LINK', plugin_dir_url(__FILE__));


//classes
include_once NBTWCCS_PATH . 'inc/storage.php';
include_once NBTWCCS_PATH . 'inc/cron.php';
include_once NBTWCCS_PATH . 'inc/fixed/fixed_amount.php';
include_once NBTWCCS_PATH . 'inc/fixed/fixed_price.php';


class NBT_Solutions_Wc_Currency_Switcher {

    private $support_time = 1519919054; // Date of the old < 3.3 version support
    private $default_woo_version = 2.9;
    private $actualized = 0.0;
    private $version_key = "nbtwccs_woo_version";
    private $_nbtwccs = null;

    public function __construct() {
        if (time() > $this->support_time) {
            $this->default_woo_version = 3.3;
        }
        $this->actualized = floatval(get_option($this->version_key, $this->default_woo_version));
    }

    public function update_version() {
        if (defined('WOOCOMMERCE_VERSION') AND ( $this->actualized !== floatval(WOOCOMMERCE_VERSION))) {
            update_option('nbtwccs_woo_version', WOOCOMMERCE_VERSION);
        }
    }

    public function get_actual_obj() {
        if ($this->_nbtwccs != null) {
            return $this->_nbtwccs;
        }
        
        if (version_compare($this->actualized, '3.3', '<')) {
            include_once NBTWCCS_PATH . 'inc/nbtwccs_before_33.php';
           
        } else {
            include_once NBTWCCS_PATH . 'inc/nbtwccs_after_33.php';
            include_once NBTWCCS_PATH . 'inc/fixed/fixed_coupon.php';
            include_once NBTWCCS_PATH . 'inc/fixed/fixed_shipping.php';
            include_once NBTWCCS_PATH . 'inc/fixed/fixed_shipping_free.php';

        }
        $this->_nbtwccs = new NBTWCCS();
        return $this->_nbtwccs;
    }

}

if (isset($_GET['P3_NOCACHE'])) {
    //stupid trick for that who believes in P3
    return;
}

$NBT_Solutions_Wc_Currency_Switcher = new NBT_Solutions_Wc_Currency_Switcher();

$NBTWCCS = $NBT_Solutions_Wc_Currency_Switcher->get_actual_obj();
$GLOBALS['NBTWCCS'] = $NBTWCCS;
add_action('init', array($NBTWCCS, 'init'), 1);

