<?php

namespace Hura8\Components\Product\Controller;

use Hura8\Components\Product\Model\ProductFilterModel;


class ProductFilterOptionsController {

    protected $_current_category_ids = [];

    protected $_list_product_ids = [];

    protected $_price_range = [];// [1000000, 5000000, 15000000, 25000000, 50000000];

    protected $_filters = [
        "price"         => array('max' => 0, 'min'=> 0),
        "brand"         => array(), // array(1,2,3,)
        "collection"    => array(), // array(1,2,3,)
        "supplier"      => array(), // array(1,2,3,)
        "rating"        => array(), // array(1,2,3,)
        "category"      => array(), // array(1,2,3,)
        "status"        => array(),  // array(1,2,3,)
        "query"         => "", // string search keyword
        "hotType"       => array(),// array(saleoff | not | new)
        "attribute"     => array(), // array(1,2,3,)
        "ids"           => array(), // array(1,2,3,)
        "promotion"     => "",
        "storeId"       => array(), // array(1,2,3,)
        "other_filter"  => array() // array(in-stock, has-promotion etc...)
    ];

    protected  $objProductFilterModel;

    public function __construct($filters, $current_category_ids = [], $list_product_ids = [], $price_range = []) {

        $this->objProductFilterModel = new ProductFilterModel();

        $this->_filters = $filters;
        $this->_current_category_ids = $current_category_ids;
        $this->_list_product_ids = $list_product_ids;
        $this->_price_range = $price_range;
    }


    public function ratingList() {

        $neededList = array();

        foreach ( $this->objProductFilterModel->ratingList($this->_list_product_ids) as $result ) {
            $neededList[] = array(
                "rating" 	=> $result['review_rate'] ,
                "count"     => $result['num'] ,
                'is_selected'   => in_array($result['review_rate'], $this->_filters['rating']),
            );
        }

        return $neededList;
    }


    public function categoryList() {

        if(!sizeof($this->_list_product_ids)) {
            return [];
        }

        return $this->objProductFilterModel->categoryList($this->_filters['category'], $this->_list_product_ids);
    }


    public function brandList() {

        if(!sizeof($this->_list_product_ids)) {
            return [];
        }

        return $this->objProductFilterModel->brandList($this->_filters['brand'], $this->_list_product_ids);
    }


    public function attributeList() {

        return $this->objProductFilterModel->attributeList(
            $this->_filters['attribute'],
            $this->_current_category_ids,
            $this->_list_product_ids
        );
    }


    public function priceList() {

        return $this->objProductFilterModel->priceList(
            $this->_filters['price']['max'] ,
            $this->_filters['price']['min'],
            $this->_list_product_ids,
            $this->_price_range
        );

    }


}
