<?php
# src/EventSubscriber/EasyAdminSubscriber.php
namespace Lc\ShopBundle\EventSubscriber;

use Lc\ShopBundle\Context\FilterMerchantInterface;
use Lc\ShopBundle\Context\FilterMultipleMerchantsInterface;
use Lc\ShopBundle\Context\StatusInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Security\Core\Security;


class ListEventSubscriber implements EventSubscriberInterface
{
        public $security;

        public function __construct(Security $security)
        {
                $this->security = $security;
        }

        public static function getSubscribedEvents()
        {
                return array(
                        'easy_admin.post_list' => array('setPaginatorCountStatus'),
                );
        }

        public function setPaginatorCountStatus(GenericEvent $event)
        {
                $paginator = $event->getSubject();
                $em = $event->getArgument('em');
                $entity = $event->getArgument('entity');
                $entityRepo = $em->getRepository($entity['class']);

                $entityObject = new $entity['class'];
                $criteria = array();
                if ($entityObject instanceof FilterMerchantInterface) {
                        $currentMerchant = $this->security->getUser()->getMerchant();
                        $criteria['merchant'] = $currentMerchant;
                }


              /*  if ($entityObject instanceof FilterMultipleMerchantsInterface) {
                        $paginator->nbResultsTotal = $entityRepo->count(array());

                // TODO : Filter sur le merchant courant; Pour le moment dans ce contexte on ne charge rien de plus, par de nbResultsOnline en fonction du merchant

                }else {*/

                        if ($entityObject instanceof StatusInterface) {

                                for ($status = -1; $status <= 1; $status++) {
                                        $criteria['status'] = $status;
                                        if ($status == -1) {
                                                $paginator->nbResultsDeleted = $entityRepo->count($criteria);
                                        } elseif ($status == 0) {
                                                $paginator->nbResultsOffline = $entityRepo->count($criteria);
                                        } else if ($status == 1) {
                                                $paginator->nbResultsOnline = $entityRepo->count($criteria);
                                        }
                                }

                                $paginator->nbResultsTotal = $paginator->nbResultsOnline + $paginator->nbResultsOffline;

                        }else{
                                $paginator->nbResultsTotal = $entityRepo->count($criteria);
                        }
                //}


        }
}
