<?php

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

/**
 * Class Wiaas_Package_Pricing
 */
class Wiaas_Package_Pricing {

	/**
	 * Available pay types for wiaas packages
	 * @var array
	 */
	private static $pay_types = array(
		'purchase' => array(
			'title' => 'Purchase',
			'package_pay_period' => 0,
			'services_contract_period' => 0,
			'max_contract_period' => 36,
			'period_unit' => 'month',
			'labe'
		),
		'purchase_24' => array(
			'title' => 'Purchase with 24M commitment',
			'package_pay_period' => 0,
			'services_contract_period' => 24,
			'max_contract_period' => 36,
			'period_unit' => 'month'
		),
		'managed_36' => array(
			'title' => 'Managed service 36M rent',
			'package_pay_period' => 36,
			'services_contract_period'=> 36,
			'max_contract_period' => 36,
			'period_unit' => 'month'
		)
	);

	/**
	 * Payment fields for pricing rules
	 * @var array
	 */
	private static $payment_fields = array(
		'minimal_fixed_price' => 0,
		'principal_amount' => 0,
		'minimal_services_price' => 0
	);

	public static function init() {

		add_filter('woocommerce_bundle_price_html',  array( __CLASS__, 'get_package_price_html' ), 10, 2);
	}

	public static function get_package_price_html($price_html, $package) {
		$bundled_items = $package->get_bundled_items();

		$recurring_price = 0;

		foreach ($bundled_items as $bundled_item) {
			$product = $bundled_item->product;
			$product_price = Wiaas_Product_Pricing::get_product_price($product);
			if ($product_price['is_recurring']) {
				$recurring_price += $product_price['price'] * $bundled_item->get_quantity();
			}
		}

		$price_html = 'Fixed: ' . $price_html . ' and ' . $recurring_price . ' / month';

		return $price_html;
	}

	/**
	 * Get configuration for available payment types
	 * @return array
	 */
	public static function get_available_pay_types() {
		return self::$pay_types;
	}

	/**
	 * Get empty payment rule with initialized fields to default values
	 * @return array
	 */
	public static function get_empty_pricing_rule() {
		return self::$payment_fields;
	}

	/**
	 * Retrieve configured payment prices for package
	 * @param $package
	 *
	 * @return array
	 */
	public static function get_package_prices($package) {
		return self::_get_package_prices($package);
	}

	public static function get_package_pricing_commission($package) {
		return self::_get_package_pricing_commision($package);
	}

	/**
	 * Persist payment prices configuration for package
	 * @param $package
	 * @param $pricing_rules
	 */
	public static function set_package_prices($package, $pricing_rules, $commision) {
		if ( isset( $pricing_rules ) ) {
			$package->update_meta_data( '_wiaas_pricing_rules', $pricing_rules );
			$package->update_meta_data('_package_pricing_commision', $commision, true);
		} else {
			$package->delete_meta_data( '_wiaas_pricing_rules' );
		}
		$package->save_meta_data();
	}

	// PRIVATE

	private static function _get_package_prices($package) {
		$pricing_rules = $package->get_meta( '_wiaas_pricing_rules' );
		$commision = self::_get_package_pricing_commision($package);

		$prices = array();
		foreach ($pricing_rules as $type => $pricing_rule) {

			$pay_type = self::$pay_types[$type];

			$prices[$type] = array(
				'id' => $type,
				'payment_type' => $pay_type['title'],
				'max_contract_period' => $pay_type['max_contract_period'],
				'package_pay_period' => floatval($pay_type['package_pay_period']),
				'period_unit' => $pay_type['period_unit'],
				'services_contract_period' => $pay_type['services_contract_period'],
				'commision_split' => $commision / 100.00,

				'minimal_fixed_price' => floatval($pricing_rule['minimal_fixed_price']),
				'principal_amount' => floatval($pricing_rule['principal_amount']),
				'minimal_services_price' => floatval($pricing_rule['minimal_services_price']),
			);
		}

		return $prices;
	}

	private static function _get_package_pricing_commision($package) {
		$commision = $package->get_meta( '_package_pricing_commision', true);

		if (!isset($commision) || $commision === '') {
			return 50.00;
		}

		return (float) $commision;
	}
}

Wiaas_Package_Pricing::init();