<?php

namespace frontend\controllers;


use common\modules\product\helpers\ProductHelper;
use frontend\models\ProductFrontendSearch;
use Yii;
use common\modules\product\models\Brand;
use common\modules\product\models\BrandSearch;
use common\modules\product\models\Category;
use common\modules\product\models\Product;
use common\modules\rubrication\models\TaxGroup;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;

class CatalogController extends \yii\web\Controller
{


    public function actionSearch() {
        // @todo
    }

    public function actionCategory()
    {


        /** @var Category $category */
        $category = Yii::$app->request->get('category');
        $filter = Yii::$app->request->get('filters', [ ]);
        $filter_check = $filter;

        if(empty( $category->category_id ) && empty( $word )) {
            return $this->render('catalog');
        }

        ProductHelper::addLastCategory($category->category_id);

        $params = [ ];

        $optionsList = ArrayHelper::getColumn(TaxGroup::find()
            ->where([ 'is_filter' => 'TRUE' ])
            ->all(), 'alias');



        if(!empty( $filter[ 'special' ] )) {
            unset( $filter_check[ 'special' ] );
            if(!is_array($filter[ 'special' ])) {
                $filter[ 'special' ] = [ $filter[ 'special' ] ];
            }
            if(in_array('new', $filter[ 'special' ])) {
                $params[ 'special' ][ 'is_new' ] = true;
            }
            if(in_array('top', $filter[ 'special' ])) {
                $params[ 'special' ][ 'is_top' ] = true;
            }
            if(in_array('promo', $filter[ 'special' ])) {
                $params[ 'special' ][ 'akciya' ] = true;
            }
        }

        if(!empty( $filter[ 'prices' ] )) {
            unset( $filter_check[ 'prices' ] );
            $params[ 'prices' ] = $filter[ 'prices' ];
        }

        $activeFiltersParams = $filter_check;

        if(!empty( $filter[ 'brands' ] )) {
            unset( $filter_check[ 'brands' ] );
            $brands = Brand::find()
                ->select('brand_id')
                ->where([
                    'in',
                    'alias',
                    $filter[ 'brands' ],
                ])
                ->all();
            $params[ 'brands' ] = [ ];
            foreach($brands as $brand) {
                $params[ 'brands' ][] = $brand->brand_id;
            }
        }

        foreach($optionsList as $optionList) {

            if(isset( $filter[ $optionList ] )) {
                unset( $filter_check[ $optionList ] );
                $params[ $optionList ] = $filter[ $optionList ];
            }

        }

        if(!empty( $filter_check )) {
            $filter = array_diff_key($filter, $filter_check);
            Yii::$app->response->redirect([
                'catalog/category',
                'category' => $category,
                'filters'  => $filter,
            ], 301);
        }

        $productModel = new ProductFrontendSearch();

        $productProvider = $productModel->search($category, $params);


        $brandModel = new BrandSearch();
        $brands = $brandModel->getBrands($category, $params)
            ->all();


        $groups = $category->getActiveFilters();

        foreach($brands as $brand){
            array_unshift($groups , [
                'group_alias' => 'brands',
                'option_alias' => $brand->alias,
                'tax_option_id' => $brand->brand_id,
                'value' => $brand->name,
                'alias' => 'brands',
                'name' => 'Бренды'
            ]);
        }



        foreach ($groups as $key=> $group) {
            $param = $activeFiltersParams;
            if(isset($param[$group['alias']])){
                if(!in_array($group['option_alias'],$param[$group['alias']])){
                    $param[$group['alias']][] = $group['option_alias'];
                } else {
                    continue;
                }
            }else {
                $param = array_merge($param, [$group['alias']=>[$group['option_alias']]]);
            }

            $groups[$key] = array_merge($groups[$key],['cacheKey' => json_encode($param)] );

        }



        $groups = ArrayHelper::index($groups, null, 'name');




        $priceLimits = $productModel->priceLimits($category, $params);


        return $this->render('products', [
            'category'        => $category,
            'filter'          => $filter,
            'params'          => $params,
            'productModel'    => $productModel,
            'productProvider' => $productProvider,
            'groups'          => $groups,
            'priceLimits'     => $priceLimits,
        ]);

    }

    public function actionProduct()
    {
        /** @var Product $product */
        $product = Yii::$app->request->get('product');


        ProductHelper::addLastProsucts($product->product_id);

        $category = null;
        $last_category_id = ProductHelper::getLastCategory();
        if(!empty($last_category_id)) {
            $category = $product->getCategory()->andWhere(['category_id' => $last_category_id])->one();
        }
        if(empty($category)) {
            $category = $product->category;
        }

        return $this->render('product', [
            'product' => $product,
            'category' => $category,
        ]);
    }

    public function actionBrands()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Brand::find()->orderBy('name'),
            'pagination' => [
                'pageSize' => -1,
            ]
        ]);

        return $this->render('brands', [
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionBrand($brand)
    {
        $brand = BrandSearch::findByAlias($brand);

        $params = [
            'brands' => $brand->brand_id,
        ];

        $productModel = new ProductFrontendSearch();
        $productProvider = $productModel->search(null, $params);

        $priceLimits = $productModel->priceLimits(null, $params);

        return $this->render('brand', [
            'productModel'      => $productModel,
            'productProvider'   => $productProvider,
            'brand'             => $brand,
            'priceLimits'       => $priceLimits,
        ]);
    }

}
