<?php
/*************************************************************************************/
/*      For the full copyright and license information, please view the LICENSE.txt  */
/*      file that was distributed with this source code.                             */
/*************************************************************************************/
/*************************************************************************************/

namespace ArtabanMarketplace\Service;

use ArtabanMarketplace\Event\AmpSellerAdminLinkEvent;
use ArtabanMarketplace\Event\AmpSellerEvents;
use ArtabanMarketplace\Model\AmpSellerAdmin;
use ArtabanMarketplace\Model\AmpSellerAdminQuery;
use ArtabanMarketplace\Service\Base\AbstractBaseService;
use ArtabanMarketplace\Service\Base\BaseServiceInterface;
use Symfony\Component\EventDispatcher\Event;

/**
 * Description of SellerAdminLinkService
 *
 * @author Lengy Gilles
 */
class SellerAdminLinkService extends AbstractBaseService implements BaseServiceInterface
{

    const EVENT_CREATE = AmpSellerEvents::AMP_SELLER_ADMIN_LINK_CREATE;
    const EVENT_CREATE_BEFORE = AmpSellerEvents::AMP_SELLER_ADMIN_LINK_CREATE_BEFORE;
    const EVENT_CREATE_AFTER = AmpSellerEvents::AMP_SELLER_ADMIN_LINK_CREATE_AFTER;
    const EVENT_DELETE = AmpSellerEvents::AMP_SELLER_ADMIN_LINK_DELETE;
    const EVENT_DELETE_BEFORE = AmpSellerEvents::AMP_SELLER_ADMIN_LINK_DELETE_BEFORE;
    const EVENT_DELETE_AFTER = AmpSellerEvents::AMP_SELLER_ADMIN_LINK_DELETE_AFTER;
    const EVENT_UPDATE = AmpSellerEvents::AMP_SELLER_ADMIN_LINK_UPDATE;
    const EVENT_UPDATE_BEFORE = AmpSellerEvents::AMP_SELLER_ADMIN_LINK_UPDATE_BEFORE;
    const EVENT_UPDATE_AFTER = AmpSellerEvents::AMP_SELLER_ADMIN_LINK_UPDATE_AFTER;

    /**
     * @inheritDoc
     */
    protected function createProcess(Event $event)
    {
        /** @var AmpSellerAdminLinkEvent $event */
        $event->getAmpSellerAdmin()->save();
    }

    /**
     * @inheritDoc
     */
    protected function updateProcess(Event $event)
    {
        /** @var AmpProductLinkEvent $event */
        $event->getAmpSellerAdmin()->save();
    }

    protected function deleteProcess(Event $event)
    {
        /** @var AmpProductLinkEvent $event */
        $event->getAmpSellerAdmin()->delete();
    }

    public function createFromArray($data, $locale = null)
    {
        $link = $this->hydrateObjectArray($data, $locale);

        $event = new AmpSellerAdminLinkEvent();
        $event->setAmpSellerAdmin($link);

        $this->create($event);

        return $event->getAmpSellerAdmin();
    }

    public function updateFromArray($data, $locale = null)
    {
        $link = $this->hydrateObjectArray($data, $locale);

        $event = new AmpSellerAdminLinkEvent();
        $event->setAmpSellerAdmin($link);

        $this->update($event);

        return $event->getAmpSellerAdmin();
    }

    public function deleteFromId($id)
    {
        $link = AmpSellerAdminQuery::create()->findOneById($id);
        if ($link) {
            $event = new AmpSellerAdminLinkEvent();
            $event->setAmpSellerAdmin($link);

            $this->delete($event);
        }
    }

    public function deleteFromArray($data)
    {
        $link = null;

        if (isset($data["admin_id"]) && isset($data["seller_id"])) {
            $link = AmpSellerAdminQuery::create()->filterByAdminId($data["admin_id"])->filterBySellerId($data["seller_id"])->findOne();
        }

        if ($link) {
            $event = new AmpSellerAdminLinkEvent();
            $event->setAmpSellerAdmin($link);

            $this->delete($event);
        }
    }

    protected function hydrateObjectArray($data, $locale = null)
    {
        $model = new AmpSellerAdmin();

        if (isset($data['id'])) {
            $link = AmpSellerAdminQuery::create()->findOneById($data['id']);
            if ($link) {
                $model = $link;
            }
        }

        if (isset($data["admin_id"]) && isset($data["seller_id"])) {
            $link = AmpSellerAdminQuery::create()->filterByAmpSellerId($data["seller_id"])->filterByAdminId($data["admin_id"])->findOne();
            if ($link) {
                throw new \Exception("A link already exist", 403);
            }

            $model->setAdminId($data["admin_id"]);
            $model->setAmpSellerId($data["seller_id"]);
        }

        return $model;
    }
}
