<?php

namespace common\modules\product\helpers;

use common\modules\product\models\Brand;
use common\modules\product\models\Category;
use common\modules\product\models\Product;
use common\modules\product\models\ProductVariant;
use yii\base\Object;
use Yii;

class ProductHelper extends Object {
    public static function getCategories() {
        return Category::find()->with('categoryName')->getTree();
    }

    public static function getBrands() {
        return Brand::find()->with('brandName');
    }

    /*
     * Return custom filter-option link
     * @var array $filter
     * @var array $options
     * @return array
     */
    public static function getFilterForOption($filter, $key, $value, $remove = false) {
        $result = $filter;
        if (is_array($value)) {
            foreach($value as $value_key => $value_items) {
                if (!is_array($value_items)) {
                    $value_items = [$value_items];
                }
                foreach($value_items as $value_item) {
                    if ($remove && isset($result[$key]) && ($i = array_search($value_item, $result[$key][$value_key])) !== FALSE) {
                        unset($result[$key][$value_key][$i]);
                        if (empty($result[$key][$value_key])) {
                            unset($result[$key][$value_key]);
                        }
                    } else {
                        if (!isset($result[$key][$value_key]) || array_search($value_item, $result[$key][$value_key]) === FALSE) {
                            $result[$key][$value_key][] = $value_item;
                        }
                    }
                }
            }
        } else {
            if ($remove && isset($result[$key]) && ($i = array_search($value, $result[$key])) !== FALSE) {
                unset($result[$key][$i]);
                if (empty($result[$key])) {
                    unset($result[$key]);
                }
            } else {
                if (!isset($result[$key]) || array_search($value, $result[$key]) === FALSE) {
                    $result[$key][] = $value;
                }
            }
        }
        return $result;
    }

    public static function addLastProsucts($product_id) {
        $last_products = self::getLastProducts();
        if (!in_array($product_id, $last_products)) {
            $last_products[] = $product_id;
            Yii::$app->session->set('last_products', $last_products);
        }
    }

    public static function getLastProducts($as_object = false) {
        $last_products = Yii::$app->session->get('last_products', []);
        if ($as_object) {
            $last_products = Product::find()->where(['product_id' => $last_products])->all();
        }
        return $last_products;
    }

    public static function getSpecialProducts($type, $count, $sort = null) {
        $data = [$type => true];
        return Product::find()
            ->innerJoinWith('variants')
            ->where($data)
//            ->andWhere(['!=', ProductVariant::tableName() .'.stock', 0])
            ->limit($count)
            /*->orderBy($sort)*/
            ->all();
    }
}