<?php

namespace Lc\ShopBundle\Controller\Frontend ;

use App\Form\Frontend\OrderProductsType;
use App\Services\OrderUtils;
use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\OrderProductInterface;
use Lc\ShopBundle\Context\OrderUtilsInterface;
use Lc\ShopBundle\Context\ProductFamilyInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class CartController extends BaseController
{
        protected $orderUtils ;
        protected $productFamilyRepository ;

        protected $productFamily ;
        protected $orderProducts = [] ;


        public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils, OrderUtilsInterface $orderUtils)
        {
                parent::__construct($em, $merchantUtils);
                $this->orderUtils = $orderUtils ;
                $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
        }

        public function addProductFamily(Request $request)
        {
                $return = [] ;
                $data = $request->request->all() ;
                if(isset($data['order_products']['id_product_family'])) {
                        $idProductFamily = $data['order_products']['id_product_family'] ;
                        $this->productFamily = $this->productFamilyRepository->find($idProductFamily) ;
                        if($this->productFamily) {
                                $form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]);
                                $form->handleRequest($request);

                                if ($form->isSubmitted() && $form->isValid()) {
                                        $orderShop = $this->orderUtils->getOrderShopCurrent() ;
                                        $data = $form->getData() ;
                                        foreach($data as $orderProduct) {
                                                if($orderProduct instanceof OrderProductInterface) {
                                                        $this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
                                                        if($orderProduct->getQuantity() > 0) {
                                                                $this->orderProducts[] = $orderProduct ;
                                                        }
                                                }
                                        }
                                }
                        }
                }

                return new JsonResponse($return) ;
        }

        public function addProduct()
        {

        }

        public function editProduct()
        {

        }

        public function deleteProduct()
        {

        }

        public function summary()
        {

        }
}