<?php

namespace Lc\ShopBundle\Controller\Backend;

use App\Entity\OrderShop;
use App\Form\Backend\Common\AddressType;
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use FOS\UserBundle\Doctrine\UserManager;
use FOS\UserBundle\Model\UserManagerInterface ;
use Lc\ShopBundle\Context\AddressInterface;
use Lc\ShopBundle\Model\Address;

class UserController extends AdminController
{

        public function showAction()
        {

                $id = $this->request->query->get('id');
                $easyadmin = $this->request->attributes->get('easyadmin');
                $entity = $easyadmin['item'];
                $this->orderShoprepo = $this->em->getRepository(OrderShop::class);

             /*   dump($this->orderUtils->getRankTotalOrder($entity));
                die();
                dump($this->orderUtils->getRankSumOrderByUser($entity));

                dump($this->orderUtils->getRankTotalOrderByUser($entity));

                dump($this->orderUtils->getTotalSpentByUser($entity));
                dump($this->orderUtils->getAverageOrderByUser($entity));


                die();*/
                $this->dispatch(EasyAdminEvents::PRE_SHOW);



                $fields = $this->entity['show']['fields'];
                $deleteForm = $this->createDeleteForm($this->entity['name'], $id);

                $this->dispatch(EasyAdminEvents::POST_SHOW, [
                        'deleteForm' => $deleteForm,
                        'fields' => $fields,
                        'entity' => $entity,
                ]);

                $parameters = [
                        'entity' => $entity,
                        'fields' => $fields,
                        'delete_form' => $deleteForm->createView(),
                ];

                return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
        }

        public function createNewEntity()
        {
                return $this->userManager->createUser();
        }

        public function persistEntity($user)
        {
                $this->userManager->updateUser($user, false);
                parent::persistEntity($user);
        }

        public function updateEntity($user)
        {
                $this->userManager->updateUser($user, false);
                parent::updateEntity($user);
        }

        public function removeEntity($user)
        {
                // désactivation UserMerchant
                if($user->getUserMerchants()) {
                        $merchant = $this->merchantUtils->getMerchantCurrent() ;
                        foreach($user->getUserMerchants() as $userMerchant) {
                                if($userMerchant->getMerchant() == $merchant) {
                                        $userMerchant->setActive(false) ;
                                        $this->em->persist($userMerchant);
                                        $this->em->flush() ;
                                        $this->addFlash('success', 'Utilisateur supprimé du hub');
                                }
                        }
                }
        }

        protected function createUserListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
        {
                $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter) ;

                $queryBuilder->innerJoin('entity.userMerchants', 'um') ;
                $queryBuilder->andWhere('um.merchant = :merchant AND um.active = 1') ;
                $queryBuilder->setParameter('merchant', $this->merchantUtils->getMerchantCurrent()) ;

                return $queryBuilder ;
        }
}
