<?php
/*************************************************************************************/
/*      This file is part of the "Diaporamas" Thelia 2 module.                       */
/*                                                                                   */
/*      Copyright (c) OpenStudio                                                     */
/*      email : dev@thelia.net                                                       */
/*      web : http://www.thelia.net                                                  */
/*                                                                                   */
/*      For the full copyright and license information, please view the LICENSE.txt  */
/*      file that was distributed with this source code.                             */
/*************************************************************************************/

namespace ArtabanMarketplace\Actions\Base;

use ArtabanMarketplace\Model\Map\AmpSellerImageTableMap;
use ArtabanMarketplace\Event\AmpSellerImageEvent;
use ArtabanMarketplace\Event\AmpSellerImageEvents;
use ArtabanMarketplace\Model\AmpSellerImageQuery;
use ArtabanMarketplace\Model\AmpSellerImage;
use Thelia\Action\BaseAction;
use Thelia\Core\Event\ToggleVisibilityEvent;
use Thelia\Core\Event\UpdatePositionEvent;
use Propel\Runtime\Propel;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\TheliaEvents;
use \Thelia\Core\Event\TheliaFormEvent;

/**
 * Class SellerImageAction
 * @package ArtabanMarketplace\Action
 * @author Artaban
 */
class SellerImageAction extends BaseAction implements EventSubscriberInterface
{
    public function create(AmpSellerImageEvent $event)
    {
        $this->createOrUpdate($event, new AmpSellerImage());
    }

    public function update(AmpSellerImageEvent $event)
    {
        $model = $this->getAmpSellerImage($event);

        $this->createOrUpdate($event, $model);
    }

    public function delete(AmpSellerImageEvent $event)
    {
        $this->getAmpSellerImage($event)->delete();
    }

    protected function createOrUpdate(AmpSellerImageEvent $event, AmpSellerImage $model)
    {
        $con = Propel::getConnection(AmpSellerImageTableMap::DATABASE_NAME);
        $con->beginTransaction();

        try {
            $model->setLocale($event->getLocale());

            if (null !== $id = $event->getId()) {
                $model->setId($id);
            }

            if (null !== $ampSellerId = $event->getAmpSellerId()) {
                $model->setAmpSellerId($ampSellerId);
            }

            if (null !== $file = $event->getFile()) {
                $model->setFile($file);
            }

            if (null !== $visible = $event->getVisible()) {
                $model->setVisible($visible);
            }

            if (null !== $position = $event->getPosition()) {
                $model->setPosition($position);
            }

            if (null !== $title = $event->getTitle()) {
                $model->setTitle($title);
            }

            if (null !== $description = $event->getDescription()) {
                $model->setDescription($description);
            }

            if (null !== $chapo = $event->getChapo()) {
                $model->setChapo($chapo);
            }

            if (null !== $postscriptum = $event->getPostscriptum()) {
                $model->setPostscriptum($postscriptum);
            }

            $model->save($con);

            $con->commit();
        } catch (\Exception $e) {
            $con->rollback();

            throw $e;
        }

        $event->setAmpSellerImage($model);
    }

    protected function getAmpSellerImage(AmpSellerImageEvent $event)
    {
        $model = AmpSellerImageQuery::create()->findPk($event->getId());

        if (null === $model) {
            throw new \RuntimeException(sprintf(
                "The 'amp_seller_image' id '%d' doesn't exist",
                $event->getId()
            ));
        }

        return $model;
    }

    public function updatePosition(UpdatePositionEvent $event)
    {
        $this->genericUpdatePosition(new AmpSellerImageQuery(), $event);
    }

    public function toggleVisibility(ToggleVisibilityEvent $event)
    {
        $this->genericToggleVisibility(new AmpSellerImageQuery(), $event);
    }

    public function beforeCreateFormBuild(TheliaFormEvent $event)
    {
    }

    public function beforeUpdateFormBuild(TheliaFormEvent $event)
    {
    }

    public function afterCreateFormBuild(TheliaFormEvent $event)
    {
    }

    public function afterUpdateFormBuild(TheliaFormEvent $event)
    {
    }

    /**
     * Returns an array of event names this subscriber wants to listen to.
     *
     * The array keys are event names and the value can be:
     *
     *  * The method name to call (priority defaults to 0)
     *  * An array composed of the method name to call and the priority
     *  * An array of arrays composed of the method names to call and respective
     *    priorities, or 0 if unset
     *
     * For instance:
     *
     *  * array('eventName' => 'methodName')
     *  * array('eventName' => array('methodName', $priority))
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
     *
     * @return array The event names to listen to
     *
     * @api
     */
    public static function getSubscribedEvents()
    {
        return array(
            AmpSellerImageEvents::CREATE => array("create", 128),
            AmpSellerImageEvents::UPDATE => array("update", 128),
            AmpSellerImageEvents::DELETE => array("delete", 128),
            AmpSellerImageEvents::UPDATE_POSITION => array("updatePosition", 128),
            AmpSellerImageEvents::TOGGLE_VISIBILITY => array("toggleVisibility", 128),
            TheliaEvents::FORM_BEFORE_BUILD . ".seller_image_create" => array("beforeCreateFormBuild", 128),
            TheliaEvents::FORM_BEFORE_BUILD . ".seller_image_update" => array("beforeUpdateFormBuild", 128),
            TheliaEvents::FORM_AFTER_BUILD . ".seller_image_create" => array("afterCreateFormBuild", 128),
            TheliaEvents::FORM_AFTER_BUILD . ".seller_image_update" => array("afterUpdateFormBuild", 128),
        );
    }
}
