<?php
/**
 * Factory class for creating discounters.
 *
 * @package flo-sync
 */

namespace NMFS\Sync\Items\Campaigns;

use NMFS\Sync\Items\Campaigns\Models\Discounter_Interface;
use NMFS\Sync\Items\Campaigns\Models\Discounter_Type_0_Model;
use NMFS\Sync\Items\Campaigns\Models\Discounter_Type_1_Model;
use NMFS\Sync\Items\Campaigns\Models\Discounter_Type_2_Model;
use NMFS\Sync\Items\Campaigns\Models\Discounter_Type_3_Model;
use NMFS\Sync\Items\Campaigns\Models\Discounter_Type_4_Model;
use NMFS\Sync\Items\Campaigns\Models\Discounter_Type_5_Model;
use NMFS\Sync\Items\Campaigns\Models\Discounter_Type_Set_Model;
use NMFS\Sync\Items\Campaigns\Models\Product_Model;
use WC_Cart;
use WC_Coupon;

defined( 'ABSPATH' ) || exit;

/**
 * Class Factory
 */
class Campaign_Factory {
	/**
	 * Create a product model from data.
	 *
	 * @param string $name     Product name.
	 * @param string $sku      Product SKU.
	 * @param float  $price    Product price.
	 * @param int    $vat_rate Product VAT rate.
	 *
	 * @return Product_Model
	 */
	public static function create_product( string $name, string $sku, float $price, int $vat_rate ): Product_Model {
		return new Product_Model( $name, $sku, $price, $vat_rate );
	}

	/**
	 * Create product models from a cart item.
	 *
	 * @param array   $cart_item Cart item.
	 * @param WC_Cart $cart      Cart.
	 *
	 * @return Product_Model[]
	 */
	public static function create_products_from_cart_item( array $cart_item, WC_Cart $cart ): array {
		$r = array();

		for ( $i = 0; $i < $cart_item['quantity']; $i++ ) {
			$name     = $cart_item['data']->get_name();
			$sku      = $cart_item['data']->get_sku();
			$price    = wc_get_price_including_tax( $cart_item['data'] );
			$vat_rate = Campaigns::get_cart_product_tax_rate( $cart_item['data'], $cart );

			$r[] = self::create_product( $name, $sku, $price, $vat_rate );
		}

		return $r;
	}

	/**
	 * Create a discounter from a coupon.
	 *
	 * @param WC_Coupon $coupon Coupon.
	 *
	 * @return Discounter_Interface|null Discounter if valid Hub Campaign, else null.
	 */
	public static function create_discounter( WC_Coupon $coupon ): ?Discounter_Interface {
		$discount_type   = get_post_meta( $coupon->get_id(), 'discount_type', true );
		$threshold_total = get_post_meta( $coupon->get_id(), 'mf_threshold_total', true );
		$priority        = get_post_meta( $coupon->get_id(), 'mf_priority', true );

		if ( 'mf_type_0' === $discount_type ) {
			$skus    = json_decode( get_post_meta( $coupon->get_id(), 'mf_parts_sku', true ) );
			$value_x = get_post_meta( $coupon->get_id(), 'mf_value_x', true );
			$value_y = get_post_meta( $coupon->get_id(), 'mf_value_y', true );

			return new Discounter_Type_0_Model( $coupon, $skus, $value_x, $value_y, $priority, $threshold_total );
		} elseif ( 'mf_type_1' === $discount_type ) {
			$skus    = json_decode( get_post_meta( $coupon->get_id(), 'mf_parts_sku', true ) );
			$value_x = get_post_meta( $coupon->get_id(), 'mf_value_x', true );
			$value_y = get_post_meta( $coupon->get_id(), 'mf_value_y', true );

			return new Discounter_Type_1_Model( $coupon, $skus, $value_x, $value_y, $priority, $threshold_total );
		} elseif ( 'mf_type_2' === $discount_type ) {
			$skus    = json_decode( get_post_meta( $coupon->get_id(), 'mf_parts_sku', true ) );
			$value_x = get_post_meta( $coupon->get_id(), 'mf_value_x', true );
			$value_y = get_post_meta( $coupon->get_id(), 'mf_value_y', true );

			return new Discounter_Type_2_Model( $coupon, $skus, $value_x, $value_y, $priority, $threshold_total );
		} elseif ( 'mf_type_3' === $discount_type ) {
			$skus   = json_decode( get_post_meta( $coupon->get_id(), 'mf_parts_sku', true ) );
			$values = json_decode( get_post_meta( $coupon->get_id(), 'mf_parts_value', true ) );

			return new Discounter_Type_3_Model( $coupon, $skus, $values, $priority, $threshold_total );
		} elseif ( 'mf_type_4' === $discount_type ) {
			$skus   = json_decode( get_post_meta( $coupon->get_id(), 'mf_parts_sku', true ) );
			$values = json_decode( get_post_meta( $coupon->get_id(), 'mf_parts_value', true ) );

			return new Discounter_Type_4_Model( $coupon, $skus, $values, $priority, $threshold_total );
		} elseif ( 'mf_type_5' === $discount_type ) {
			$skus   = json_decode( get_post_meta( $coupon->get_id(), 'mf_parts_sku', true ) );
			$values = json_decode( get_post_meta( $coupon->get_id(), 'mf_parts_value', true ) );

			return new Discounter_Type_5_Model( $coupon, $skus, $values, $priority, $threshold_total );
		} elseif ( 'mf_type_set' === $discount_type ) {
			$values = json_decode( get_post_meta( $coupon->get_id(), 'mf_parts', true ) );

			return new Discounter_Type_Set_Model( $coupon, $values, $priority );
		}

		return null;
	}
}
