<?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 Thelia\Core\Event\Order\OrderAddressEvent;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Form\OrderUpdateAddress;
use Thelia\Model\ConfigQuery;
use Thelia\Model\OrderAddressQuery;
use Thelia\Model\OrderQuery;
use Thelia\Model\OrderStatusQuery;
use ArtabanMarketplace\Controller\Base\BaseOrderController;

/**
 * Class OrderGeneralController
 * @package ArtabanMarketplace\Controller
 * @author Gilles Lengy <gilles.lengy@artaban.fr>
 */
class OrderGeneralController extends BaseOrderController {

    protected $currentRouter = "router.ArtabanMarketplace";

    const CONTROLLER_INDEX_TEMPLATE = 'orders-general';

    public function updateStatusAction($order_id = null) {
        if (null !== $response = $this->checkAuth(AdminResources::ORDER, array(), AccessManager::UPDATE)) {
            return $response;
        }

        $message = null;

        try {
            if ($order_id === null) {
                $order_id = $this->getRequest()->get("order_id");
            }

            $order = OrderQuery::create()->findPk($order_id);

            $statusId = $this->getRequest()->get("status_id");
            $status = OrderStatusQuery::create()->findPk($statusId);

            if (null === $order) {
                throw new \InvalidArgumentException("The order you want to update status does not exist");
            }
            if (null === $status) {
                throw new \InvalidArgumentException("The status you want to set to the order does not exist");
            }

            $event = new OrderEvent($order);
            $event->setStatus($statusId);

            $this->dispatch(TheliaEvents::ORDER_UPDATE_STATUS, $event);
        } catch (\Exception $e) {
            $message = $e->getMessage();
        }

        $params = array();

        if ($message) {
            $params["update_status_error_message"] = $message;
        }

        $browsedPage = $this->getRequest()->get("order_page");
        $currentStatus = $this->getRequest()->get("status");

        if ($browsedPage) {
            $params["order_page"] = $browsedPage;

            if (null !== $currentStatus) {
                $params["status"] = $currentStatus;
            }

            $response = $this->generateRedirectFromRoute("ArtabanMarketplace.admin.general.orders", $params);
        } else {
            $params["tab"] = $this->getRequest()->get("tab", 'cart');

            $response = $this->generateRedirectFromRoute("ArtabanMarketplace.admin.general.order.update.view", $params, ['order_id' => $order_id]);
        }

        return $response;
    }

    public function generateOrderPdfAction($order_id, $browser) {
        if (null !== $response = $this->checkAuth(AdminResources::ORDER, array(), AccessManager::UPDATE)) {
            return $response;
        }
        return $this->generateBackOfficeOrderPdf($order_id, ConfigQuery::read('pdf_order_general_file', 'order_general'), $browser);
    }

    public function generateDeliveryPdfAction($order_id, $browser) {
        if (null !== $response = $this->checkAuth(AdminResources::ORDER, array(), AccessManager::UPDATE)) {
            return $response;
        }
        return $this->generateBackOfficeOrderPdf($order_id, ConfigQuery::read('pdf_delivery_general_file', 'delivery_general'), $browser);
    }

    private function generateBackOfficeOrderPdf($order_id, $fileName, $browser) {
        if (null === $response = $this->generateOrderPdf($order_id, $fileName, true, true, $browser)) {
            return $this->generateRedirectFromRoute(
                            "ArtabanMarketplace.admin.general.order.update.view", [], ['order_id' => $order_id]
            );
        }

        return $response;
    }

}
