<?php

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

// Return if wishlist inactive
if ( !class_exists( 'YITH_WCWL' ) ) {
	return;
}

require_once SHRK_THEME_MODULES . '/woocommerce/extras/yith-wishlist-widget.php';

/* remove default placement */
add_filter( 'yith_wcwl_positions', function ( $positions ) {
	unset( $positions['summary'] );
	unset( $positions['add-to-cart'] );
});

/* add body class when enabled */
add_filter( 'body_class', function ($c) {
	if ( in_array( 'yith-woocommerce-wishlist/init.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { 
		$c[] = 'yith-wishlist-enabled';
	}
	return $c;
});
	





function shrk_yith_wishlist_init() {
	global $product;

	if ( !function_exists( 'wc_get_product' ) ) {
		return;
	}

	if ( ! is_object( $product ) ) {
		$product = wc_get_product( get_the_ID() );	
	} 
	
	/* add to custom theme locations */
	add_action( 'woocommerce_after_shop_loop_item', 'woo_shrk_wishlist_button' );
	
	if ( is_object( $product ) && $product->is_type( 'variable' ) ) {
		add_action( 'woocommerce_single_variation', 'woo_shrk_wishlist_button', 28 );
	} else {
		add_action( 'woocommerce_after_add_to_cart_button', 'woo_shrk_wishlist_button' );
	}
}
add_action( 'wp_head', 'shrk_yith_wishlist_init' );

/* enqueue scripts */
function woo_shrk_wishlist_scripts() {
	wp_enqueue_script( 'shrk-woo-wishlist',  SHRK_THEME_MODULES_URI . 'woocommerce/extras/yith-wishlist.js', array( 'shrk-theme-js' ), false, true );
}
add_action( 'wp_enqueue_scripts', 'woo_shrk_wishlist_scripts', 12 );
	
/* render button */
function woo_shrk_wishlist_button() {
	if ( in_array( 'yith-woocommerce-wishlist/init.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { 
		echo wp_kses( do_shortcode('[yith_wcwl_add_to_wishlist]'), shrk_kses_allowed( 'shortcode' ) );
	}
}

/* return wishlist count (ajax) */
function woo_shrk_get_wishlist_count() {

	if ( !class_exists( 'YITH_WCWL' ) ) {
		echo '0';
		exit;
	}

	echo absint( YITH_WCWL()->count_products() );	
	exit;
}
add_action( 'wp_ajax_get_wishlist_count', 'woo_shrk_get_wishlist_count' );
add_action( 'wp_ajax_nopriv_get_wishlist_count', 'woo_shrk_get_wishlist_count' );

/* return wishlist items (html) & count (ajax) */
function woo_shrk_get_wishlist_items() {

	if ( !class_exists( 'YITH_WCWL' ) ) {
		echo '0';
		exit;
	}
	
	$per_page    = 5;
	$pagination  = 'no';
	$wishlist_id = false;
	$query_args  = array();

	if( ! empty( $user_id ) ){
		$query_args[ 'user_id' ] = $user_id;
		$query_args[ 'is_default' ] = 1;

		if( get_current_user_id() == $user_id ){
			$is_user_owner = true;
		}
	}
	elseif( ! is_user_logged_in() ){
		if( empty( $wishlist_id ) ){
			$query_args[ 'wishlist_id' ] = false;
			$is_user_owner = true;
		}
		else{
			$is_user_owner = false;

			$query_args[ 'wishlist_token' ] = $wishlist_id;
			$query_args[ 'wishlist_visibility' ] = 'visible';
		}
	}
	else{
		if( empty( $wishlist_id ) ){
			$query_args[ 'user_id' ] = get_current_user_id();
			$query_args[ 'is_default' ] = 1;
			$is_user_owner = true;
		}
		else{
			$wishlist = YITH_WCWL()->get_wishlist_detail_by_token( $wishlist_id );
			$is_user_owner = $wishlist['user_id'] == get_current_user_id();

			$query_args[ 'wishlist_token' ] = $wishlist_id;

			if( ! empty( $wishlist ) && $wishlist['user_id'] != get_current_user_id() ){
				$query_args[ 'user_id' ] = false;
				if( ! current_user_can( 'manage_options' ) ){
					$query_args[ 'wishlist_visibility' ] = 'visible';
				}
			}
		}
	}

	// counts number of elements in wishlist for the user
	$count = YITH_WCWL()->count_products( $wishlist_id );

	// retrieve wishlist information
	$wishlist_meta = YITH_WCWL()->get_wishlist_detail_by_token( $wishlist_id );

	// sets current page, number of pages and element offset
	$current_page = max( 1, get_query_var( 'paged' ) );

	// sets variables for pagination, if shortcode atts is set to yes
	if( $pagination == 'yes' && $count > 1 ){
		$pages = ceil( $count / $per_page );

		if( $current_page > $pages ){
			$current_page = $pages;
		}

		$offset = ( $current_page - 1 ) * $per_page;

		if( $pages > 1 ){
			$page_links = paginate_links( array(
				'base' => esc_url( add_query_arg( array( 'paged' => '%#%' ), YITH_WCWL()->get_wishlist_url( 'view' . '/' . $wishlist_id ) ) ),
				'format' => '?paged=%#%',
				'current' => $current_page,
				'total' => $pages,
				'show_all' => true
			) );
		}

		$query_args[ 'limit' ] = $per_page;
		$query_args[ 'offset' ] = $offset;
	}

	if( empty( $wishlist_id ) ){
		$wishlists = YITH_WCWL()->get_wishlists( array( 'user_id' => get_current_user_id(), 'is_default' => 1 ) );
		if( ! empty( $wishlists ) ){
			$wishlist_id = $wishlists[0]['wishlist_token'];
		}
	}

	// retrieve items to print
	$wishlist_items = YITH_WCWL()->get_products( $query_args );

	$wishlist_url = YITH_WCWL()->get_wishlist_url();

	ob_start();
	?>

	<ul class="cart_list product_list_widget" data-pagination="<?php echo esc_attr( $pagination )?>" data-per-page="<?php echo esc_attr( $per_page )?>" data-page="<?php echo esc_attr( $current_page )?>" data-id="<?php echo ( is_user_logged_in() ) ? esc_attr( $wishlist_meta['ID'] ) : '' ?>" data-token="<?php echo ( ! empty( $wishlist_meta['wishlist_token'] ) && is_user_logged_in() ) ? esc_attr( $wishlist_meta['wishlist_token'] ) : '' ?>">

	<?php
	if( count( $wishlist_items ) > 0 ) {
		foreach( $wishlist_items as $item ) {
			global $product;

			if( function_exists( 'wc_get_product' ) ) {
				$product = wc_get_product( $item['prod_id'] );
			}
			else{
				$product = get_product( $item['prod_id'] );
			}

			if( $product !== false && $product->exists() ) {
				$availability = $product->get_availability();
				$stock_status = $availability['class'];
				?>
				<li class="mini_cart_item" id="yith-wcwl-row-<?php echo esc_attr( $item['prod_id'] ) ?>" data-row-id="<?php echo esc_attr( $item['prod_id'] ) ?>">

					<?php if( $is_user_owner ): ?>
						<a href="<?php echo esc_url( add_query_arg( 'remove_from_wishlist', $item['prod_id'] ) ) ?>" class="remove" title="<?php esc_html_e( 'Remove this product', 'ux-shop' ) ?>">&times;</a>
					<?php endif; ?>

					<a href="<?php echo esc_url( get_permalink( apply_filters( 'woocommerce_in_cart_product', $item['prod_id'] ) ) ) ?>">
						<?php echo $product->get_image() ?><?php echo apply_filters( 'woocommerce_in_cartproduct_obj_title', $product->get_title(), $product ) ?>
					</a>
					
					<span class="product-price">
						<?php
						if( is_a( $product, 'WC_Product_Bundle' ) ){
							if( $product->min_price != $product->max_price ){
								echo sprintf( '%s - %s', wc_price( $product->min_price ), wc_price( $product->max_price ) );
							}
							else{
								echo wc_price( $product->min_price );
							}
						}
						elseif( $product->get_price() != '0' ) {
							echo $product->get_price_html();
						}
						else {
							echo apply_filters( 'yith_free_text', esc_html__( 'Free!', 'ux-shop' ) );
						}
						?>
					</span>

					<span class="product-stock-status">
						<?php
						if( $stock_status == 'out-of-stock' ) {
							$stock_status = "Out";
							echo '<span class="wishlist-out-of-stock">' . esc_html__( 'Out of Stock', 'ux-shop' ) . '</span>';
						} else {
							$stock_status = "In";
							echo '<span class="wishlist-in-stock">' . esc_html__( 'In Stock', 'ux-shop' ) . '</span>';
						}
						?>
					</span>

					<!-- Add to cart button -->
					<?php if( isset( $stock_status ) && $stock_status != 'Out' ): ?>
						<?php
						echo apply_filters( 'woocommerce_loop_add_to_cart_link',
							sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="%s button product_type_%s">%s</a>',
								esc_url( $product->add_to_cart_url() ),
								esc_attr( $product->get_id() ),
								esc_attr( $product->get_sku() ),
								esc_attr( isset( $quantity ) ? $quantity : 1 ),
								$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button ajax_add_to_cart' : '',
								esc_attr( woo_shrk_get_product_type( $product ) ),
								esc_html( $product->add_to_cart_text() )
								),
							$product );
						?>
					<?php endif ?>

				</li>
				<?php
				}
			}
		} else { ?>
			<li>
				<span class="wishlist-empty"><?php esc_html_e( 'No products were added to the wishlist', 'ux-shop' ) ?></span>
			</li>
		<?php
		}
	?>
	</ul>
	<?php if( count( $wishlist_items ) > 0 ) { ?>
	<p class="buttons">
		<a href="<?php echo esc_url($wishlist_url); ?>" class="button wc-forward"><?php esc_html_e('View Wishlist', 'ux-shop'); ?></a>
	</p>
	<?php }

	$response = array( 'count' => count( $wishlist_items ), 'html' => ob_get_clean() );
	wp_send_json( $response );
	exit;
}
add_action( 'wp_ajax_get_wishlist_items', 'woo_shrk_get_wishlist_items' );
add_action( 'wp_ajax_nopriv_get_wishlist_items', 'woo_shrk_get_wishlist_items' );

function shrk_woo_wishlist_wrap_add_to_cart( $button_text ) {
	return '<span class="btn-text">' . esc_html( $button_text, 'ux-shop' ) . '</span><div class="tooltip">' . esc_html( $button_text, 'ux-shop' ) . '</div>';
}
add_filter( 'variable_add_to_cart_text', 'shrk_woo_wishlist_wrap_add_to_cart' );
add_filter( 'yith_wcwl_add_to_cart_label', 'shrk_woo_wishlist_wrap_add_to_cart' );

?>