<?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)
        {
            $data = $this->getData();
            if(array_key_exists($product_variant_id, $data)) {
                $data[ $product_variant_id ][ 'count' ] = 1;
                if($data[ $product_variant_id ][ 'count' ] <= 0) {
                    unset( $data[ $product_variant_id ] );
                }
            } else {
                if($this->findModel($product_variant_id)) {
                    $data[ $product_variant_id ] = [
                        'count' => 1,
                    ];
                }
            }
            $this->setData($data);
        }
        
        private 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) {
                if($this->findModel($product_variant_id)) {
                    $data[ $product_variant_id ] = [
                        '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 getCount(): int
        {
            $data = $this->getData();
            return count($data);
        }
        
        public function findModel(int $product_variant_id): ProductVariant
        {
            /**
             * @var ProductVariant $model
             */
            $model = ProductVariant::find()
                                   ->where([ 'product_variant.product_variant_id' => $product_variant_id ])
                                   ->joinWith('lang', true, 'INNER JOIN')
                                   ->one();
            if(empty( $model )) {
                throw new NotFoundHttpException(\Yii::t('app', 'Product not found'));
            } else {
                return $model;
            }
        }
        
        public function findModels(array $product_variant_ids)
        {
            return ProductVariant::find()
                                 ->where([ 'product_variant.product_variant_id' => $product_variant_ids ])
                                 ->joinWith('lang', true, 'INNER JOIN')
                                 ->with([
                                     'product',
                                     'image',
                                 ])
                                 ->all();
        }
        
        public function clear()
        {
            $this->setData([]);
        }
        
    }