<?php
    
    namespace common\models;
    
    use common\modules\product\models\ProductVariant;
    use yii\base\Component;
    use yii\web\NotFoundHttpException;
    
    class Basket extends Component
    {
        
        public $session;
        
        public function __construct(array $config = [ ])
        {
            $this->session = \Yii::$app->session;
            if(!$this->session->has('basket')) {
                $this->session->set('basket', [ ]);
            }
            parent::__construct($config);
        }
        
        public function add(int $product_variant_id, int $count)
        {
            $data = $this->getData();
            if(array_key_exists($product_variant_id, $data)) {
                $data[ $product_variant_id ][ 'count' ] += $count;
                if($data[$product_variant_id]['count'] <= 0) {
                    unset($data[$product_variant_id]);
                }
            } else {
                $model = $this->findModel($product_variant_id);
                $data[ $product_variant_id ] = [
                    'price' => $model->price,
                    'count' => $count,
                ];
            }
            $this->setData($data);
        }
    
        public function set(int $product_variant_id, int $count)
        {
            $data = $this->getData();
            if(array_key_exists($product_variant_id, $data)) {
                $data[ $product_variant_id ][ 'count' ] = $count;
                if($data[$product_variant_id]['count'] <= 0) {
                    unset($data[$product_variant_id]);
                }
            } elseif($count > 0) {
                $model = $this->findModel($product_variant_id);
                $data[ $product_variant_id ] = [
                    'price' => $model->price,
                    'count' => $count,
                ];
            }
            $this->setData($data);
        }
        
        public function delete(int $product_variant_id) {
            $this->set($product_variant_id, 0);
        }
        
        public function getData(): array
        {
            return $this->session->get('basket');
        }
        
        public function getItem(int $product_variant_id) {
            $data = $this->getData();
            if(!empty($data[$product_variant_id])) {
                return $data[$product_variant_id];
            } else {
                return false;
            }
        }
        
        public function setData(array $data)
        {
            $this->session->set('basket', $data);
        }
        
        public function getSum(): float
        {
            $data = $this->getData();
            $sum = 0;
            foreach($data as $item) {
                $sum += $item[ 'price' ] * $item[ 'count' ];
            }
            return $sum;
        }
        
        public function getCount(): int {
            $data = $this->getData();
            return count($data);
        }
        
        public function findModel(int $product_variant_id): ProductVariant
        {
            $model = ProductVariant::find()
                                   ->where([ 'product_variant_id' => $product_variant_id ])
                                   ->one();
            if(empty( $model )) {
                throw new NotFoundHttpException('Product not found');
            } else {
                return $model;
            }
        }
        
        public function findModels(array $product_variant_ids)
        {
            return ProductVariant::find()
                                 ->where([ 'product_variant_id' => $product_variant_ids ])
                                 ->with([
                                     'product',
                                     'image',
                                 ])
                                 ->all();
        }
        
        public function clear()
        {
            $this->setData([]);
        }
        
    }