<?php
/**
 * Extend and customize WooCommerce
 */
class Estana_Extensions_WooCommerce {
	protected static $init = false;

	public static function init()
	{
		//TODO Fix this Cheat
		$product_list = estana_get_options('nbcore_product_list');

		remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);
		remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);
		remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10);
		remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
		remove_action('woocommerce_cart_collaterals', 'woocommerce_cross_sell_display');
		remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10);
		remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5);

		add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
		add_filter( 'woocommerce_before_main_content', 'estana_page_title', 5 );
		add_filter( 'loop_shop_columns', array(__CLASS__, 'loop_columns') );
		add_filter( 'loop_shop_per_page', array(__CLASS__, 'products_per_page'), 20 );
		add_filter( 'woocommerce_pagination_args', array(__CLASS__, 'woocommerce_pagination') );
		add_filter('woocommerce_product_description_heading', '__return_empty_string');
		add_filter('woocommerce_product_additional_information_heading', '__return_empty_string');
		add_filter('woocommerce_review_gravatar_size', array(__CLASS__, 'wc_review_avatar_size'));
		add_filter('woocommerce_cross_sells_total', array(__CLASS__, 'cross_sells_limit'));
		add_filter('woocommerce_upsells_total', array(__CLASS__, 'upsells_limit'));
		add_filter('yith_add_quick_view_button_html', array(__CLASS__, 'quickview_button'), 10, 3);
		add_filter('yith_quick_view_loader_gif', '__return_empty_string');
		add_action('woocommerce_after_shop_loop_item', array(__CLASS__, 'product_action_div_open'), 6);
		add_action('woocommerce_after_shop_loop_item', array(__CLASS__, 'product_action_div_close'), 50);
		add_action('woocommerce_after_shop_loop_item', array(__CLASS__, 'wishlist_button'), 7);
		add_action('woocommerce_after_add_to_cart_button', array(__CLASS__, 'wishlist_button_single'), 10);
		add_action('woocommerce_shop_loop_item_title', array(__CLASS__, 'product_title'), 10);
		add_action('woocommerce_after_shop_loop_item_title', array(__CLASS__, 'product_desc'), 15);
		add_action('woocommerce_before_main_content', array(__CLASS__, 'shop_banner'), 15);
		add_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 15);
		add_action('woocommerce_single_product_summary', array(__CLASS__, 'wide_meta_left_div_open'), 9);
		add_action('woocommerce_single_product_summary', array(__CLASS__, 'wide_meta_left_div_close'), 24);
		add_action('woocommerce_single_product_summary', array(__CLASS__, 'wide_meta_right_div_open'), 26);
		add_action('woocommerce_single_product_summary', array(__CLASS__, 'wide_meta_right_div_close'), 55);
		add_action('woocommerce_share', array(__CLASS__, 'wc_share_social'));
		add_action('woocommerce_cart_collaterals', 'woocommerce_cross_sell_display', 15);
		add_action('wp_footer', array(__CLASS__, 'add_cart_notice'));

        //TODO Fix this hack?
        add_filter('woocommerce_in_cart_product', array(__CLASS__, 'remove_wishlist_quickview'), 50, 1 );
        $attrs = estana_get_options('nbcore_pa_swatch_style');
		if ($attrs && $attrs!=''):
			$attrs = (array)json_decode($attrs);
		endif;
        if(is_array($attrs))
        {
            foreach($attrs as $key => $val)
            {
                if($val)
                {
                    add_action( $key.'_add_form_fields', array( __CLASS__, 'add_attribute_fields' ) );
                    add_action( $key.'_edit_form_fields', array( __CLASS__, 'edit_attribute_fields' ), 10 );
                    add_action( 'created_term', array( __CLASS__, 'save_attribute_fields' ), 10, 3 );
                    add_action( 'edit_term', array( __CLASS__, 'save_attribute_fields' ), 10, 3 );
                }
            }
        }
		add_action( 'wp_ajax_woocommerce_add_to_cart_variable_rc', array( __CLASS__, 'woocommerce_add_to_cart_variable_rc_callback' ));
		add_action( 'wp_ajax_nopriv_woocommerce_add_to_cart_variable_rc', array( __CLASS__, 'woocommerce_add_to_cart_variable_rc_callback' ));
		if(class_exists('WeDevs_Dokan')){
			remove_filter('woocommerce_product_tabs', 'dokan_seller_product_tab');
			add_action('woocommerce_single_product_summary', 'dokan_product_seller_tab', 60);
			add_filter( 'pre_get_posts', array( __CLASS__, 'store_query_filter' ), 20 );
			add_action( 'wp_ajax_dokan_seller_listing_search', array( __CLASS__, 'seller_listing_search') );
			add_action( 'wp_ajax_nopriv_dokan_seller_listing_search', array( __CLASS__, 'seller_listing_search') );
		}
	}

	public static function remove_wishlist_quickview($a){
        add_filter('yith_add_quick_view_button_html',  '__return_empty_string', 50, 3);
    }

	public static function loop_columns()
	{
		if (function_exists('dokan_is_store_page') && dokan_is_store_page()) {
			return estana_get_options('nbcore_vendor_loop_columns');
		} else {
			return estana_get_options('nbcore_loop_columns');
		}
	}

	public static function loop_columns_resp()
	{
		if (function_exists('dokan_is_store_page') && dokan_is_store_page()) {
			$nbcore_vendor_loop_columns['xl'] = estana_get_options('nbcore_vendor_loop_columns_xl');
			$nbcore_vendor_loop_columns['lg'] = estana_get_options('nbcore_vendor_loop_columns_lg');
			$nbcore_vendor_loop_columns['md'] = estana_get_options('nbcore_vendor_loop_columns_md');
			$nbcore_vendor_loop_columns['sm'] = estana_get_options('nbcore_vendor_loop_columns_sm');
			// return estana_get_options('nbcore_vendor_loop_columns');
		} else {
			$nbcore_vendor_loop_columns['xl'] = estana_get_options('nbcore_loop_columns_xl');
			$nbcore_vendor_loop_columns['lg'] = estana_get_options('nbcore_loop_columns_lg');
			$nbcore_vendor_loop_columns['md'] = estana_get_options('nbcore_loop_columns_md');
			$nbcore_vendor_loop_columns['sm'] = estana_get_options('nbcore_loop_columns_sm');
			// return estana_get_options('nbcore_loop_columns');
		}
		return $nbcore_vendor_loop_columns;
	}

	public static function product_action_div_open()
	{
		echo '<div class="product-action">';
	}

	public static function product_action_div_close()
	{
		echo '</div>';
	}

    public static function wishlist_button()
    {
        if(estana_get_options('product_category_wishlist')) {
            if ( class_exists( 'YITH_WCWL' ) ) {
                echo '<div class="wishlist-btn button bt-4">' . do_shortcode( '[yith_wcwl_add_to_wishlist]' ) . '</div>';
            }
        }
	}

    public static function wishlist_button_single()
    {
        if( class_exists( 'YITH_WCWL' ) ) {
            if ( get_option( 'yith_wcwl_button_position' ) == 'shortcode' ) {
                echo do_shortcode( '[yith_wcwl_add_to_wishlist]' );
            }
        }
	}

	public static function product_title()
	{
		echo '<h4 class="product-title"><a href="' . esc_url(get_the_permalink()) . '">' . esc_html(get_the_title()) . '</a></h4>';
	}

	public static function product_desc()
	{
		global $product_desc, $product_desc_limit;
		$product_desc = isset($product_desc) ? $product_desc : (estana_get_options('nbcore_grid_product_description') ? estana_get_options('nbcore_grid_product_description') : '');
		$product_desc_limit = isset($product_desc_limit) ? $product_desc_limit : '';
		if($product_desc):
			if (get_the_excerpt()):
				if ($product_desc_limit):
					echo '<p class="product-description">' . wp_trim_words(get_the_excerpt(),$product_desc_limit, '...') . '</p>';
				else :
					echo '<p class="product-description">' . esc_html(get_the_excerpt()) . '</p>';
				endif;
			else :
				if ($product_desc_limit):
					echo '<p class="product-description">' . wp_trim_words(get_the_content(),$product_desc_limit, '...') . '</p>';
				else :
					echo '<p class="product-description">' . wp_trim_words(get_the_content(),15, '...') . '</p>';
				endif;
			endif;
		endif;
	}

	public static function products_per_page($cols)
	{
		if (function_exists('dokan_is_store_page') && dokan_is_store_page()) {
			return estana_get_options('nbcore_vendor_per_page');
		} else {
			return estana_get_options('nbcore_products_per_page');
		}
	}
	
	public static function store_query_filter( $query ) {
        global $wp_query;

        $author = get_query_var( dokan_get_option( 'custom_store_url', 'dokan_general', 'store' ) );

        if ( !is_admin() && $query->is_main_query() && !empty( $author ) ) {
            $seller_info  = get_user_by( 'slug', $author );
            $store_info   = dokan_get_store_info( $seller_info->data->ID );
            $post_per_page = isset( $store_info['store_ppp'] ) && !empty( $store_info['store_ppp'] ) ? $store_info['store_ppp'] : estana_get_options('nbcore_vendor_per_page');
            set_query_var( 'posts_per_page', $post_per_page );
            $query->set( 'post_type', 'product' );
            $query->set( 'author_name', $author );
            $query->query['term_section'] = isset( $query->query['term_section'] ) ? $query->query['term_section'] : array();

            if ( $query->query['term_section'] ) {
                $query->set( 'tax_query',
                    array(
                        array(
                            'taxonomy' => 'product_cat',
                            'field'    => 'term_id',
                            'terms'    => $query->query['term']
                        )
                    )
                );
            }
        }
    }

	public static function woocommerce_pagination()
	{
		return array(
			'prev_text' => '<i class="icon-angle-left"></i>',
			'next_text' => '<i class="icon-angle-right"></i>',
			'end_size' => 1,
			'mid_size' => 1,
		);
	}

	public static function product_description()
	{
		echo '<p class="product-description">' . esc_html(get_the_excerpt()) . '</p>';
	}

	public static function product_category()
	{
		global $post;
		$terms = get_the_terms( $post->ID, 'product_cat' );
		foreach ($terms as $term) {
			echo '<a class="product-category-link" href="' . esc_url(get_term_link($term->term_id)) . '">' . esc_html($term->name) . '</a>';
		}
	}

	public static function shop_banner()
	{
		if(function_exists( 'is_shop' ) && is_shop()) {
			$shop_banner_url = estana_get_options('nbcore_shop_banner');
			if ($shop_banner_url) {
				echo '<div class="shop-banner"><img src="' . esc_url(wp_get_attachment_url(absint($shop_banner_url))) . '" /></div>';
			}
		}
	}

	public static function wc_review_avatar_size()
	{
		return '80';
	}

	public static function wide_meta_left_div_open()
	{
		if('wide' === estana_get_options('nbcore_pd_meta_layout')) {
			echo '<div class="pd-meta-left">';
		}
	}

	public static function wide_meta_left_div_close()
	{
		if('wide' === estana_get_options('nbcore_pd_meta_layout')) {
			echo '</div>';
		}
	}

	public static function wide_meta_right_div_open()
	{
		if('wide' === estana_get_options('nbcore_pd_meta_layout')) {
			echo '<div class="pd-meta-right">';
		}
	}

	public static function wide_meta_right_div_close()
	{
		if('wide' === estana_get_options('nbcore_pd_meta_layout')) {
			echo '</div>';
		}
	}

	public static function wc_share_social()
	{
		if(estana_get_options('nbcore_pd_show_social')) {
			if (function_exists('nbcore_share_social')){
				$style = estana_get_options('share_buttons_style');
				$position = estana_get_options('share_buttons_position');
				nbcore_share_social($style,$position);
			}
		}
	}

	public static function cross_sells_limit()
	{
		$cross_sells_limit = estana_get_options('nbcore_cross_sells_limit');
		return $cross_sells_limit;
	}

	public static function upsells_limit()
	{
		$upsells_limit = estana_get_options('nbcore_upsells_limit');
		return $upsells_limit;
	}

    public static function quickview_button($button, $label, $product)
    {
        $html = '';
        if(estana_get_options('product_category_quickview')) {
            global $product;

            $product_id = yit_get_prop( $product, 'id', true );

            $html = '<a href="#" class="button yith-wcqv-button bt-4" data-product_id="' . $product_id . '"><i class="icon-resize"></i><span class="tooltip">' . $label . '</span></a>';

        }
        return $html;
	}

	public function upload_scripts()
    {
        wp_enqueue_script('media-upload');
        wp_enqueue_media();
    }
	
	public static function add_attribute_fields() {

        ?>

        <div class="form-field">
            <label><?php esc_html_e( 'Thumbnail', 'estana' ); ?></label>
            <div id="product_cat_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( wc_placeholder_img_src() ); ?>" width="60px" height="60px" /></div>
            <div style="line-height: 60px;">
                <input type="hidden" name="is_attribute" value="1">
                <input type="hidden" id="product_attribute_thumbnail_id" name="product_attribute_thumbnail_id" />
                <button type="button" class="upload_image_button button"><?php esc_html_e( 'Upload/Add image', 'estana' ); ?></button>
                <button type="button" class="remove_image_button button"><?php esc_html_e( 'Remove image', 'estana' ); ?></button>
            </div>
			
			<?php
			$inline_script = '( function( $ ) {'
				. '"use strict";'
				. 'if ( ! $( "#product_attribute_thumbnail_id" ).val() ) {'
					. '$( ".remove_image_button" ).hide();'
				. '}'
				. 'var file_frame;'
				. ' $( document ).on( "click", ".upload_image_button", function( event ) {'
					. 'event.preventDefault();'
					. 'if ( file_frame ) {'
						. 'file_frame.open();'
						. 'return;'
					. '}'
					. 'file_frame = wp.media.frames.downloadable_file = wp.media({'
						. 'title: "' . __( "Choose an image", 'estana' ) . '",'
						. 'button: {'
							. 'text: "' . __( "Use image", 'estana' ) . '"'
						. '},'
						. 'multiple: false'
					. '});'
					. 'file_frame.on( "select", function() {'
						. 'var attachment = file_frame.state().get( "selection" ).first().toJSON();'
						. '$( "#product_attribute_thumbnail_id" ).val( attachment.id );'
						. 'if (typeof attachment.sizes.thumbnail !== "undefined"){'
							. '$( "#product_cat_thumbnail img" ).attr( "src", attachment.sizes.thumbnail.url );'
						. '} else {'
							. '$( "#product_cat_thumbnail img" ).attr( "src", attachment.url );'
						. '}'
						. '$( ".remove_image_button" ).show();'
					. '});'
					. 'file_frame.open();'
				. '});'
				. '$( document ).on( "click", ".remove_image_button", function() {'
					. '$( "#product_cat_thumbnail img" ).attr( "src", "' . esc_js( wc_placeholder_img_src() ) . '" );'
					. '$( "#product_attribute_thumbnail_id" ).val( "" );'
					. '$( ".remove_image_button" ).hide();'
					. 'return false;'
				. '});'
			. '} )( jQuery );';
			wp_enqueue_script( 'estana_admin_inline_script' );
			wp_add_inline_script( 'estana_admin_inline_script', $inline_script );
			?>
            <div class="clear"></div>
        </div>
    <?php
    }
	
	public static function edit_attribute_fields( $term ) {
        $thumbnail_id = absint( get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true ) );

        if ( $thumbnail_id ) {
            $image = wp_get_attachment_thumb_url( $thumbnail_id );
        } else {
            $image = wc_placeholder_img_src();
        }
        ?>

        <tr class="form-field">
            <th scope="row" valign="top"><label><?php esc_html_e( 'Thumbnail', 'estana' ); ?></label></th>
            <td>
                <div id="product_cat_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( $image ); ?>" width="60px" height="60px" /></div>
                <div style="line-height: 60px;">
                    <input type="hidden" name="is_attribute" value="1">
                    <input type="hidden" id="product_attribute_thumbnail_id" name="product_attribute_thumbnail_id" value="<?php echo esc_attr($thumbnail_id); ?>" />
                    <button type="button" class="upload_image_button button"><?php esc_html_e( 'Upload/Add image', 'estana' ); ?></button>
                    <button type="button" class="remove_image_button button"><?php esc_html_e( 'Remove image', 'estana' ); ?></button>
                </div>
                <div class="clear"></div>
            </td>
        </tr>
		<?php
		$inline_script = '( function( $ ) {'
			. '"use strict";'
			. 'if ( "0" === $( "#product_attribute_thumbnail_id" ).val() ) {'
				. '$( ".remove_image_button" ).hide();'
			. '}'
			. 'var file_frame;'
			. '$( document ).on( "click", ".upload_image_button", function( event ) {'
				. 'event.preventDefault();'
				. 'if ( file_frame ) {'
					. 'file_frame.open();'
					. 'return;'
				. '}'
				. 'file_frame = wp.media.frames.downloadable_file = wp.media({'
					. 'title: "' . __( "Choose an image", 'estana' ) . '",'
					. 'button: {'
						. 'text: "' . __( "Use image", 'estana' ) . '"'
					. '},'
					. 'multiple: false'
				. '});'
				. 'file_frame.on( "select", function() {'
					. 'var attachment = file_frame.state().get( "selection" ).first().toJSON();'
					. '$( "#product_attribute_thumbnail_id" ).val( attachment.id );'
					. '$( "#product_cat_thumbnail img" ).attr( "src", attachment.url );'
					. '$( ".remove_image_button" ).show();'
				. '});'
				. 'file_frame.open();'
			. '});'
			. '$( document ).on( "click", ".remove_image_button", function() {'
				. '$( "#product_cat_thumbnail img" ).attr( "src", "' . esc_js( wc_placeholder_img_src() ) . '" );'
				. '$( "#product_attribute_thumbnail_id" ).val( "" );'
				. '$( ".remove_image_button" ).hide();'
				. 'return false;'
			. '});'
		. '} )( jQuery );';
		wp_enqueue_script( 'estana_admin_inline_script' );
		wp_add_inline_script( 'estana_admin_inline_script', $inline_script );
		?>
    <?php
    }
	
    public function save_attribute_fields( $term_id, $tt_id = '', $taxonomy = '' ) {
        if ( isset( $_POST['product_attribute_thumbnail_id'] ) && isset($_POST['is_attribute']) && $_POST['is_attribute'] == 1 ) {
            update_woocommerce_term_meta( $term_id, 'thumbnail_id', absint( $_POST['product_attribute_thumbnail_id'] ) );
        }
    }
	
	function woocommerce_add_to_cart_variable_rc_callback() {
		
		ob_start();
		
		$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
		$quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] );
		$variation_id = $_POST['variation_id'];
		$variation  = $_POST['variation'];
		$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
	
		if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation  ) ) {
			do_action( 'woocommerce_ajax_added_to_cart', $product_id );
			if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
				wc_add_to_cart_message( $product_id );
			}
	
			// Return fragments
			WC_AJAX::get_refreshed_fragments();
		} else {
			$this->json_headers();
	
			// If there was an error adding to the cart, redirect to the product page to show any errors
			$data = array(
				'error' => true,
				'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
				);
			echo json_encode( $data );
		}
		die();
	}

	public static function add_cart_notice()
    {
        global $woocommerce
        ?>
        <div class="cart-notice-wrap">
            <div class="cart-notice">
                <p><?php esc_html_e('Product has been added to cart', 'estana'); ?></p>
                <p class="cart-url button"><a href="<?php echo wc_get_cart_url(); ?>"><?php esc_html_e('View Cart', 'estana'); ?></a></p>
                <span><i class="icon-cancel-circle"></i></span>
            </div>
        </div>
        <?php
    }
	
	/**
     * Search seller listing
     *
     * @return void
     */
    public function seller_listing_search() {
        if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'dokan-seller-listing-search' ) ) {
            wp_send_json_error( __( 'Error: Nonce verification failed', 'estana' ) );
        }

        $paged  = 1;
        $limit  = isset( $_REQUEST['limit'] ) ? sanitize_text_field( $_REQUEST['limit'] ) : '3';;
        $offset = ( $paged - 1 ) * $limit;

        $seller_args = array(
            'number' => $limit,
            'offset' => $offset
        );

        $search_term = isset( $_REQUEST['search_term'] ) ? sanitize_text_field( $_REQUEST['search_term'] ) : '';
        $pagination_base = isset( $_REQUEST['pagination_base'] ) ? sanitize_text_field( $_REQUEST['pagination_base'] ) : '';
        $per_row_xl = isset( $_REQUEST['per_row_xl'] ) ? sanitize_text_field( $_REQUEST['per_row_xl'] ) : '3';
        $per_row_lg = isset( $_REQUEST['per_row_lg'] ) ? sanitize_text_field( $_REQUEST['per_row_lg'] ) : '3';
        $per_row_md = isset( $_REQUEST['per_row_md'] ) ? sanitize_text_field( $_REQUEST['per_row_md'] ) : '3';
        $per_row_sm = isset( $_REQUEST['per_row_sm'] ) ? sanitize_text_field( $_REQUEST['per_row_sm'] ) : '3';
        $style = isset( $_REQUEST['style'] ) ? sanitize_text_field( $_REQUEST['style'] ) : '3';

        if ( '' != $search_term ) {

            $seller_args['meta_query'] = array(

                array(
                    'key'     => 'dokan_enable_selling',
                    'value'   => 'yes',
                    'compare' => '='
                ),

                array(
                    'key'     => 'dokan_store_name',
                    'value'   => $search_term,
                    'compare' => 'LIKE'
                )

            );
        }

        $sellers = dokan_get_sellers( $seller_args );

        $template_args = apply_filters( 'dokan_store_list_args', array(
            'sellers'         => $sellers,
            'limit'           => $limit,
            'paged'           => $paged,
            'image_size'      => 'medium',
            'search'          => 'yes',
            'pagination_base' => $pagination_base,
            'per_row_xl'      => $per_row_xl,
            'per_row_lg'      => $per_row_lg,
            'per_row_md'      => $per_row_md,
            'per_row_sm'      => $per_row_sm,
            'search_query'    => $search_term,
			'style'			  => $style
        ) );

        ob_start();
        dokan_get_template_part( 'store-lists-loop', false, $template_args );
        $content = ob_get_clean();

        wp_send_json_success( $content );
    }
}