<?php

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






function woo_shrk_header_wishlist() {
	if ( !class_exists( 'YITH_WCWL' ) ) {
		return;
	}

	$wishlist_url = YITH_WCWL()->get_wishlist_url(); // Count updated via JS on page load

	?>
	<div class="header-icon header-wishlist">
		<ul class="navbar-nav nav">
			<li class="">
				<a href="<?php echo esc_url($wishlist_url); ?>" class="header-wishlist-button">
					<i class="ion-android-favorite-outline ion"></i>
					<span class="product-count"></span>
				</a>
			</li>
		</ul>
	</div>
	<?php
}


/**
 * Add cart to header
 */
function woo_shrk_header_cart() {
	global $woocommerce, $shrk_options;

	if ( !empty($shrk_options['woo_cart_display']) && $shrk_options['woo_cart_display'] != 'none' ) { ?>
	<div class="header-cart header-icon">
		<ul class="navbar-nav nav">
			<li class="woo-cart-menu-item woocommerce">
				<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="woo-cart">
					<div class="cart-icon <?php if ( !empty( $shrk_options['woo_cart_icon']['url'] ) ) { echo "custom"; }?> <?php echo ($woocommerce->cart->cart_contents_count>0 ? 'has-items' : '');?> ">
						<i class="default-icon <?php echo esc_attr( apply_filters( 'shrk-header-cart-icon-class', 'ion' ) ); ?>"></i>
						<?php if ( !empty( $shrk_options['woo_cart_icon']['url'] ) ) { ?>
							<img src="<?php echo esc_url($shrk_options['woo_cart_icon']['url']); ?>">
							<?php if ( !empty( $shrk_options['woo_cart_icon_transp']['url'] ) ) { ?>
								<img src="<?php echo esc_url($shrk_options['woo_cart_icon_transp']['url']); ?>" class="transparent-cart-icon">
							<?php } ?>
						<?php } ?>
						<span class="product-count"><?php echo absint( $woocommerce->cart->cart_contents_count ); ?></span>
					</div>
				</a>
				
				<?php if ( !empty($shrk_options['woo_cart_display']) && $shrk_options['woo_cart_display'] == 'auto' || $shrk_options['woo_cart_display'] == 'dropdown' ) { ?>
				<div class="cart-dropdown">
					<div class="cart-inner">
						<?php if ($woocommerce->cart->cart_contents_count == 0) { ?>
							<span class="empty"><?php esc_html_e('No products in the cart.', 'ux-shop'); ?></span>
						<?php } else { ?> 
							<span class="loading"><?php esc_html_e('Loading...', 'ux-shop'); ?></span>
						<?php } ?>
					</div>
				</div>
				<?php } ?>
			</li>
		</ul>
	</div>	
	<?php
	}
}

function woo_shrk_product_link() {
	?>
	<a href="<?php the_permalink(); ?>" class="button to-product"><?php echo esc_html__('View product', 'ux-shop'); ?></a>
	<?php
}


function woo_shrk_header_add_to_cart_fragment( $fragments ) {
	global $woocommerce;
	ob_start(); ?>
	<div class="cart-inner">
		<?php if ($woocommerce->cart->cart_contents_count == 0) { ?>
		<?php echo '<span class="empty">'.esc_html__('No products in the cart.', 'ux-shop').'</span>'; ?>
		<?php } else { ?>
			<div class="cart-products">
			<?php foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) { ?>                                          
				<?php if ($cart_item['data']->exists() && $cart_item['quantity']>0) {  ?>  
					<div class="cart-product">
						<?php  echo '<a class="cart-product-image" href="' . esc_url(get_permalink($cart_item['product_id'])).'">' . str_replace( array( 'http:', 'https:' ), '', $cart_item['data']->get_image() ) . '</a>'; ?>
						<div class="cart-product-meta"><?php 
							$product_title = $cart_item['data']->get_title();
							echo '<a class="cart-product-title clearfix" href="'.esc_url(get_permalink($cart_item['product_id'])).'">' . apply_filters('woocommerce_cart_widget_product_title', $product_title, $cart_item['data']) . '</a>';
							echo '<div class="cart-product-price">'.wc_price($cart_item['data']->get_price()).'</div>';
							echo '<div class="cart-product-quantity"> ' . esc_html__('x', 'ux-shop') . ' ' . esc_html( $cart_item['quantity'] ) . '</div>';
						?></div>
						<div class="cart-product-remove">
							<?php echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf('<a href="%s" class="remove" title="%s"><span class="icon-close"><i class="ion ion-close-round"></i></span></a>', esc_url( function_exists( 'wc_get_cart_remove_url' ) ? wc_get_cart_remove_url( $cart_item_key ) : $woocommerce->cart->get_remove_url( $cart_item_key ) ), esc_html__('Remove this item', 'ux-shop') ), $cart_item_key ); ?>
						</div>
					</div>
				<?php } /* end if */ ?>
			<?php } /* end foreach */ ?>
			</div>
			<div class="cart-footer">
				<a class="cart-contents clearfix" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_html_e('View your shopping cart', 'ux-shop'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'ux-shop'), $woocommerce->cart->cart_contents_count);?> - <?php echo wp_kses( $woocommerce->cart->get_cart_total(), shrk_kses_allowed() ); ?></a>
				<div class="cart-footer-buttons">
					<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="button"><?php esc_html_e('View Cart', 'ux-shop'); ?></a><a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="button alt"><?php esc_html_e( 'Checkout', 'ux-shop' ); ?></a>
				</div>
			</div>
		<?php } ?>
	</div>
	<?php
	$cart_contents = ob_get_clean();
	$fragments['.cart-dropdown .cart-inner'] = $cart_contents;
	$fragments['.side-panel-menu .cart-inner'] = $cart_contents;
	$fragments['.cart-icon .product-count'] = '<span class="product-count" data-product-count="' . esc_attr( $woocommerce->cart->cart_contents_count ) . '">' . esc_attr( $woocommerce->cart->cart_contents_count ) . '</span>';	
	$fragments['.woo-js-cart-count'] = esc_attr( $woocommerce->cart->cart_contents_count );	

	return $fragments;
	
}


if ( ! function_exists( 'woo_shrk_subcategories_list' ) ) {

	/**
	 * Display product sub categories as a list
	 *
	 * @subpackage	Loop
	 * @param array $args
	 * @return null|boolean
	 */
	function woo_shrk_subcategories_list( $args = array() ) {
		global $wp_query;

		$defaults = array(
			'force_display' => false
		);

		$args = wp_parse_args( $args, $defaults );

		extract( $args );

		// Main query only
		if ( ! is_main_query() && ! $force_display ) {
			return;
		}

		// Don't show when filtering, searching or when on page > 1 and ensure we're on a product archive
		if ( is_search() || ( ! is_product_category() && ! is_shop() ) ) {
			return;
		}

		// Find the category + category parent, if applicable
		$term 			= get_queried_object();
		$parent_id 		= empty( $term->term_id ) ? 0 : $term->term_id;

		// NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( http://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work
		$product_categories = get_categories( apply_filters( 'woocommerce_product_subcategories_args', array(
			'parent'       => $parent_id,
			'menu_order'   => 'ASC',
			'hide_empty'   => 0,
			'hierarchical' => 1,
			'taxonomy'     => 'product_cat',
			'pad_counts'   => 1
		) ) );

		if ( ! apply_filters( 'woocommerce_product_subcategories_hide_empty', false ) ) {
			$product_categories = wp_list_filter( $product_categories, array( 'count' => 0 ), 'NOT' );
		}

		if ( $product_categories ) {
			echo '<ul class="categories nav">';
			foreach ( $product_categories as $category ) {
				echo '<li>';
				woocommerce_template_loop_category_link_open( $category );
				echo esc_html( $category->name );
				if ( $category->count > 0 ) {
					echo apply_filters( 'woo_shrk_subcategories_list_count_html', ' <mark class="count">(' . esc_html( $category->count ) . ')</mark>', $category );
				}
				woocommerce_template_loop_category_link_close();
				echo '</li>';
			}
			echo '</ul>';
			return true;
		}
	}
}

if ( ! function_exists( 'woo_shrk_product_video_thumbnail' ) ) {
	function woo_shrk_product_video_thumbnail() {
		global $post, $product;

    	$videos = trim( get_post_meta( $post->ID, '_shrk_product_videos', true ) );

		if ( empty( $videos ) ) {
			return;
		}

		wp_enqueue_script( 'shrk-sc-video-popup' );

		$videos = explode( ' ', $videos );

		// Count existing thumbs
		$attachment_ids = woo_shrk_get_gallery_image_ids( $product );
		$columns        = apply_filters( 'woocommerce_product_thumbnails_columns', 3 );

		if ( !empty( $attachment_ids ) && count( $attachment_ids ) > 0 ) {
			$thumbs_count = count( $attachment_ids );
			$thumbs_count = has_post_thumbnail() ? $thumbs_count + 1 : $thumbs_count;
		} else {
			$thumbs_count = 0;
		}

		$loop = $thumbs_count;

		foreach ($videos as $video) {
			$classes   = array();
			$video_url = trim( $video );

			if ( !empty( $video_url ) ) {
				if ( $loop == 0 || $loop % $columns == 0 ) {
					$classes[] = 'first';
				}

				if ( ( $loop + 1 ) % $columns == 0 ){
					$classes[] = 'last';
				}

				echo apply_filters( 
						'woo_shrk_single_product_video_thumbnail_html', 
						sprintf( 
							'<a href="%s" class="shrk-video-popup image %s" data-video-popup-id="%s" data-video-popup-url="%s" data-video-autoplay="yes"><div class="thumbnail-bg"></div></a>', 
							esc_url( $video_url ), 
							esc_attr( implode( ' ', $classes ) ),
							'video-popup-singleproduct-' . $loop, 
							esc_url( $video_url )
							),
						esc_url( $video_url )
					);
				$loop++;
			}
		}
	}
}

function woo_shoppage_afterheader_content() {
	if ( shrk_get_theme_option( 'woo_shoppage_afterheader' ) == 'menu' && shrk_get_theme_option( 'woo_shoppage_afterheader_menu' ) ) {
		wp_nav_menu(
			array(
				'menu' => shrk_get_theme_option( 'woo_shoppage_afterheader_menu' ),
				'container_class' => '',
				'menu_class' => 'nav',
				'fallback_cb' => 'shrk_fallback_menu',
				'walker' => shrk_get_menu_walker()
				)
			);
	} elseif ( shrk_get_theme_option( 'woo_shoppage_afterheader' ) == 'categories' ) {
 		woo_shrk_subcategories_list();
	} else {
		// do nothing;
		return;
	}
}


function woo_products_afterheader_content() {
	if ( shrk_get_theme_option( 'woo_products_afterheader' ) == 'menu' && shrk_get_theme_option( 'woo_products_afterheader_menu' ) ) {
		wp_nav_menu(
			array(
				'menu' => shrk_get_theme_option( 'woo_products_afterheader_menu' ),
				'container_class' => '',
				'menu_class' => 'nav',
				'fallback_cb' => 'shrk_fallback_menu',
				'walker' => shrk_get_menu_walker()
				)
			);
	} elseif ( shrk_get_theme_option( 'woo_products_afterheader' ) == 'categories' ) {
 		woo_shrk_subcategories_list();
	} else {
		// do nothing;
		return;
	}
}


function woo_shrk_grid_list_choice() {
	?>
	<div class="grid-list-controls">
		<a href="#" class="grid <?php echo isset($_COOKIE['products_style_view']) && $_COOKIE['products_style_view'] == 'grid' ? 'active' : ''; ?>">
			<i class="ion-android-apps ion"></i>
		</a>
		<a href="#" class="list <?php echo isset($_COOKIE['products_style_view']) && $_COOKIE['products_style_view'] == 'list' ? 'active' : ''; ?>">
			<i class="ion-android-menu ion"></i>
		</a>
	</div>
	<?php 
}

function woo_shrk_list_product_description() {
	global $post;
	echo '<div class="description">' . apply_filters( 'woocommerce_short_description', $post->post_excerpt ) . '</div>';
}

function woo_shrk_print_notices() {
	?>
	<div class="woocommerce-message-floating autohide">
	<?php wc_print_notices(); ?>
	</div>
	<?php
}