<?php

namespace PixelYourSite;

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

function isWooCommerceVersionGte( $version ) {

	if ( defined( 'WC_VERSION' ) && WC_VERSION ) {
		return version_compare( WC_VERSION, $version, '>=' );
	} else if ( defined( 'WOOCOMMERCE_VERSION' ) && WOOCOMMERCE_VERSION ) {
		return version_compare( WOOCOMMERCE_VERSION, $version, '>=' );
	} else {
		return false;
	}

}

/**
 * @param \WC_Product|\WP_Post $product
 *
 * @return bool
 */
function wooProductIsType( $product, $type ) {

	if ( isWooCommerceVersionGte( '2.7' ) ) {
		return $type == $product->is_type( $type );
	} else {
		return $product->product_type == $type;
	}

}

function getWooProductPriceToDisplay( $product_id, $qty = 1 ) {

	if ( ! $product = wc_get_product( $product_id ) ) {
		return 0;
	}

	if ( isWooCommerceVersionGte( '2.7' ) ) {

		return (float) wc_get_price_to_display( $product, array( 'qty' => $qty ) );

	} else {

		return 'incl' === get_option( 'woocommerce_tax_display_shop' )
			? $product->get_price_including_tax( $qty )
			: $product->get_price_excluding_tax( $qty );

	}

}

function getWooCartSubtotal() {

	// subtotal is always same value on front-end and depends on PYS options
	$include_tax = get_option( 'woocommerce_tax_display_cart' ) == 'incl';

	if ( $include_tax ) {

		if ( isWooCommerceVersionGte( '3.2.0' ) ) {
			$subtotal = (float) WC()->cart->get_subtotal() + (float) WC()->cart->get_subtotal_tax();
		} else {
			$subtotal = WC()->cart->subtotal;
		}

	} else {

		if ( isWooCommerceVersionGte( '3.2.0' ) ) {
			$subtotal = (float) WC()->cart->get_subtotal();
		} else {
			$subtotal = WC()->cart->subtotal_ex_tax;
		}

	}

	return $subtotal;

}

function getWooEventValue( $valueOption, $amount, $global, $product_id = null, $percent = 100 ) {

	switch ( $valueOption ) {
		case 'global':
			$value = (float) $global;
			break;

		case 'cog':
			$cog = getAvailableProductCog($product_id);
			if ($cog['val']){
				if ($cog['type'] == 'fix') {
					$value = round((float) $amount - (float) $cog['val'], 2);
				} else {
					$value = round((float) $amount - ( (float) $amount * (float) $cog['val'] / 100 ), 2);
				}
			} else {
				$value = (float) $amount;
			}
			if ( !isPixelCogActive() ) $value = (float) $amount;
				break;

		case 'percent':
			$percents = (float) $percent;
			$percents = str_replace( '%', null, $percents );
			$percents = (float) $percents / 100;
			$value    = (float) $amount * $percents;
			break;

		default:    // "price" option
			$value = (float) $amount;
	}

	return $value;

}

function getWooEventValueOrder( $valueOption, $amount, $global, $order_id, $content_ids, $percent = 100 ) {

	switch ( $valueOption ) {
		case 'global':
			$value = (float) $global;
			break;

		case 'cog':
			$cog_value = getAvailableProductCogOrder($order_id);
			($cog_value !== '') ? $value = (float) round($cog_value, 2) : $value = (float) $amount;
			if ( !isPixelCogActive() ) $value = (float) $amount;
			break;

		case 'percent':
			$percents = (float) $percent;
			$percents = str_replace( '%', null, $percents );
			$percents = (float) $percents / 100;
			$value    = (float) $amount * $percents;
			break;

		default:    // "price" option
			$value = (float) $amount;
	}

	return $value;

}

function getWooEventValueCart( $valueOption, $amount, $global, $percent = 100 ) {

	switch ( $valueOption ) {
		case 'global':
			$value = (float) $global;
			break;

		case 'cog':
			$cog_value = getAvailableProductCogCart($amount);
			($cog_value !== '') ? $value = (float) round($cog_value, 2) : $value = (float) $amount;
			if ( !isPixelCogActive() ) $value = (float) $amount;
			break;

		case 'percent':
			$percents = (float) $percent;
			$percents = str_replace( '%', null, $percents );
			$percents = (float) $percents / 100;
			$value    = (float) $amount * $percents;
			break;

		default:    // "price" option
			$value = (float) $amount;
	}

	return $value;

}