<?php

namespace KupShop\ContentBundle\Controller;

use KupShop\ContentBundle\View\OrderEditView;
use KupShop\ContentBundle\View\OrderStatusView;
use KupShop\ContentBundle\View\OrderSuccessView;
use KupShop\ContentBundle\View\OrdersView;
use KupShop\ContentBundle\View\OrderView;
use KupShop\GraphQLBundle\EventListener\JsShopRefreshListener;
use KupShop\KupShopBundle\Exception\RedirectException;
use KupShop\KupShopBundle\Routing\TranslatedRoute;
use KupShop\OrderingBundle\Cart;
use KupShop\OrderingBundle\Util\Order\OrderUtil;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class OrdersController extends AbstractController
{
    /**
     * @TranslatedRoute("/#orders#/", name="orders")
     */
    public function ordersAction(Request $request, OrdersView $view)
    {
        $view->setRequest($request);

        $filter = $request->get('filter', []);
        $view->setFilter($filter);

        return $view->getResponse();
    }

    /**
     * @TranslatedRoute("/#orderView#/{id}/", requirements={"id":"\d+"})
     */
    public function orderAction(Request $request, OrderView $view, OrderSuccessView $successView, $id)
    {
        if ($request->get('status') == '1') {
            $view = $successView;
        }

        $view->setIDo($id);
        $view->setRequest($request);

        if ($error = $request->get('error')) {
            $view->setError($error);
        }

        return $view;
    }

    /**
     * @TranslatedRoute("/#orderView#/{id}/success/", requirements={"id":"\d+"})
     */
    public function successAction(Request $request, OrderSuccessView $view, $id)
    {
        $view->setIDo($id);
        $view->setRequest($request);

        return $view->getResponse();
    }

    /**
     * @TranslatedRoute("/#orderView#/{id}/storno/", requirements={"id":"\d+"})
     */
    public function orderStornoAction(Request $request, $id)
    {
        OrderView::checkOrderOwnership($id, $request->get('cf'));

        $order = new \Order();
        $order->createFromDB($id);

        if (in_array($order->status, getStatuses('editable'))) {
            $order->setData('stornoByUser', true);
            $order->storno(true);
        }

        $referer = $request->headers->get('referer', '/');

        throw new RedirectException($referer);
    }

    /**
     * @TranslatedRoute("/#orderStatus#/")
     */
    public function orderStatusAction(Request $request, OrderStatusView $view)
    {
        $view->setRequest($request);
        $view->setOrderId($request->get('IDo'));

        return $view->getResponse();
    }

    /**
     * @TranslatedRoute("/#orderView#/{id}/#orderEdit#/", requirements={"id":"\d+"})
     */
    public function orderEditAction(Request $request, OrderEditView $view, $id)
    {
        $view->setRequest($request);
        $view->setOrderId($id);

        return $view->getResponse();
    }

    /**
     * @TranslatedRoute("/#orderView#/{id}/change-payment/{payment}",
     *     requirements={"id":"\d+", "payment":"\d+"})
     */
    public function orderPaymentChangeAction(Request $request, OrderView $view, $id, $payment)
    {
        $view->setIDo($id);
        $view->setRequest($request);

        if ($deliveryType = $view->canChangePayment($payment)) {
            $view->changeDeliveryType($deliveryType);
            addUserMessage(translate('payment_change', 'order'), 'success');
        } else {
            addUserMessage(translate('payment_change_false', 'order'));
        }

        $redirectUri = $request->headers->get('referer');

        if (!$redirectUri) {
            $redirectUri = $this->generateUrl('kupshop_content_orders_order', ['id' => $id]);
        }

        return new RedirectResponse($redirectUri);
    }

    /**
     * @TranslatedRoute("/#orderView#/{id}/reorder/", requirements={"id":"\d+"})
     */
    public function orderReorderAction(Request $request, OrderUtil $orderUtil, Cart $cart, $id)
    {
        OrderView::checkOrderOwnership($id, $request->get('cf'));

        $order = new \Order();
        $order->createFromDB($id);
        $order->fetchItems();

        $orderUtil->orderReorder($order, $cart);

        if (findModule(\Modules::JS_SHOP)) {
            $request->getSession()->set(JsShopRefreshListener::SESSION_NAME, true);
        }

        return new RedirectResponse(path('kupshop_content_cart_cart_1'));
    }
}
