<?php

namespace Lc\ShopBundle\EventSubscriber;

use Lc\ShopBundle\Context\FilterMerchantInterface;
use Lc\ShopBundle\Context\FilterMultipleMerchantsInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\SortableInterface;
use Lc\ShopBundle\Context\StatusInterface;
use Lc\ShopBundle\Context\TreeInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;


class EditEventSubscriber implements EventSubscriberInterface
{
        public $merchantUtils;

        public function __construct(MerchantUtilsInterface $merchantUtils)
        {
                $this->merchantUtils = $merchantUtils;
        }

        public static function getSubscribedEvents()
        {
                return array(
                        'easy_admin.pre_persist' => array('persistCommonProperty'),
                        'easy_admin.post_update' => array('updateCommonProperty'),
                );
        }


        public function persistCommonProperty(GenericEvent $event)
        {
                $entity = $event->getSubject();
                $em = $event->getArgument('em');

                $entityRepo = $em->getRepository(get_class($entity));

                if ($entity instanceof StatusInterface) {
                        $this->setStatusProperty($entity);
                }

                if ($entity instanceof SortableInterface) {
                        $this->setSortableProperty($entity, $entityRepo);
                }

                if ($entity instanceof FilterMerchantInterface) {
                        $this->setMerchantProperty($entity);
                }

                if ($entity instanceof FilterMultipleMerchantsInterface) {
                        $this->setMultipleMerchantProperty($entity);
                }
        }


        public function updateCommonProperty(GenericEvent $event)
        {
               /* $this->setUpdated($entity);
                $this->setAddressCreatedBy($entity) ;*/
        }

        private function setStatusProperty($entity){
                $entity->setStatus(1);

                return $entity;
        }

        private function setSortableProperty($entity, $entityRepo)
        {
                if ($entity instanceof TreeInterface) {
                        if ($entity->getParent()) {
                                $total = $entityRepo->count(array('status' => 1, 'parent' => $entity->getParent()->getId()));
                                $entity->setPosition((float) $entity->getParent()->getId() . '.' . str_pad($total, 2, '0', STR_PAD_LEFT));
                        }
                        else {
                                if ($entity instanceof FilterMerchantInterface) {
                                        $total = $entityRepo->count(array('status' => 1, 'parent' => null, 'merchant' => $this->merchantUtils->getMerchantCurrent()));
                                }else{
                                        $total = $entityRepo->count(array('status' => 1, 'parent' => null));
                                }
                                $entity->setPosition($total);
                        }
                }
                else {
                        if ($entity instanceof FilterMerchantInterface) {
                                $total = $entityRepo->count(array('status' => 1, 'merchant' => $this->merchantUtils->getMerchantCurrent()));
                        }
                        else {
                                $total = $entityRepo->count(array('status' => 1));
                        }
                        $entity->setPosition($total);
                }
                return $entity;
        }

        private function setMerchantProperty($entity)
        {
                $entity->setMerchant($this->merchantUtils->getMerchantCurrent());
        }

        private function setMultipleMerchantProperty($entity)
        {
                if ($entity->getMerchants()->isEmpty()) {
                        $entity->addMerchant($this->merchantUtils->getMerchantCurrent());
                }
        }

}
