<?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\AmpProductLinkEvent;
use ArtabanMarketplace\Event\AmpSellerEvents;
use ArtabanMarketplace\Model\AmpSellerProduct;
use ArtabanMarketplace\Model\AmpSellerProductQuery;
use ArtabanMarketplace\Service\Base\AbstractBaseService;
use ArtabanMarketplace\Service\Base\BaseServiceInterface;
use Symfony\Component\EventDispatcher\Event;

/**
 * Class ProductLinkService
 * @package ArtabanMarketplace\Service
 */
class ProductLinkService extends AbstractBaseService implements BaseServiceInterface
{

    const EVENT_CREATE = AmpSellerEvents::AMP_PRODUCT_LINK_CREATE;
    const EVENT_CREATE_BEFORE = AmpSellerEvents::AMP_PRODUCT_LINK_CREATE_BEFORE;
    const EVENT_CREATE_AFTER = AmpSellerEvents::AMP_PRODUCT_LINK_CREATE_AFTER;
    const EVENT_DELETE = AmpSellerEvents::AMP_PRODUCT_LINK_DELETE;
    const EVENT_DELETE_BEFORE = AmpSellerEvents::AMP_PRODUCT_LINK_DELETE_BEFORE;
    const EVENT_DELETE_AFTER = AmpSellerEvents::AMP_PRODUCT_LINK_DELETE_AFTER;
    const EVENT_UPDATE = AmpSellerEvents::AMP_PRODUCT_LINK_UPDATE;
    const EVENT_UPDATE_BEFORE = AmpSellerEvents::AMP_PRODUCT_LINK_UPDATE_BEFORE;
    const EVENT_UPDATE_AFTER = AmpSellerEvents::AMP_PRODUCT_LINK_UPDATE_AFTER;

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

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

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

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

        $event = new AmpProductLinkEvent();
        $event->setAmpProductLink($link);

        $this->create($event);

        return $event->getAmpProductLink();
    }

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

        $event = new AmpProductLinkEvent();
        $event->setAmpProductLink($link);

        $this->update($event);

        return $event->getAmpProductLink();
    }

    public function deleteFromId($id)
    {
        $link = AmpSellerProductQuery::create()->findOneById($id);
        if ($link) {
            $event = new AmpProductLinkEvent();
            $event->setDealerProductLink($link);

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

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

        if (isset($data["product_id"]) && isset($data["dealer_id"])) {
            $link = AmpSellerProductQuery::create()->filterByDealerId($data["dealer_id"])->filterByProductId($data["product_id"])->findOne();
        }

        if ($link) {
            $event = new AmpProductLinkEvent();
            $event->setAmpProductLink($link);

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

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

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

        if (isset($data["product_id"]) && isset($data["amp_seller_id"])) {
            $link = AmpSellerProductQuery::create()->filterByAmpSellerId($data["amp_seller_id"])->filterByProductId($data["product_id"])->findOne();
            if ($link) {
                throw new \Exception("A link already exist", 403);
            }

            $model->setProductId($data["product_id"]);
            $model->setAmpSellerId($data["amp_seller_id"]);
        }

        return $model;
    }
}
