<?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\Controller;

use ArtabanMarketplace\Controller\Base\BaseController;
use ArtabanMarketplace\ArtabanMarketplace as ArtabanMarketplaceModule;
use ArtabanMarketplace\Model\AmpSeller;
use ArtabanMarketplace\Model\AmpSellerQuery;
use Propel\Runtime\Propel;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Tools\URL;

/**
 * Class SellerController
 * @package ArtabanMarketplace\Controller
 */
class SellerController extends BaseController {

    /**
     * Load an existing object from the database
     */
    protected function getExistingObject() {
        return AmpSellerQuery::create()->findPk($this->getRequest()->query->get("ampseller_id"));
    }

    /**
     * Hydrate the update form for this object, before passing it to the update template
     *
     * @param mixed $object
     */
    protected function hydrateObjectForm($object) {
        $data = array(
            "id" => $object->getId(),
            "title" => $object->getTitle(),
            "ref" => $object->getRef(),
            "address1" => $object->getAddress1(),
            "address2" => $object->getAddress2(),
            "address3" => $object->getAddress3(),
            "zipcode" => $object->getZipcode(),
            "city" => $object->getCity(),
            "country_id" => $object->getCountryId(),
            "admin_id" => $object->getAdminId(),
            "description" => $object->getDescription(),
            "latitude" => $object->getLatitude(),
            "longitude" => $object->getLongitude(),
            "email" => $object->getEmail(),
            "phone" => $object->getPhone(),
            "fax" => $object->getFax(),
            "url" => $object->getUrl(),
            "facebook" => $object->getFacebook(),
            "twitter" => $object->getTwitter(),
            "pinterest" => $object->getPinterest(),
            "google" => $object->getGoogle(),
            "youtube" => $object->getYoutube(),
            "instagram" => $object->getInstagram(),
        );

        return $this->getUpdateForm($data);
    }

    const CONTROLLER_ENTITY_NAME = "seller";

    /**
     * Use to get render of list
     * @return mixed
     */
    protected function getListRenderTemplate() {
        return $this->render("sellers");
    }

    /**
     * Use to get Edit render
     * @return mixed
     */
    protected function getEditRenderTemplate() {
        return $this->render("seller-edit");
    }

    /**
     * Use to get Create render
     * @return mixed
     */
    protected function getCreateRenderTemplate() {
        // TODO: Implement getCreateRenderTemplate() method.
    }

    /**
     * @return mixed
     */
    protected function getObjectId($object) {
        /** @var Seller $object */
        return $object->getId();
    }

    protected function redirectToListTemplate() {
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/artabanmarketplace/sellers"));
    }

    protected function redirectToEditionTemplate() {
        $id = $this->getRequest()->query->get("seller_id");

        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/artabanmarketplace/seller/edit", ["seller_id" => $id,]));
    }

    public function toggleVisibleAction($id) {
        // Check current user authorization
//        if (null !== $response = $this->checkAuth(AdminResources::MODULE, ArtabanMarketplaceModule::getModuleCode(),
//                AccessManager::UPDATE)
//        ) {
//        return $response;
//        }
        // Error (Default: false)
        $error_msg = false;

        $retour = [];
        $code = 200;


        $con = Propel::getConnection();
        $con->beginTransaction();

        try {
            $updatedObject = $this->getService()->toggleVisibilityFromId($id);

            // Check if object exist
            if (!$updatedObject) {
                throw new \LogicException(
                $this->getTranslator()->trans("No %obj was updated.", ['%obj' => static::CONTROLLER_ENTITY_NAME])
                );
            }

            $con->commit();

            $retour["message"] = "Visibility was updated";
        } catch (\Exception $ex) {
            $con->rollBack();
            // Any other error
            $retour["message"] = "Visibility can be updated";
            $code = 500;
            $retour["error"] = $ex->getMessage();
        }

        return JsonResponse::create($retour, $code);
    }

}
