<?php


class WKP_Woo_Query {

    private static $instance = null;

    public static function init(){
        if(null === self::$instance ){
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function __construct(){
        
    }

    public function product_query_type($query_type, $num_of_product, $order_by){
        /**
         * recent
         */
        $recent_product_args = array(
            'post_type'             => 'product', 
            'post_status'           => 'publish',
            'ignore_sticky_posts'   => 1,
            'orderby'               => $order_by,
            'posts_per_page'        => $num_of_product,
        );
        /**
         * featured product
         */
        $featured_tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured',
            'operator' => 'IN', // or 'NOT IN' to exclude feature products
        );
        $featured_product_args = array(
            'post_type'             => 'product', 
            'post_status'           => 'publish',
            'ignore_sticky_posts'   => 1,
            'posts_per_page'        => $num_of_product,
            'orderby'                 => $order_by,
            'tax_query'             => $featured_tax_query,
        );
        /**
         * best selling
         */
        $best_selling_args = array(
            'post_type' => 'product',
            'meta_key' => 'total_sales',
            'orderby' => 'meta_value_num',
            'posts_per_page' => $num_of_product,
            'order' => 'DESC',
        );
        /**
         * Sale Product
         */
        $sale_product_args = array(
            'post_type' => 'product',
            'meta_key' => '_sale_price',
            'meta_value' => '0',
            'posts_per_page' => $num_of_product,
            'meta_compare' => '>=',
            // 'orderby'        => $order_by,
            // 'order'        => 'ASC',
            // 'post__in'          => array_merge( array( 0 ), wc_get_product_ids_on_sale() ),
        );
        /**
         * top rated
         */
        $top_rated_args = array(
            'post_type' => 'product',
            'meta_key' => '_wc_average_rating',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
            'posts_per_page' => $num_of_product,
        );

        switch ($query_type) {
            case "latest":
                return $recent_product_args;
                break;
            case "featured":
                return $featured_product_args;
                break;
            case "best_selling":
                return $best_selling_args;
                break;
            case "sale":
                return $sale_product_args;
                break;
            case "top_rated":
                return $top_rated_args;
                break;
            default:
                return $recent_product_args;
                break;
        }
    }

    public function category_query($num_of_cat, $order, $category_hide_empty){
        $args = array(
            'post_type' => 'product',
            'taxonomy' => 'product_cat',
            'posts_per_page' => $num_of_cat,
            'order'        => $order,
            'hide_empty' => $category_hide_empty
        );
        return $args;
    }
    /**
     * woo_products
     * query
     */
    public function advanced_products_query_type($product_query_parameter_arr ){
        $product_query_type = $product_query_parameter_arr[0];
        $product_num_to_show = $product_query_parameter_arr[1];
        $product_category_filter_rule = $product_query_parameter_arr[2];
        $select_product_category = $product_query_parameter_arr[3];
        $product_offset = $product_query_parameter_arr[4];
        $product_exclude_from_query = $product_query_parameter_arr[5];
        $product_query_filterby = $product_query_parameter_arr[6];
        $product_query_orderby = $product_query_parameter_arr[7];
        $product_query_order = $product_query_parameter_arr[8];
        $product_include_to_query = $product_query_parameter_arr[9];
        
        /**
         * All Product
         */
        $ADVANCED_PRODUCT_QUERY = array(
            'post_type'             => 'product', 
            'post_status'           => 'publish',
            'ignore_sticky_posts'   => 1,
            'offset' => $product_offset,
            'post__not_in' => $product_exclude_from_query,
            'posts_per_page'        => $product_num_to_show,
            'orderby'               => $product_query_orderby,
            'order'               => $product_query_order,
        );
        $featured_tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured',
            'operator' => 'IN',
        );
        if('sale' === $product_query_filterby){
            $ADVANCED_PRODUCT_QUERY['meta_key'] = '_sale_price';
            $ADVANCED_PRODUCT_QUERY['meta_value'] = '0';
            $ADVANCED_PRODUCT_QUERY['meta_compare'] = '>=';
        }elseif('featured' === $product_query_filterby){
            $ADVANCED_PRODUCT_QUERY['tax_query'] = $featured_tax_query;
        }
        
        if('custom_query' === $product_query_type){
            if($product_category_filter_rule && ('match_cat' === $product_category_filter_rule)){
                if('featured' === $product_query_filterby){
                    $ADVANCED_PRODUCT_QUERY['tax_query'] = array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'product_cat',
                            'field' => 'id',
                            'terms' => $select_product_category,
                        ),
                        $featured_tax_query
                    );
                }else{
                    $ADVANCED_PRODUCT_QUERY['tax_query'] = array(
                        array(
                            'taxonomy' => 'product_cat',
                            'field' => 'id',
                            'terms' => $select_product_category,
                        )
                    );
                }
            }else{
                if('featured' === $product_query_filterby){
                    $ADVANCED_PRODUCT_QUERY['tax_query'] = array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'product_cat',
                            'field' => 'id',
                            'terms' => $select_product_category,
                            'operator' => 'NOT IN',
                        ),
                        $featured_tax_query
                    );
                }else{
                    $ADVANCED_PRODUCT_QUERY['tax_query'] = array(
                        array(
                            'taxonomy' => 'product_cat',
                            'field' => 'id',
                            'terms' => $select_product_category,
                            'operator' => 'NOT IN',
                        )
                    );
                }
            }
        }elseif('manual_selection' === $product_query_type){
            $ADVANCED_PRODUCT_QUERY['post__in'] = $product_include_to_query;
        }
        return $ADVANCED_PRODUCT_QUERY;
        
    }

}