<?php

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

include_once( 'admin/meta-config.php' );
include_once( 'admin/meta-tabs.php' );
include_once( 'inc/helpers.php' );
include_once( 'inc/deprecated.php' );
include_once( 'inc/template-functions.php' );
include_once( 'inc/custom-css.php' );
include_once( 'extras/yith-wishlist.php' );
include_once( 'extras/yith-quickview.php' );
include_once( 'extras/slides-image-grid.php' );

if ( function_exists( 'WC' ) ) {
	include_once( 'redux/woo.redux.config.php' );

	add_action( 'after_setup_theme', 'woo_shrk_setup', 8 );	
	add_action( 'after_setup_theme', 'woo_shrk_after_setup', 12 );	
	add_action( 'wp_enqueue_scripts', 'woo_shrk_scripts', 12 );
	add_action( 'widgets_init', 'woo_shrk_widgets_init' );
	
	add_action( 'shrk_header_right_content', 'woo_shrk_header_cart', 20 );

	add_action( 'shrk-pre-custom-css', 'woo_shrk_shop_columns_grid_css' );

	add_filter( 'woocommerce_add_to_cart_fragments', 'woo_shrk_header_add_to_cart_fragment' );
	add_filter( 'woocommerce_enqueue_styles', 'woo_shrk_dequeue_styles' );
	add_filter( 'woocommerce_output_related_products_args', 'woo_shrk_related_products_args' );	
	add_filter( 'woocommerce_pagination_args', 'woo_shrk_modify_pagination_args' );	

	// Product thumbnails columns
	add_filter( 'woocommerce_product_thumbnails_columns', function() { return 4; } );
	
	// Slides hooks
	add_action( 'shrk_slide_shop_loop_item_title',        'woocommerce_template_loop_product_title', 10 );
	add_action( 'shrk_slide_before_shop_loop_item',       'woocommerce_show_product_loop_sale_flash' );
	add_action( 'shrk_slide_before_shop_loop_item',       'woo_shrk_product_link' );
	add_action( 'shrk_slide_after_shop_loop_item_title',  'woocommerce_template_loop_price' );
	add_action( 'shrk_slide_after_shop_loop_item_title',  'woocommerce_template_single_excerpt', 15 );
	add_action( 'shrk_slide_after_shop_loop_item',        'shrk_get_clearfix_div', 5 );
	add_action( 'shrk_slide_after_shop_loop_item',        'woocommerce_template_loop_add_to_cart' );

	// Add extra product Meta boxes
	add_action('admin_menu', function () { shrk_add_meta_boxes( 'product' ); });

	// Product videos in thumbnails
	add_action( 'woocommerce_product_thumbnails', 'woo_shrk_product_video_thumbnail', 30 );

}






function woo_shrk_setup() {
	if ( function_exists( 'add_theme_support' ) ) {
		/**
		 * Woocommerce support
		 */
		add_theme_support( 'woocommerce' );
	}
	
	// single product

	// Add a wrapper around product images & summary for single
	add_action( 'woocommerce_before_single_product_summary', function () { 
		global $product;
		$sidethumbs = false;
		
		if ( shrk_get_theme_option('woo_single_side_thumbs') ) {
			$sidethumbs = ( function_exists( 'yith_ywzm_init' ) && get_option( 'yith_wcmg_enable_plugin' ) == 'yes' && get_option( 'yith_wcmg_enableslider' ) == 'yes' ) ? false : true;
		}
		echo '<div class="single-product-main clearfix ' . ( $sidethumbs ? 'side-thumbs' : '' ) . '">'; 

	}, 1 );

	add_action( 'woocommerce_after_single_product_summary', function () { 
		echo '</div>';  // close .single-product-main
	}, 1 );

	// Remove single meta info from the normal flow
	remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );

	// Add wrapper for new position of meta info
	add_action( 'woocommerce_after_single_product_summary', function () { 
		if ( is_product() && shrk_show_sidebar() == false ) {
			echo '<div class="single-meta-wrapper shrk-force-full-width vc_hidden">'; 
		} else {
			echo '<div class="single-meta-wrapper">';
		}
	}, 4 );

	// Add meta in new position
	add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_meta', 5 );

	// Close wrapper of meta
	add_action( 'woocommerce_after_single_product_summary', function () { 
		echo '</div>'; // close .single-meta-wrapper
	}, 6 );

	// Move rating to new position
	remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
	add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 4 );

	// Wrap add-to-cart and wishlist, share buttons
	add_action( 'woocommerce_before_add_to_cart_button', function () { echo '<div class="form-cart-wrapper">'; } );
	add_action( 'woocommerce_after_add_to_cart_button',  function () { echo '</div><!-- .form-cart-wrapper -->'; }, 12 );

	// Shop loop (content-product.php) hooks reordering & wrappers
	add_action( 'woocommerce_before_shop_loop_item_title', function () { echo '<div class="product-thumb-wrapper">'; }, 5 );
	add_action( 'woocommerce_before_shop_loop_item_title', function () { echo '</div>'; }, 35 );
	add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 36 ); // close link around thumb-wrapper
	remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 ); // Remove closing function from default position

	remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

	add_action( 'woocommerce_before_shop_loop_item_title', function () { echo '<div class="product-meta-wrapper"><div class="product-meta">'; }, 40 );
	add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 41); // link open before title, price & rating
	add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 14); // link close before new link from add2cart
	add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_add_to_cart', 16);
	add_action( 'woocommerce_after_shop_loop_item_title', function () { echo '</div></div>'; }, 20 );
	

	// store page-cover of product for product-cover-target
	add_action( 'woocommerce_after_shop_loop_item', function () {
		echo '<div class="page-cover-data hidden" data-page-cover="' . shrk_get_cover_image() . '"></div>';
	}, 50);
	
	// Product cat & tax description to page cover
	remove_action( 'woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10 );
	remove_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 );
	add_action( 'shrk-page-cover-after', 'woocommerce_taxonomy_archive_description', 15 );
	add_action( 'shrk-page-cover-after', 'woocommerce_product_archive_description', 15 );

	// Add wrapper around woocommerce_subcategory_thumbnail
	add_action( 'woocommerce_before_subcategory_title', function () { echo '<div class="cat-thumb-wrapper">'; }, 9 );
	add_action( 'woocommerce_before_subcategory_title', function () { echo '</div>'; }, 11 );

	// Remove product category count //TODO option
	add_filter( 'woocommerce_subcategory_count_html', function () { return ''; });

	// Remove page title as it is shown in page cover
	add_filter( 'woocommerce_show_page_title', function() { return false; } );

	// Setup stuff that needs the post object
	add_action( 'wp', 'woo_shrk_setup_obj' );

	// Add to cart button args
	add_filter( 'woocommerce_loop_add_to_cart_args', 'woo_shrk_loop_add_to_cart_args', 10, 2 );

	// Add notices to the footer on all pages.
	add_action( 'wp_footer', 'woo_shrk_print_notices' );

	// Change default placement of notices 
	add_action( 'shrk-wrapper-start-after-cover', function () {
		if ( shrk_get_theme_option( 'woo_notices_floating' ) != 1 ) {
			// Output notices
			wc_print_notices();
			
			// Remove them from footer as thery are printed on the header
			remove_action( 'wp_footer', 'woo_shrk_print_notices' );
		}

		// Remove them from default positions
		remove_action( 'woocommerce_shortcode_before_product_cat_loop', 'wc_print_notices', 10 );
		remove_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
		remove_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
	});
}	

/**
 * Run after theme options have been set.
 * @return none
 */
function woo_shrk_after_setup() {
	// Add HiDPI shop image sizes if needed.
	if ( shrk_get_theme_option( 'retina_support' ) == 1 ) {
		$woo_image_sizes = array( 'shop_catalog', 'shop_single', 'shop_thumbnail' );

		foreach ($woo_image_sizes as $woo_image_size) {
			$woo_image_size_lo_dpi = shrk_get_image_size( $woo_image_size );
			if ( isset( $woo_image_size_lo_dpi['width'] ) && isset( $woo_image_size_lo_dpi['height'] ) ) {
				add_image_size( $woo_image_size . '_hidpi', $woo_image_size_lo_dpi['width'] * 2, $woo_image_size_lo_dpi['height'] * 2, $woo_image_size_lo_dpi['crop'] ); 
			}
		}
	}
}

/**
 * Run after post object has been set (frontend)
 * @return none
 */
function woo_shrk_setup_obj() {
	global $product;

	if ( ! is_object( $product ) ) {
		$product = wc_get_product( get_the_ID() );	
	} 

	$product_view = shrk_get_mso( 'woo_products_style_view' );

	if ( !shrk_get_theme_option( 'disable_theme_select2', false ) ) {
		// Apply select2 to either all selects or only in variable product
		if ( apply_filters( 'woo_shrk_select2_globally', true ) ) {
			// On all selects
			add_action( 'wp_enqueue_scripts', 'woo_shrk_select2_scripts_global', 14 );
		} else {
			// On variable products only
			add_action( 'woocommerce_variable_add_to_cart', 'woo_shrk_select2_scripts_variableproducts', 14 );
		}
	}

	// Fade all products except the hovered one 
	if ( shrk_get_theme_option( 'woo_isolate_product_on_hover' ) == 1 ) {
		add_filter( 'body_class', function ( $c ) { 
			$c[] = 'li-isolation';
			return $c;
		});
	}

	if ( shrk_get_theme_option( 'woo_single_show_share_icons' ) == 1 ) { 
		if ( is_object( $product ) && $product->is_type( 'variable' ) ) {
			add_action( 'woocommerce_single_variation', 'shrk_get_share_buttons', 25 );
		} else {
			add_action( 'woocommerce_after_add_to_cart_button', 'shrk_get_share_buttons', 8 );
		}
	}

	if ( shrk_get_theme_option( 'woo_related_as_slider' ) == 1 ) {
		// Disable slides image grid for upsells & related 
		add_action( 'woocommerce_after_single_product_summary', 'woo_shrk_slides_image_grid_disable', 14 );
		add_action( 'woocommerce_after_single_product_summary', 'woo_shrk_slides_image_grid_enable', 21 ); //re-enable them.
	}

	if ( is_shop() && shrk_get_theme_option( 'woo_shoppage_afterheader' ) ) {
		add_action( 'shrk-page-cover-header-after', 'woo_shoppage_afterheader_content' );
	}

	if ( is_product_category() && shrk_get_theme_option( 'woo_products_afterheader' ) ) {
		add_action( 'shrk-page-cover-header-after', 'woo_products_afterheader_content' );
	}

	// Header cart icon
	if ( shrk_get_theme_option( 'woo_header_cart_icon' ) ) {
		add_filter( 'shrk-header-cart-icon-class', function( $c ) {
			$icon_class = shrk_get_theme_option( 'woo_header_cart_icon' );
			$c .= ' ' . $icon_class;
			return $c;
		});
	}

	// loop add-to-cart icon
	if ( shrk_get_theme_option( 'woo_loop_cart_icon' ) ) {
		add_filter( 'shrk-loop-add-to-cart-button-class', function( $c ) {
			$icon_class = shrk_get_theme_option( 'woo_loop_cart_icon' );
			$c .= ' ' . $icon_class;
			return $c;
		});
	}

	// Remove product image when product cover disabled
	// before body class to affect 'transparent-header' setting
	if ( is_product() && shrk_get_mso( 'product_page_cover_enabled' ) != '1' ) {
		add_filter( 'shrk-cover-image', function() {
			return false;
		});

		add_filter( 'shrk-cover-image-id', function() { 
			return false;
		});
	}

	// Don't show product cover at all.
	if ( is_product() && shrk_get_mso( 'product_page_cover_enabled' ) != '1') {
		add_filter( 'shrk_woocommerce_page_cover', function ($show) {
			return false;
		});
	}

	// Don't show product cover when no image is available.
	if ( is_product() && shrk_get_mso( 'product_cover_hide_when_noimage' ) == '1' && !shrk_has_cover_image() ) {
		add_filter( 'shrk_woocommerce_page_cover', function ($show) {
			return false;
		});
	}

	// Page cover bottom links position
	if ( shrk_get_mso( 'woo_afterheader_position' ) == 'bottom' ) {
		add_filter( 'shrk-page-cover-classes', function ( $classes ) {
			$classes[] = 'bottom-nav';
			return $classes;
		});
	}

	// Grid or list view?
	if ( $product_view == 'list' && ( is_product_category() || is_product_tag() || is_shop() ) ) {
		add_filter( 'shrk_loop_start_classes', function ( $c_str ) {
			if ( is_main_query() ) {
				$c_str .= ' ' . 'list';
			}

			return $c_str;
		});
	}

	// Add excerpt to loop product to use in list view
	if ( $product_view == 'list' || $product_view == 'manual' ) {
		add_action( 'woocommerce_after_shop_loop_item_title', 'woo_shrk_list_product_description', 15);
		if ( function_exists( 'woo_shrk_wishlist_button' ) ) {
			add_action( 'woocommerce_after_shop_loop_item_title', 'woo_shrk_wishlist_button', 18 );
		}
	}

	// Add manual controls for list/grid view
	if ( $product_view == 'manual' && ( is_product_category() || is_product_tag() || is_shop() ) ) {
		add_action( 'woocommerce_before_shop_loop', 'woo_shrk_grid_list_choice', 35 );
		add_filter( 'shrk_loop_start_classes', function ( $c_str ) {
			global $_COOKIE;
			if ( isset( $_COOKIE['products_style_view'] ) && is_main_query() ) {
				$c_str .= ' ' . ( $_COOKIE['products_style_view'] == 'list' ? 'list' : 'grid' );
			}

			return $c_str;
		});
	}

	// Wrap sale flash in single product
	if ( is_product() && shrk_get_theme_option( 'woo_product_badges_style' ) == 'badges-style-b' ) {
		add_filter( 'woocommerce_sale_flash', function( $sale_flash ) {
			return '<div class="onsale-wrapper">' . $sale_flash . '</div>';
		});
	}
	
	// Body classes
	add_filter( 'body_class', function ( $c ) {

		if ( shrk_get_mso( 'woo_transparent_products' ) == '1' ) {
			$c[] = 'products-transparent';
		}
		if ( shrk_get_mso( 'woo_meta_bg_products' ) == '1' ) {
			$c[] = 'products-meta-bg';
		}
		if ( shrk_get_mso( 'woo_hovershadow_products' ) == '1' ) {
			$c[] = 'products-hover-shadow';
		}
		if ( shrk_get_mso( 'woo_hovermeta_products' ) == '1' && shrk_get_mso( 'woo_products_style_view' ) == 'grid' ) {
			$c[] = 'products-hover-showmeta';
		}
		if ( shrk_get_mso( 'woo_products_loop_style' ) ) {
			$c[] = esc_attr( shrk_get_mso( 'woo_products_loop_style' ) );
		}
		if ( shrk_get_mso( 'woo_product_badges_style' ) ) {
			$c[] = esc_attr( shrk_get_mso( 'woo_product_badges_style' ) );
		}
		if ( is_product() && shrk_get_mso( 'woo_single_slider_arrows' ) == '1' ) {
			$c[] = 'single-swiper-mobile-no-arrows';
		}
		if ( shrk_get_mso( 'woo_single_add2cart_stretch' ) == '1' ) {
			$c[] = 'single-add2cart-stretched';
		}

		if ( shrk_get_theme_option( 'woo_single_show_qty_buttons', false ) ) {
			$c[] = 'shrk-wc-quantity-buttons';
		}

		// Set checkout page style
		if ( is_checkout() && shrk_get_theme_option( 'woo_checkout_style' ) ) {
			$c[] = esc_attr( shrk_get_mso( 'woo_checkout_style' ) );
		}

		$c[] = 'woocommerce'; // yes, on every page.
		$c[] = 'widget-left-media'; // TODO option
		return $c;
	});

	// Post classes 
	add_filter( 'post_class', function ( $c ) {
		global $product;

		if ( is_product() ) {
			$gallery_images = woo_shrk_get_gallery_image_ids( $product );
			if ( !$gallery_images ) {
				$c[] = 'one-image';
			}
		}

		return $c;
	});

	// Setup proper cover image and postid for options on shop page.
	if ( is_shop() ) {
		add_filter( 'shrk-cover-image', function ( $cover ) {
			if ( $post_thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( get_option( 'woocommerce_shop_page_id' ) ), 'shrk_section_bg' ) ) {
				return $post_thumbnail[0];	
			} else {
				return $cover;
			}
		});

		add_filter( 'shrk-cover-image-id', function ( $cover_id ) {
			if ( $post_thumbnail_id = get_post_thumbnail_id( get_option( 'woocommerce_shop_page_id' ) ) ) {
				return $post_thumbnail_id;	
			} else {
				return $cover_id;
			}
		});

		add_filter( 'shrk_mso_postid', function ( $postid ) { 
			return get_option( 'woocommerce_shop_page_id' );
		});
	}
}


function woo_shrk_scripts() {
	global $shrk_theme_info;
	
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

	wp_enqueue_script( 'shrk-woocommerce-scripts',  SHRK_THEME_MODULES_URI . 'woocommerce/js/woocommerce' . $suffix . '.js', array( 'shrk-theme-js', 'shrk-photoswipe', 'photoswipe-ui-default' ), $shrk_theme_info['Version'], true );
	wp_enqueue_script( 'shrk-swiper' );

	if ( is_product_category() || is_product_tag() || is_shop() ) {
		wp_enqueue_script( 'select2' );
	}

	wp_dequeue_script( 'prettyPhoto' );
	wp_dequeue_script( 'prettyPhoto-init' );
	wp_dequeue_style( 'nb-styles' );
	wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
	wp_deregister_style( 'select2' );

	if ( version_compare( WC()->version, '2.7.0' ) >= 0 ) {
		// After 2.7.0
		wp_register_style( 'select2', get_template_directory_uri() . '/css/vendor/select2/select2-40' . $suffix . '.css' , array( 'shrk-style' ), NULL );
	} else {
		wp_register_style( 'select2', get_template_directory_uri() . '/css/vendor/select2/select2-35' . $suffix . '.css' , array( 'shrk-style' ), NULL );
	}

	wp_enqueue_style( 'select2' );
}

function woo_shrk_dequeue_styles( $enqueue_styles ) {
	/**
	 * Remove woocommerce styles 
	 */
	unset( $enqueue_styles['woocommerce-general'] );
	unset( $enqueue_styles['woocommerce-layout'] );
	unset( $enqueue_styles['woocommerce-smallscreen'] );
	return $enqueue_styles;
}

function woo_shrk_select2_scripts_global() {
	wp_enqueue_script( 'select2' );
	wp_add_inline_script( 'shrk-woocommerce-scripts', 'if ( typeof( shrk ) !== "undefined" && typeof( shrk.woo ) !== "undefined" ) { shrk.woo.select2 = {}; shrk.woo.select2.global_enabled = 1; }' );
}

function woo_shrk_select2_scripts_variableproducts() {
	wp_enqueue_script( 'select2' );
	wp_add_inline_script( 'shrk-woocommerce-scripts', 'if ( typeof( shrk ) !== "undefined" && typeof( shrk.woo ) !== "undefined" ) { shrk.woo.select2 = {}; shrk.woo.select2.variations_enabled = 1; }' );
}

function woo_shrk_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Shop Sidebar', 'ux-shop' ),
		'id'            => 'sidebar-products',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
}

?>