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

/**
 * Plugin Name: WOOCS - WooCommerce Currency Switcher
 * Plugin URI: https://currency-switcher.com/
 * Author: realmag777
 *
 * Class WDP_WOOCS_Compatibility
 */
class WDP_WOOCS_Compatibility {
	const KEY = 'realmag777_woocs';
	const WDP_VERSION = '2.2.4';
	const TARGET_VERSION = '2.2.8.1';

	/**
	 * @var null|WOOCS
	 */
	protected $woocs = null;

	/**
	 * @var array
	 */
	private $currencies = array();

	public function is_required() {
		return class_exists( 'WOOCS' );
	}

	public function admin_install() {
	}

	public function remove_admin_hooks() {
	}

	/**
	 * @param WDP_Price_Display $price_display
	 */
	public function install( $price_display ) {
		if ( ! $this->load_requirements() ) {
			return;
		}

		add_action( 'wdp_rule_init', array( $this, 'set_rule_currency' ), 10, 1 );
		add_action( 'woocommerce_before_calculate_totals', array( $this, 'kill_currency_hooks' ), 10, 0 );
		add_action( 'woocommerce_after_calculate_totals', array( $this, 'activate_currency_hooks' ), 10, 0 );
	}

	public function remove_hooks() {
		remove_filter( 'wdp_save_cart_item_keys', array( $this, 'wdp_save_cart_item_keys' ), 10 );
		remove_filter( 'wdp_get_product_price', array( $this, 'wdp_get_product_price' ), 10 );
	}

	public function load_requirements() {
		$this->woocs = isset( $GLOBALS['WOOCS'] ) ? $GLOBALS['WOOCS'] : null;

		if ( $this->woocs ) {
			$this->currencies = $this->woocs->get_currencies();
		}

		return ! is_null( $this->woocs ) && ( $this->woocs instanceof WOOCS );
	}

	public function activate_currency_hooks() {
		add_filter( 'woocommerce_product_get_price', array( $this->woocs, 'raw_woocommerce_price' ), 9999, 2 );
		add_filter( 'woocommerce_product_variation_get_price', array( $this->woocs, 'raw_woocommerce_price' ), 9999, 2 );
	}

	public function kill_currency_hooks() {
		remove_filter( 'woocommerce_product_get_price', array( $this->woocs, 'raw_woocommerce_price' ), 9999 );
		remove_filter( 'woocommerce_product_variation_get_price', array( $this->woocs, 'raw_woocommerce_price' ), 9999 );
	}

	/**
	 * @param WDP_Rule $rule
	 */
	public function set_rule_currency( $rule ) {
		$currency         = $this->woocs->current_currency;
		$default_currency = $this->woocs->default_currency;
		$rate             = (float) $this->get_currency_prop( $currency, 'rate' ) / (float) $this->get_currency_prop( $default_currency, 'rate' );

		$rule->set_currency( $currency, $rate );
	}

	protected function is_woocs_exists() {
		return ! is_null( $this->woocs ) && ( $this->woocs instanceof WOOCS );
	}

	protected function get_currency( $currency ) {
		$currency_data = null;

		if ( isset( $this->currencies[ $currency ] ) && ! is_null( $this->currencies[ $currency ] ) ) {
			$currency_data = $this->currencies[ $currency ];
		}

		return $currency_data;
	}

	protected function get_currency_prop( $currency, $prop ) {
		$currency = $this->get_currency( $currency );

		return ! is_null( $currency ) && isset( $currency[ $prop ] ) ? $currency[ $prop ] : null;
	}
}

$cmp = new WDP_WOOCS_Compatibility();

if ( $cmp ) {
    $cmp->install(null);
}