<?php
    
    namespace frontend\controllers;
    
    use common\behaviors\AjaxFilter;
    use common\models\Basket;
    use common\models\Orders;
    use yii\filters\VerbFilter;
    use yii\web\Response;
    
    class BasketController extends \yii\web\Controller
    {
        
        public $enableCsrfValidation = false;
        
        public function behaviors()
        {
            return [
                'verbs' => [
                    'class'   => VerbFilter::className(),
                    'actions' => [
                        'add'    => [ 'post' ],
                        'remove' => [ 'post' ],
                        'index'  => [ 'get' ],
                    ],
                ],
                'ajax' => [
                    'class' => AjaxFilter::className(),
                ],
            ];
        }
    
        public function actionIndex()
        {
            $response = \Yii::$app->response;
            $response->format = Response::FORMAT_JSON;
            /**
             * @var Basket $basket
             */
            $basket = \Yii::$app->basket;
            $result = [
                'basket' => $basket->getData(),
                'modal'  => $this->getModal($basket),
                'cart'   => $this->getCart($basket),
            ];
            return $result;
        }
        
        public function actionAdd(int $product_variant_id)
        {
            $response = \Yii::$app->response;
            $response->format = Response::FORMAT_JSON;
            /**
             * @var Basket $basket
             */
            $basket = \Yii::$app->basket;
            $basket->add($product_variant_id);
            $result = [
                'basket' => $basket->getData(),
                'modal'  => $this->getModal($basket),
                'cart'   => $this->getCart($basket),
            ];
            return $result;
        }
        
        public function actionRemove(int $product_variant_id)
        {
            $response = \Yii::$app->response;
            $response->format = $response::FORMAT_JSON;
            /**
             * @var Basket $basket
             */
            $basket = \Yii::$app->basket;
            $basket->delete($product_variant_id);
            $result = [
                'basket' => $basket->getData(),
                'modal'  => $this->getModal($basket),
                'cart'   => $this->getCart($basket),
            ];
            return $result;
        }
        
        /**
         * @var $basket \common\models\Basket
         * @return string modal_items
         */
        private function getModal($basket): string
        {
            $order = new Orders();
            $data = $basket->getData();
            $models = $basket->findModels(array_keys($data));
            $output = $this->renderAjax('modal_items', [
                'order'  => $order,
                'models' => $models,
                'basket' => $basket,
            ]);
            return $output;
        }
        
        /**
         * @param Basket $basket
         *
         * @return string
         */
        private function getCart($basket): string
        {
            $count = $basket->getCount();
            $output = $this->renderPartial('cart', [
                'count' => $count,
            ]);
            return $output;
        }
    }
