<?php

class Wiaas_Unit_Test_Product_Factory {

	function __construct() {


	}

	/**
	 * Create new simple product
	 *
	 * @param array $args {
     *      @type int           $price            Simple product price
	 *      @type bool          $is_recurring     Flag indicating is simple product price is recurring
	 *      @type int           $pay_period       Pay period for simple product price if price is recurring
	 *      @type string|int    $category         Simple product category
	 *      @type string|int    $country          Simple product category term slug or id
	 *      @type string|int    $supplier         Simple product supplier term slug or id
	 *
	 * }
	 *
	 * @return WC_Product_Simple
	 */
	public function create_simple_product($args = array()) {

		$defaults = array(
			'price' => 10,
			'is_recurring' => false,
			'pay_period' => 0
		);

		$args = wp_parse_args($args, $defaults);

		$post_id = wp_insert_post(array(
			'post_type' => 'product',
			'post_status' => 'publish',
			'post_name' => 'product',
			'post_title' => 'Product',
			'post_content' => 'Product',
			'post_excerpt' => 'Product'
		), true);

		$product =  new WC_Product_Simple($post_id);

		$this->set_product_price($product, $args['price'], $args['is_recurring'],  $args['pay_period']);

		$product->save();

		if (! empty($args['category']) ) {

			$this->assign_product_to_category($product->get_id(), $args['category']);
		}

		if (! empty($args['country']) ) {

			$this->assign_product_to_country($product->get_id(), $args['country']);
		}

		if (! empty($args['supplier']) ) {

			$this->assign_product_to_supplier($product->get_id(), $args['supplier']);
		}

		return $product;
	}

	/**
	 * @param WC_Product_Simple|int $product  Product or Product ID
	 * @param int $price
	 * @param bool $is_recurring
	 * @param int $pay_period
	 */
	public function set_product_price($product, $price = 10, $is_recurring = false, $pay_period = 0) {

		if (is_numeric($product)) {

			$product = wc_get_product($product);
		}

		Wiaas_Product_Pricing::set_product_price($product, $price, $is_recurring,  $pay_period);

		$product->save();

		delete_transient('wc_bundled_product_data');


	}

	/**
	 * @param WC_Product_Simple|int $product  Product or Product ID
	 * @param string|int $category
	 */
	public function assign_product_to_category($product, $category) {

		if (is_numeric($product)) {
			$product_id = $product;
		} else {
			$product_id = $product->get_id();
		}

		wp_add_object_terms($product_id, $category, 'product_cat');
	}

	/**
	 * @param WC_Product_Simple|int $product  Product or Product ID
	 * @param string|int $country
	 */
	public function assign_product_to_country($product, $country) {

		if (is_numeric($product)) {
			$product_id = $product;
		} else {
			$product_id = $product->get_id();
		}

		wp_add_object_terms($product_id, $country, 'product_country');
	}

	/**
	 * @param WC_Product_Simple|int $product  Product or Product ID
	 * @param string|int $supplier
	 */
	public function assign_product_to_supplier($product, $supplier) {

		if (is_numeric($product)) {
			$product_id = $product;
		} else {
			$product_id = $product->get_id();
		}

		wp_add_object_terms($product_id, $supplier, 'supplier');
	}

	/**
	 * @param array $args {
	 *      @type array $products        Simple products
	 *      @type array $product_quantities Quantities for corresponding products from $products ( ex: [10, 5, 7] )
	 * }
	 *
	 * @return WC_Product_Bundle
	 */
	public function create_product_bundle($args = array()) {

		$post_id = wp_insert_post(array(
			'post_type' => 'product',
			'post_status' => 'publish',
			'post_name' => 'Bundle',
			'post_title' => 'Product Bundle',
			'post_content' => 'Product Bundle',
			'post_excerpt' => 'Product Bundle'
		), true);

		$bundle =  new WC_Product_Bundle($post_id);

		if (! empty($args['products']) ) {

			if ( empty($args['product_quantities']) ) {

				$args['product_quantities'] = array_fill(0, count($args['products']), 1);
			}

			if ( count($args['product_quantities']) < count($args['products'])) {

				$args['product_quantities'] = array_pad($args['product_quantities'], count($args['products']), 1);
			}

			$args['product_quantities'] = array_map('absint', $args['product_quantities']);

			$bundled_data = array();

			foreach ($args['products'] as $index => $product) {

				$bundled_data[] = array(
					'product_id' => $product->get_id(),
					'quantity_min' => $args['product_quantities'][$index],
					'quantity_max' => $args['product_quantities'][$index],
					'priced_individually' => true
				);
			}

			$bundle->set_bundled_data_items($bundled_data);

			$bundle->sync(true);
		}

		$bundle->save();

		if (! empty($args['country']) ) {

			$this->assign_product_to_country($bundle->get_id(), $args['country']);
		}

		return $bundle;
	}

	/**
	 * @param WC_Product_Bundle $bundle
	 * @param array $pricing_rules
	 * @param int $commission
	 * @param int $cost_margin
	 */
	public function set_product_bundle_prices($bundle, $pricing_rules = null, $commission = 50, $cost_margin = 0) {

		if ( empty($pricing_rules) ) {

			$pricing_rules = array(
				'purchase' => array(
					'minimal_fixed_price' => 500,
					'principal_amount' => 0,
					'minimal_services_price' => 0
				)
			);
		}

		Wiaas_Package_Pricing::set_package_prices($bundle, $pricing_rules, $commission, $cost_margin);
	}
}