<?php
/**
 * Mysterx Shortcodes.
 *
 * Registers and handles all shortcodes provided by the
 * Mysterx - Mystery Boxes for WooCommerce plugin, including
 * [mysterx], [my_mysterx], [my_won_mysterx], [featured_mysterx],
 * and [recent_mysterx].
 *
 * @link              https://wpgenie.org
 * @since             1.0.0
 * @package           Mysterx
 * @subpackage        Mysterx/public
 */

/**
 * Class Mysterx_Shortcodes
 *
 * @since 1.0.0
 */
class Mysterx_Shortcodes extends WC_Shortcodes {

	/**
	 * Init shortcodes.
	 */
	public static function init() {

		$shortcodes = array(

			'mysterx'          => __CLASS__ . '::mysterx',
			'my_won_mysterx'   => __CLASS__ . '::my_won_mysterx',
			'my_mysterx'       => __CLASS__ . '::my_mysterx',
			'featured_mysterx' => __CLASS__ . '::featured_mysterx',
			'recent_mysterx'   => __CLASS__ . '::recent_mysterx',

		);

		foreach ( $shortcodes as $shortcode => $function ) {
			add_shortcode( apply_filters( "{$shortcode}_shortcode_tag", $shortcode ), $function );
		}
	}
	/**
	 * List multiple products shortcode.
	 *
	 * @param array $atts Attributes.
	 * @return string
	 */
	public static function mysterx( $atts ) {

		$atts = (array) $atts;

		$type = 'mysterx';

		if ( isset( $atts['my_won_mysterx'] ) && wc_string_to_bool( $atts['my_won_mysterx'] ) ) {
			$type = 'my_won_mysterx';

		} elseif ( isset( $atts['my_mysterx'] ) && wc_string_to_bool( $atts['my_mysterx'] ) ) {
			$type = 'my_mysterx';

		} elseif ( isset( $atts['featured_mysterx'] ) && wc_string_to_bool( $atts['featured_mysterx'] ) ) {
			$type = 'featured_mysterx';

		} elseif ( isset( $atts['recent_mysterx'] ) && wc_string_to_bool( $atts['recent_mysterx'] ) ) {
			$type = 'recent_mysterx';

		}

		$shortcode = new Mysterx_Shortcode( $atts, $type );

		return $shortcode->get_content();
	}
	/**
	 * Output featured mystery box products.
	 *
	 * @param array $atts Attributes.
	 * @return string
	 */
	public static function featured_mysterx( $atts ) {

		$atts = array_merge(
			array(
				'limit'        => '12',
				'columns'      => '4',
				'orderby'      => 'date',
				'order'        => 'DESC',
				'category'     => '',
				'cat_operator' => 'IN',
			),
			(array) $atts
		);

		$atts['visibility'] = 'featured';

		$shortcode = new Mysterx_Shortcode( $atts, 'featured_mysterx' );

		return $shortcode->get_content();
	}
	/**
	 * Output recent mystery box products.
	 *
	 * @param array $atts Attributes.
	 * @return string
	 */
	public static function recent_mysterx( $atts ) {

		$atts = array_merge(
			array(
				'limit'          => '12',
				'columns'        => '4',
				'orderby'        => 'date',
				'order'          => 'DESC',
				'category'       => '',
				'cat_operator'   => 'IN',
				'mysterx_status' => '',
			),
			(array) $atts
		);

		$shortcode = new Mysterx_Shortcode( $atts, 'recent_mysterx' );

		return $shortcode->get_content();
	}
	/**
	 * Output my mystery box products.
	 *
	 * @param array $atts Attributes.
	 * @return string
	 */
	public static function my_mysterx( $atts ) {

		if ( is_user_logged_in() ) {

			global $wpdb;

			$current_user_id        = get_current_user_id();
			$current_page           = empty( get_query_var( 'paged' ) ) ? 1 : absint( get_query_var( 'paged' ) );
			$default_posts_per_page = apply_filters( 'loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page() );
			$customer_instant_wins  = array();
			$max_num_pages          = '';
			$ofset                  = ( $current_page - 1 ) * $default_posts_per_page;

			$countresults = $wpdb->get_var(
				$wpdb->prepare(
					'SELECT COUNT(1)
					FROM %i as l
					LEFT JOIN %i as o ON l.id = o.id
					WHERE l.user_id = %d
					AND o.opened_at IS NULL',
					MYSTERX_TBL_LOG,
					MYSTERX_TBL_OPEN_LOG,
					$current_user_id
				)
			);

			$max_num_pages = ceil( intval( $countresults ) / intval( $default_posts_per_page ) );

			$results = $wpdb->get_results(
				$wpdb->prepare(
					'SELECT
					l.product_id,
					l.order_id,
					o.opened_at
					FROM %i as l
					LEFT JOIN %i as o ON l.id = o.id
					WHERE l.user_id = %d
					AND o.opened_at IS NULL
					ORDER BY `date` ASC
					LIMIT %d OFFSET %d',
					MYSTERX_TBL_LOG,
					MYSTERX_TBL_OPEN_LOG,
					$current_user_id,
					$default_posts_per_page,
					$ofset
				),
				ARRAY_A
			);

			$output = wc_get_template_html(
				'shortcodes/mysterx.php',
				array(
					'current_page'    => absint( $current_page ),
					'boxes'           => $results,
					'max_num_pages'   => intval( $max_num_pages ),
					'wp_button_class' => wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '',
				)
			);

		} else {

			$output = '<div class="woocommerce"><p class="woocommerce-info">' . esc_html__( 'Please log in to see your Mystery Box(es).', 'mysterx-mystery-boxes-for-woocommerce' ) . '</p></div>';
			return $output;
		}

		return $output;
	}

	/**
	 * Output featured products.
	 *
	 * @param array $atts Attributes.
	 * @return string
	 */
	public static function my_won_mysterx( $atts ) {

		if ( is_user_logged_in() ) {

			global $wpdb;

			$current_user_id        = get_current_user_id();
			$current_page           = empty( get_query_var( 'paged' ) ) ? 1 : absint( get_query_var( 'paged' ) );
			$default_posts_per_page = apply_filters( 'loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page() );
			$customer_instant_wins  = array();
			$max_num_pages          = '';
			$ofset                  = ( $current_page - 1 ) * $default_posts_per_page;
			$countresults           = $wpdb->get_var(
				$wpdb->prepare(
					'SELECT COUNT(1)
						FROM %i as o
						WHERE o.user_id = %d
						AND o.opened_at IS NOT NULL',
					MYSTERX_TBL_OPEN_LOG,
					$current_user_id,
				)
			);
			$max_num_pages          = ceil( intval( $countresults ) / intval( $default_posts_per_page ) );
			$results                = $wpdb->get_results(
				$wpdb->prepare(
					'SELECT
						o.reward_id,
						o.opened_at
						FROM %i as o
						WHERE o.user_id = %d
						AND o.opened_at IS NOT NULL
						ORDER BY o.opened_at DESC
						LIMIT %d OFFSET %d',
					MYSTERX_TBL_OPEN_LOG,
					$current_user_id,
					$default_posts_per_page,
					$ofset
				),
				ARRAY_A
			);
			$output                 = wc_get_template_html(
				'shortcodes/mysterx-wins.php',
				array(
					'current_page'    => absint( $current_page ),
					'boxes'           => $results,
					'max_num_pages'   => intval( $max_num_pages ),
					'wp_button_class' => wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '',
				)
			);

		} else {

			$output = '<div class="woocommerce"><p class="woocommerce-info">' . esc_html__( 'Please log in to see your Mystery Box prizes.', 'mysterx-mystery-boxes-for-woocommerce' ) . '</p></div>';
			return $output;
		}

		return $output;
	}
}
