<?php
/**
 * Template functions.
 *
 * Contains all template-related functions used by the
 * Mysterx - Mystery Boxes for WooCommerce plugin.
 *
 * @link       https://wpgenie.org
 * @since      1.0.0
 *
 * @package    Mysterx
 * @subpackage Mysterx/includes
 */

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


if ( ! function_exists( 'woocommerce_mysterx_add_to_cart_template' ) ) {
	/**
	 * Load mysterx product add to cart template part.
	 */
	function woocommerce_mysterx_add_to_cart_template() {
		wc_get_template( 'single-product/add-to-cart/mysterx.php' );
	}
}


if ( ! function_exists( 'mysterx_print_rewards_dataset' ) ) {
	/**
	 * Load mysterx product add to cart template part.
	 */
	function mysterx_print_rewards_dataset() {
		global $product;

		if ( 'mysterx' !== $product->get_type() ) {
			return;
		}
		$product_id = $product->get_id();

		$rewards = wp_cache_get( 'mysterx_print_rewards_dataset' . $product_id, 'mysterx' );

		if ( false === $rewards ) {
			$rewards = mysterx_get_items_for_product( $product_id );
			if ( empty( $rewards ) ) {
				return;
			}
			if ( ! is_array( $rewards ) ) {
				return;
			}

			$opened_counts = $product->get_count_of_opened_rewards();
			foreach ( $rewards as &$it ) {
				$reward_id = isset( $it['reward_id'] ) ? intval( $it['reward_id'] ) : 0;
				if ( ! empty( $it['product_id'] ) ) {
					$p = wc_get_product( $reward_id );
					if ( $p ) {
						$it['title'] = $p->get_name();
						$img_id      = $p->get_image_id();
						$it['image'] = $img_id ? wp_get_attachment_image_url( $img_id, 'thumbnail' ) : wc_placeholder_img_src();
					}
				}
				$it['reward_id']  = $reward_id;
				$it['weight']     = isset( $it['weight'] ) ? intval( $it['weight'] ) : 0;
				$it['quantity']   = isset( $it['qty'] ) ? intval( $it['qty'] ) : 0;
				$it['rarity']     = mysterx_get_rarity_class_by_weight( $it['weight'], $product_id, $reward_id );
				$already_opened   = isset( $opened_counts[ $reward_id ] ) ? intval( $opened_counts[ $reward_id ]->total_used ) : 0;
				$it['is_sold_out'] = ( $it['quantity'] > 0 && $already_opened >= $it['quantity'] );
			}
			unset( $it );
			wp_cache_set( 'mysterx_print_rewards_dataset' . $product_id, $rewards, 'mysterx' );

		}
		$rewards = apply_filters( 'mysterx_rewards_data', $rewards, $product_id );
		echo '<script type="application/json" id="box-data-' . esc_attr( $product_id ) . '">' . wp_json_encode( $rewards ) . '</script>';
	}
}


if ( ! function_exists( 'mysterx_box_opened_at' ) ) {
	/**
	 * Load mysterx product add to cart template part.
	 */
	function mysterx_box_opened_at() {

		wc_get_template( 'mysterx/opened-info.php' );
	}
}

if ( ! function_exists( 'mysterx_win_loop_product_title' ) ) {
	/**
	 * Display the product title in the loop for win context.
	 *
	 * @return void
	 */
	function mysterx_win_loop_product_title() {

		global $product;

		if ( ! $product ) {
			return;
		}

		$post_id = $product->get_id();
		$title   = get_the_title( $post_id );
		$classes = apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' );
		echo '<h2 class="' . esc_attr( $classes ) . '">' . esc_html( $title ) . '</h2>';
	}
}

if ( ! function_exists( 'mysterx_win_loop_box_opened_at' ) ) {
	/**
	 *  Display the box opened at info in the loop for win context.
	 *
	 * @return void
	 */
	function mysterx_win_loop_box_opened_at() {

		global $product, $myster_win;

		if ( ! $myster_win || ! $product ) {
			return;
		}

		$value = $product->get_meta( 'mysterx_opened_at' );

		if ( $value ) {

			$dateformat = get_option( 'date_format' );
			$timeformat = get_option( 'time_format' );

			?>
			<p class="opened_box_at">
				<span class='text'><?php echo esc_html__( 'Opened at: ', 'mysterx-mystery-boxes-for-woocommerce' ); ?></span>
				<span class='date'><?php echo esc_html( date_i18n( $dateformat, strtotime( $value ) ) ); ?></span>
				<span class='time'><?php echo esc_html( date_i18n( $timeformat, strtotime( $value ) ) ); ?></span>
			</p>
			<?php
		}
	}
}
if ( ! function_exists( 'woocommerce_mysterx_single_product_info' ) ) {
	/**
	 * Load mysterx product add to cart template part.
	 */
	function woocommerce_mysterx_single_product_info() {

		wc_get_template( 'single-product/mysterx-info.php' );
	}
}

if ( ! function_exists( 'woocommerce_mysterx_opening_modal' ) ) {
	/**
	 * Load mysterx product add to cart template part.
	 */
	function woocommerce_mysterx_opening_modal() {
		wc_get_template( 'modals/mysterx-opening-modal.php' );
	}
}
if ( ! function_exists( 'woocommerce_mysterx_win_modal' ) ) {
	/**
	 * Load mysterx product add to cart template part.
	 */
	function woocommerce_mysterx_win_modal() {
		wc_get_template( 'modals/mysterx-win-modal.php' );
	}
}

if ( ! function_exists( 'woocommerce_mysterx_single_product_rewards' ) ) {
	/**
	 * Load mysterx rewards list template part.
	 */
	function woocommerce_mysterx_single_product_rewards() {
		global $product;

		// Prevent double execution.
		static $has_run = false;
		if ( $has_run ) {
			return;
		}

		if ( ! is_a( $product, 'WC_Product' ) || 'mysterx' !== $product->get_type() ) {
			return;
		}

		$has_run = true;
		wc_get_template( 'single-product/mysterx-rewards.php' );
	}
}
