<?php

/* * ********************************************************************************** */
/*      This file is part of the Thelia package.                                     */
/*                                                                                   */
/*      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\Service;

use ArtabanMarketplace\Event\AmpSellerEvent;
use ArtabanMarketplace\Event\AmpSellerEvents;
use ArtabanMarketplace\Model\AmpSeller;
use ArtabanMarketplace\Model\AmpSellerQuery;
use ArtabanMarketplace\Service\Base\AbstractBaseService;
use ArtabanMarketplace\Service\Base\BaseServiceInterface;
use Symfony\Component\EventDispatcher\Event;

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

    const EVENT_CREATE = AmpSellerEvents::SELLER_CREATE;
    const EVENT_CREATE_BEFORE = AmpSellerEvents::SELLER_CREATE_BEFORE;
    const EVENT_CREATE_AFTER = AmpSellerEvents::SELLER_CREATE_AFTER;
    const EVENT_DELETE = AmpSellerEvents::SELLER_DELETE;
    const EVENT_DELETE_BEFORE = AmpSellerEvents::SELLER_DELETE_BEFORE;
    const EVENT_DELETE_AFTER = AmpSellerEvents::SELLER_DELETE_AFTER;
    const EVENT_UPDATE = AmpSellerEvents::SELLER_UPDATE;
    const EVENT_UPDATE_BEFORE = AmpSellerEvents::SELLER_UPDATE_BEFORE;
    const EVENT_UPDATE_AFTER = AmpSellerEvents::SELLER_UPDATE_AFTER;

    protected function createProcess(Event $event) {
        $event->getAmpSeller()->save();
    }

    protected function updateProcess(Event $event) {
        $event->getAmpSeller()->save();
    }

    protected function deleteProcess(Event $event) {
        $event->getAmpSeller()->delete();
    }

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

        $event = new AmpSellerEvent();
        $event->setAmpSeller($seller);

        $this->create($event);

        return $event->getAmpSeller();
    }

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

        $event = new AmpSellerEvent();
        $event->setAmpSeller($seller);

        $this->update($event);

        return $event->getAmpSeller();
    }

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

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

        if ($locale) {
            $model->setLocale($locale);
        }

        // Require Field
        if (isset($data['title'])) {
            $model->setTitle($data['title']);
        }
        if (isset($data['ref'])) {
            $model->setRef($data['ref']);
        }
        if (isset($data['address1'])) {
            $model->setAddress1($data['address1']);
        }
        if (isset($data['zipcode'])) {
            $model->setZipcode($data['zipcode']);
        }
        if (isset($data['city'])) {
            $model->setCity($data['city']);
        }
        if (isset($data['country_id'])) {
            $model->setCountryId($data['country_id']);
        }
        if (isset($data['visible'])) {
            $model->setVisible($data['visible']);
        }

//          Optionnal Field
        if (isset($data['admin_id'])) {
            $model->setAdminId($data['admin_id']);
        }
        if (isset($data['description'])) {
            $model->setDescription($data['description']);
        } else {
            $model->setDescription(null);
        }

        if (isset($data['access'])) {
            $model->setAccess($data['access']);
        } else {
            $model->setAccess(null);
        }

        if (isset($data['address2'])) {
            $model->setAddress2($data['address2']);
        } else {
            $model->setAddress2(null);
        }

        if (isset($data['address3'])) {
            $model->setAddress3($data['address3']);
        } else {
            $model->setAddress3(null);
        }

        if (isset($data['email'])) {
            $model->setEmail($data['email']);
        } else {
            $model->setEmail(null);
        }

        if (isset($data['phone'])) {
            $model->setPhone($data['phone']);
        } else {
            $model->setPhone(null);
        }

        if (isset($data['fax'])) {
            $model->setFax($data['fax']);
        } else {
            $model->setFax(null);
        }

        if (isset($data['url'])) {
            $model->setUrl($data['url']);
        } else {
            $model->setUrl(null);
        }

        if (isset($data['facebook'])) {
            $model->setFacebook($data['facebook']);
        } else {
            $model->setFacebook(null);
        }

        if (isset($data['twitter'])) {
            $model->setTwitter($data['twitter']);
        } else {
            $model->setTwitter(null);
        }

        if (isset($data['pinterest'])) {
            $model->setPinterest($data['pinterest']);
        } else {
            $model->setPinterest(null);
        }

        if (isset($data['google'])) {
            $model->setGoogle($data['google']);
        } else {
            $model->setGoogle(null);
        }

        if (isset($data['youtube'])) {
            $model->setYoutube($data['youtube']);
        } else {
            $model->setYoutube(null);
        }

        if (isset($data['instagram'])) {
            $model->setInstagram($data['instagram']);
        } else {
            $model->setInstagram(null);
        }


        return $model;
    }

    public function deleteFromId($id) {
        $seller = AmpSellerQuery::create()->findOneById($id);
        if ($seller) {
            $event = new AmpSellerEvent();
            $event->setAmpSeller($seller);

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

    public function toggleVisibilityFromId($id) {
        $seller = AmpSellerQuery::create()->findOneById($id);

        $seller->setVisible($seller->getVisible() ? 0 : 1);

        $event = new AmpSellerEvent();
        $event->setAmpSeller($seller);

        $this->update($event);

        return $event->getAmpSeller();
    }

}
