/**
 * Currency Switcher (WOOCS)
 */

class WDP_with_WOOCS_compatibility {

  /**
   * @return null|WOOCS
   */
  private static function get_woocs() {
    return isset( $GLOBALS['WOOCS'] ) ? $GLOBALS['WOOCS'] : null;
  }

  public function __construct() {
    add_filter( "wdp_get_product_price", function ( $price, $product, $price_mode, $item_meta ) {
      /**
       * @var WC_Product $product
       */

      $reflection = new ReflectionClass( $product );
      $property   = $reflection->getProperty( 'data' );
      $property->setAccessible( true );

      if ( $product->is_on_sale( 'edit' ) ) {
        if ( 'sale_price' === $price_mode ) {
          $price = $property->getValue( $product )['sale_price'];
        } elseif ( 'discount_sale' === $price_mode ) {
          $price = $property->getValue( $product )['sale_price'];
        } else {
          $price = $property->getValue( $product )['regular_price'];
        }
      } else {
        $price = $property->getValue( $product )['price'];
      }

      return $price;
    }, 50, 4 );


    add_filter( 'wdp_initial_cart_subtotal', array( $this, 'get_current_currency_price' ), 10, 1 );
    add_filter( 'wdp_cart_item_subtotal', array( $this, 'get_current_currency_price' ), 10, 1 );
    add_filter( 'wdp_cart_item_initial_subtotal', array( $this, 'get_current_currency_price' ), 10, 1 );

    add_filter( 'wdp_get_price_html_from_wdp_product', function ( $wdp_product, $price_display ) {
      /**
       * @var $wdp_product WDP_Product
       */
      $wdp_product->set_price( $this->get_current_currency_price( $wdp_product->get_price() ) );
      $wdp_product->set_new_price( $this->get_current_currency_price( $wdp_product->get_new_price() ) );
      $wdp_product->set_min_price( $this->get_current_currency_price( $wdp_product->get_min_price() ) );
      $wdp_product->set_max_price( $this->get_current_currency_price( $wdp_product->get_max_price() ) );
      $wdp_product->set_min_bulk_price( $this->get_current_currency_price( $wdp_product->get_min_bulk_price() ) );
		
		$summary = $wdp_product->get_children_summary();

		if ( isset($summary['min_bulk']['initial_price']) ) {
			$summary['min_bulk']['initial_price'] = (float)$this->get_current_currency_price( $summary['min_bulk']['initial_price'] );
		}

		if ( isset($summary['min']['initial_price']) ) {
			$summary['min']['initial_price'] = (float)$this->get_current_currency_price( $summary['min']['initial_price'] );
		}

		$wdp_product->set_children_summary($summary);


      return $wdp_product;
    }, 10, 2 );

    add_filter( 'wdp_cart_totals_coupon_data', function ( $coupon, $wdp_totals ) {
      $coupon['amount'] = $this->get_current_currency_price( $coupon['amount'] );

      return $coupon;
    }, 10, 2 );

    add_filter( 'wdp_cart_totals_apply_fee_data', function ( $fee_data, $wdp_totals ) {
      $fee_data['amount'] = $this->get_current_currency_price( $fee_data['amount'] );

      return $fee_data;
    }, 10, 2 );

    add_filter( 'woocommerce_cart_shipping_method_full_label', array(
      $this,
      'woocommerce_cart_shipping_method_full_label'
    ), 9, 2 );
	  
	  add_filter( 'wdp_amount_saved', function ( $amount_saved, $cart_items ) {
			return $this->get_current_currency_price( $amount_saved );
		}, 10, 2 );
  }

  public function woocommerce_cart_shipping_method_full_label( $label, $method ) {
    if ( false !== strpos( $label, 'wdp-amount' ) ) {
      return $label;
    }

    $initial_cost_key = '_wdp_initial_cost';
    $initial_tax_key  = '_wdp_initial_tax';

    $meta_data = $method->get_meta_data();
    if ( ! isset( $meta_data[ $initial_cost_key ] ) ) {
      return $label;
    }

    $initial_cost = $meta_data[ $initial_cost_key ];
    $initial_tax  = 0.0;

    if ( isset( $meta_data[ $initial_tax_key ] ) ) {
      $initial_tax = $meta_data[ $initial_tax_key ];
    }

    $initial_cost = (float) $this->get_current_currency_price( $initial_cost );
    $initial_tax  = (float) $this->get_current_currency_price( $initial_tax );

    if ( WC()->cart->display_prices_including_tax() ) {
      $initial_cost_html = '<del>' . wc_price( $initial_cost + $initial_tax ) . '</del>';
    } else {
      $initial_cost_html = '<del>' . wc_price( $initial_cost ) . '</del>';
    }
    $initial_cost_html = preg_replace( '/\samount/is', 'wdp-amount', $initial_cost_html );

    $label = preg_replace( '/(<span[^>]*>)/is', $initial_cost_html . ' $1', $label, 1 );

    return $label;
  }

  public function get_default_currency_price( $price ) {
    $woocs = $this->get_woocs();
    if ( is_null( $woocs ) ) {
      return $price;
    }
    $currencies = $woocs->get_currencies();

    if ( in_array( $woocs->current_currency, $woocs->no_cents ) ) {
      $precision = 0;
    } else {
      if ( $woocs->current_currency != $woocs->default_currency ) {
        $precision = $woocs->get_currency_price_num_decimals( $woocs->current_currency, $woocs->price_num_decimals );
      } else {
        $precision = $woocs->get_currency_price_num_decimals( $woocs->default_currency, $woocs->price_num_decimals );
      }
    }

    if ( isset( $currencies[ $woocs->current_currency ] ) AND $currencies[ $woocs->current_currency ] != null ) {
      $price = number_format( floatval( (float) $price / (float) $currencies[ $woocs->current_currency ]['rate'] ), $precision, $woocs->decimal_sep, '' );
    }

    return $price;
  }


  public function get_current_currency_price( $price ) {
    $woocs = $this->get_woocs();
    if ( is_null( $woocs ) ) {
      return $price;
    }

    $currencies = $woocs->get_currencies();

    if ( in_array( $woocs->current_currency, $woocs->no_cents ) ) {
      $precision = 0;
    } else {
      if ( $woocs->current_currency != $woocs->default_currency ) {
        $precision = $woocs->get_currency_price_num_decimals( $woocs->current_currency, $woocs->price_num_decimals );
      } else {
        $precision = $woocs->get_currency_price_num_decimals( $woocs->default_currency, $woocs->price_num_decimals );
      }
    }

    if ( isset( $currencies[ $woocs->current_currency ] ) AND $currencies[ $woocs->current_currency ] != null ) {
      $price = number_format( floatval( (float) $price * (float) $currencies[ $woocs->current_currency ]['rate'] ), $precision, $woocs->decimal_sep, '' );
    }

    return $price;
  }

}

new WDP_with_WOOCS_compatibility();