<?php

defined( 'ABSPATH' ) || exit;

/**
 * Class Wiaas_Product_Pricing
 */
class Wiaas_Product_Pricing {

	public static function init() {

		add_filter('woocommerce_get_price_html', array(__CLASS__, 'get_product_price_html'), 10, 2);

		add_filter('woocommerce_bundled_item_price', array(__CLASS__, 'get_product_fixed_price'), 10, 2);

		add_filter('woocommerce_bundled_item_is_priced_individually', array(__CLASS__, 'is_bundled_product_price_fixed'), 10, 2);

		add_filter('woocommerce_bundles_process_bundled_item_admin_data', array(__CLASS__, 'handle_bundled_product_pricing'));
	}

	/**
	 * Configures each bundled product to be priced individually as default setting
	 * @param $bundled_item_data
	 *
	 * @return mixed
	 */
	public static function handle_bundled_product_pricing($bundled_item_data) {

		$bundled_item_data[ 'priced_individually' ] = 'yes';

		return $bundled_item_data;
	}

	/**
	 * Checks if bundled product has fixed price
	 * @param $is_priced_individually
	 * @param $bundled_item
	 *
	 * @return bool
	 */
	public static function is_bundled_product_price_fixed($is_priced_individually, $bundled_item) {

		$bundled_product = $bundled_item->product;

		return isset($bundled_product) ?
			!self::_get_is_product_price_recurring($bundled_product) :
			false;
	}

	/**
	 * Retrives fixed price for bundled product
	 * @param $price
	 * @param $product
	 *
	 * @return int
	 */
	public static function get_product_fixed_price($price, $product) {
		if (self::_get_is_product_price_recurring($product)) {
			return 0;
		}
		return $price;
	}

	/**
	 * Builds html for product
	 * @param $price
	 * @param $object
	 *
	 * @return string
	 */
	public static function get_product_price_html($price, $object) {

		if ($object instanceof WC_Product_Simple) {
			$is_recurring = self::_get_is_product_price_recurring($object);
			if ($is_recurring) {
				$price .= ' / month';
			}
		}

		return $price;
	}

	/**
	 * Retrieves product configured price
	 * @param $product
	 *
	 * @return array
	 */
	public static function get_product_price($product) {
		$is_recurring = self::_get_is_product_price_recurring($product);
		$pay_period = self::_get_product_price_pay_period($product);

		return array(
			'price' => $product->get_price(),
			'is_recurring' => $is_recurring,
			'pay_period' => $pay_period
		);
	}

	/**
	 * Sets configured product price
	 * @param $product
	 * @param $price
	 * @param bool $is_recurring
	 * @param int $pay_period
	 */
	public static function set_product_price($product, $price, $is_recurring = false, $pay_period = 0) {
		$product->set_price($price);
		$product->set_regular_price($price);

		self::_set_is_product_price_recurring($product, $is_recurring);
		self::_set_product_price_pay_period($product, $pay_period);

		$product->save_meta_data();

	}

	// PRIVATE

	private static function _get_is_product_price_recurring($product) {
		return $product->get_meta( '_wiaas_is_product_price_recurring', true) === 'yes';
	}

	private static function _get_product_price_pay_period($product) {
		return (int) $product->get_meta( '_wiaas_product_pay_period', true);
	}

	private static function _set_is_product_price_recurring($product, $is_recurring) {
		$product->update_meta_data('_wiaas_is_product_price_recurring', $is_recurring ? 'yes' : 'no');
	}


	private static function _set_product_price_pay_period($product, $pay_period) {
		$product->update_meta_data('_wiaas_product_pay_period', (int) $pay_period);
	}

}

Wiaas_Product_Pricing::init();