<?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\Controller\Admin\BaseAdminController;
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;
use ArtabanMarketplace\ArtabanMarketplace;
use ArtabanMarketplace\Model\AmpSellerOrderQuery;
use ArtabanMarketplace\Model\AmpSellerOrderDeliveryReference;
use ArtabanMarketplace\Model\AmpSellerOrderDeliveryReferenceQuery;

/**
 * Class OrderController
 * @package Thelia\Controller\Admin
 * @author Manuel Raynaud <manu@raynaud.io>
 */
class OrderSellerController extends BaseOrderController {

    const CONTROLLER_INDEX_TEMPLATE = 'seller-orders';
    const CONTROLLER_EDIT_TEMPLATE = "seller-order-edit";

    protected $currentRouter = "router.ArtabanMarketplace";

    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.seller.admin.orders", $params);
        } else {
            $params["tab"] = $this->getRequest()->get("tab", 'cart');

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

        return $response;
    }

    public function updateDeliveryRefAction($order_id) {

// Artaban cf core\lib\Thelia\Core\Security\Resource\AdminResources.php et core\lib\Thelia\Controller\Api\ImageController.php
//        if (null !== $response = $this->checkAuth($this->getAdminResources()->getResource('RESOURCES_OWNER','ArtabanMarketplace'), array(), AccessManager::UPDATE)) {
//            return $response;
//        }

        $message = null;

        try {
            $order = AmpSellerOrderQuery::create()->findPk($order_id);

            $deliveryRef = $this->getRequest()->get("delivery_ref");

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

            $deliveryRefObject = AmpSellerOrderDeliveryReferenceQuery::create()->findOneByOrderSellerId($order_id);
            if (null === $deliveryRefObject) {
            $deliveryRefObject = new AmpSellerOrderDeliveryReference();
            }
            $deliveryRefObject->setDeliveryReference($deliveryRef);
            $deliveryRefObject->setOrderSellerId($order_id);
            $deliveryRefObject->save();


        } catch (\Exception $e) {
            $message = $e->getMessage();
        }

        $params = array();

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

        $params["tab"] = $this->getRequest()->get("tab", 'bill');

        return $this->generateRedirectFromRoute(
                        "ArtabanMarketplace.seller.admin.order", $params, ['order_id' => $order_id]
        );
    }

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

        $message = null;

        $orderUpdateAddress = new OrderUpdateAddress($this->getRequest());

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

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

            $form = $this->validateForm($orderUpdateAddress, "post");

            $orderAddress = OrderAddressQuery::create()->findPk($form->get("id")->getData());

            if ($orderAddress->getId() !== $order->getInvoiceOrderAddressId() && $orderAddress->getId() !== $order->getDeliveryOrderAddressId()) {
                throw new \InvalidArgumentException("The order address you want to update does not belong to the current order not exist");
            }

            $event = new OrderAddressEvent(
                    $form->get("title")->getData(), $form->get("firstname")->getData(), $form->get("lastname")->getData(), $form->get("address1")->getData(), $form->get("address2")->getData(), $form->get("address3")->getData(), $form->get("zipcode")->getData(), $form->get("city")->getData(), $form->get("country")->getData(), $form->get("phone")->getData(), $form->get("company")->getData(), $form->get("cellphone")->getData(), $form->get("state")->getData()
            );
            $event->setOrderAddress($orderAddress);
            $event->setOrder($order);

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

        $params = array();

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

        $params["tab"] = $this->getRequest()->get("tab", 'bill');

        return $this->generateRedirectFromRoute(
                        "ArtabanMarketplace.seller.admin.order", $params, ['order_id' => $order_id]
        );
    }

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

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

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

        return $response;
    }

}
