<?php

namespace Discountify\Core\Frontend;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Discountify\Utils\Helper;
use Discountify\Utils\Singleton;
use DiscountifyPro\Utils\Helper as UtilsHelper;

class Message {

	use Singleton;

	/**
	 * get currency
	 *
	 * @return string
	 */
	public function discount_currency() {
		return class_exists( 'WooCommerce' ) ? get_woocommerce_currency_symbol() : '';
	}


	/**
	 * Discount description
	 *
	 * @param [type] $discount
	 * @return void
	 */
	public function discount_desc( $discount, $product_id ) {
		if ( 'simple' === $discount['rules_type'] ) {
			return;
		}
		$currency  = $this->discount_currency();
		$bogo_s    = $discount['multi_discounts'];
		$bogo_type = $discount['bogo_discount_type'];
		$bogo_in   = $discount['bogo_discount_in'];
		foreach ( $bogo_s  as $index => $rules ) {
			$html = '';
			// X_Y types: buy_x_get_y and category_bogo
			if ( in_array( $bogo_type, array( 'buy_x_get_y', 'category_bogo' ), true ) ) {
				if ( 'product' === $bogo_in ) {
					$products = '';
					if ( is_array( $rules['bogo_x_y_products'] ) ) {
						foreach ( $rules['bogo_x_y_products'] as $key => $item_id ) {
							$concat    = $key === array_key_last( $rules['bogo_x_y_products'] ) ? '' : ' ,';
							$products .= get_the_title( $item_id ) . $concat;
						}
					} else {
						$products .= $rules['bogo_x_y_products'] !== '' ? get_the_title( $rules['bogo_x_y_products'] ) : '';
					}

					$html = '<strong>' . ' ' . $products . '</strong>';
				} elseif ( 'category' === $bogo_in ) {
					$category = '';
					if ( is_array( $rules['bogo_x_y_categories'] ) ) {
						foreach ( $rules['bogo_x_y_categories'] as $key => $item_id ) {
							$concat    = $key === array_key_last( $rules['bogo_x_y_categories'] ) ? '' : ' ,';
							$term      = get_term_by( 'id', $item_id, 'product_cat' );
							$category .= $term->name . $concat;
						}
					} else {
						$term      = $rules['bogo_x_y_categories'] !== '' ? get_term_by( 'id', $rules['bogo_x_y_categories'], 'product_cat' ) : '';
						$category .= ! empty( $term ) ? $term->name : '';
					}

					$html = '<strong>' . ' ' . $category . '</strong>';
				}
			} else {
				$html = ' ';
				if ( $discount['rules_type'] !== 'total_spend' ) {
					$html = ' ' . esc_html__( 'On this item', 'discountify' );
				}
			}

			if ( $discount['rules_type'] === 'bulk_pricing' ) {
				return $this->bulk_pricing_msg( $discount, $product_id );
			} elseif ( $discount['rules_type'] === 'cart_adjustment' ) {
				$offer_condtion = ' ' . esc_html__( 'is', 'discountify' ) . ' ' . \Discountify\Utils\Helper::get_condtion_type( $rules['condition_type'] ) . ' ';
				// Cart adjustment has different structure - min is subtotal threshold
				$min               = ! empty( $rules['min'] ) ? esc_html__( 'Subtotal ', 'discountify' ) . $offer_condtion . $currency . $rules['min'] : '';
				$offer_type_cart   = ! empty( $rules['offer_type'] ) ? $rules['offer_type'] : '';
				$discount_num_cart = ! empty( $rules['discount_number'] ) ? $rules['discount_number'] : '';
				$get_discounts     = '';

				if ( 'percentage' === $offer_type_cart ) {
					$get_discounts .= esc_html__( ' Get ', 'discountify' ) . $discount_num_cart . '%' . esc_html__( ' OFF', 'discountify' );
				} elseif ( 'fixed' === $offer_type_cart ) {
					$get_discounts .= esc_html__( ' Get ', 'discountify' ) . $currency . $discount_num_cart . esc_html__( ' OFF', 'discountify' );
				}
				echo '<div class="single_desc">' . wp_kses_post( $min . $get_discounts ) . '</div>';
			} else {
				$qty_part      = '';
				$free_quantity = ! empty( $rules['free_quantity'] ) ? $rules['free_quantity'] : '';
				$max_value     = ! empty( $rules['max'] ) ? $rules['max'] : '';
				if ( $discount['rules_type'] === 'total_spend' ) {
					$min = ! empty( $rules['min'] ) ? esc_html__( 'Spend Minimum ', 'discountify' ) . $rules['min'] . $currency : '';
				} else {
					$min      = ! empty( $rules['min'] ) ? esc_html__( 'Buy Minimum ', 'discountify' ) . $rules['min'] : '';
					$qty_part = $free_quantity !== '' ? ' ' . esc_html__( 'at', 'discountify' ) . ' ' . $free_quantity . ' ' . esc_html__( 'quantity', 'discountify' ) : '';
				}
				$max            = $max_value !== '' ? esc_html__( ' Maximum ', 'discountify' ) . $max_value : '';
				$get_discounts  = '';
				$get_discounts .= $discount['rules_type'] === 'total_spend' ? ' ' . esc_html__( 'On this item', 'discountify' ) . ' ' : '';

				$offer_type      = ! empty( $rules['offer_type'] ) ? $rules['offer_type'] : '';
				$discount_number = ! empty( $rules['discount_number'] ) ? $rules['discount_number'] : '';
				if ( 'free' === $offer_type ) {
					$get_discounts .= esc_html__( ' Get ', 'discountify' ) . $free_quantity . ' ' . esc_html__( 'Free', 'discountify' );
				} elseif ( 'percentage' === $offer_type ) {
					$get_discounts .= esc_html__( ' Get ', 'discountify' ) . $discount_number . '%'
					. esc_html__( ' Percent OFF', 'discountify' ) . ' ' . esc_html__( 'at', 'discountify' ) . ' ' . $free_quantity . ' ' . esc_html__( 'quantity', 'discountify' );
				} elseif ( 'fixed' === $offer_type ) {
					$get_discounts .= esc_html__( ' Get ', 'discountify' ) . $discount_number . $currency
					. esc_html__( ' OFF', 'discountify' ) . $qty_part;
				}

				echo '<div class="single_desc">' . wp_kses_post( $min . $max . $get_discounts . $html ) . '</div>';
			}
		}
	}

		/**
		 * Bulk pricing message
		 *
		 * @param [type] $discount
		 * @param [type] $product_id
		 * @return void
		 */
	public function bulk_pricing_msg( $discount, $product_id ) {
		$bulk_pricing = ( ! empty( $discount['multi_discounts'] ) ) ? $discount['multi_discounts'] : array();
		if ( empty( $bulk_pricing ) || empty( wc_get_product( $product_id ) ) ) {
			return;
		}
		$prod = wc_get_product( $product_id );
		?>
		<table id="discount-table">
			<thead>
				<th><?php esc_html_e( 'Quantity', 'discountify' ); ?></th>
				<th><?php esc_html_e( 'Discount', 'discountify' ); ?></th>
				<th><?php esc_html_e( 'Price', 'discountify' ); ?></th>
			</thead>
			<tbody>
				<?php
				foreach ( $bulk_pricing as $item ) :
					$min            = ! empty( $item['min'] ) ? $item['min'] : '';
					$max            = ! empty( $item['max'] ) ? '-' . $item['max'] : '';
					$offer_type     = $item['offer_type'] === 'percentage' ? '%' : $this->discount_currency();
					$discount       = \Discountify\Core\Frontend\Calculator::instance()->calculate_discount( $item['offer_type'], $prod->get_price(), $item['discount_number'] );
					$discount_price = \Discountify\Core\Frontend\Calculator::instance()->apply_discount( $prod->get_price(), $discount );

					?>
				<tr>
					<td><?php echo esc_html( $min . $max ); ?></td>
					<td><?php echo $item['discount_number'] !== '' ? esc_html( $item['discount_number'] . $offer_type ) : ''; ?></td>
					<td><?php echo esc_html( $discount_price . ' (' . $discount . ')' ); ?></td>
				</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
		<?php
	}

	public function message_markup( $product_id, $message_data ) {
		$message_data = str_replace( '{{product_name}}', get_the_title( $product_id ), $message_data );

		$message  = '';
		$message .= '<div class="discountify-product-discount">';
		$message .= '<span class="discountify-product-discount-label-value">' . Helper::kses( $message_data ) . '</span>';
		$message .= '</div>';

		return Helper::kses( $message );
	}

		/**
		 * show discount label only
		 */
	public static function discount_label( $value ) {
		if ( empty( $value['discount_label'] ) ) {
			$discount_label = '';
		}
		if ( $value['rules_type'] === 'simple' ) {
			$discount_label = $value['discount_label'] === '' ? $value['discount_number'] . Helper::discount_symbol( $value ) : $value['discount_label'];
		} else {
			$discount_label = $value['discount_label'];
		}

		return $discount_label;
	}
}
