<?php

namespace KupShop\POSBundle\Controller;

use KupShop\POSBundle\Util\PosEntity;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class PosTabController
{
    /** @var PosEntity */
    protected $posEntity;

    /** @var RequestStack */
    private $requestStack;

    /**
     * @Route("/pos-app/{idPos}/tab/{url}", requirements={"url"=".+"})
     */
    public function posTab(Request $request, string $idPos, string $url = '')
    {
        $this->posEntity->createFromDB($idPos);
        if (empty($this->posEntity->getId())) {
            return new Response('#403 Forbidden', 403);
        }

        $this->requestStack->getSession()->set('pos_entity_id', $this->posEntity->getId());

        return new RedirectResponse("/{$url}?{$request->getQueryString()}");
    }

    /**
     * @required
     */
    public function setPosEntity(?PosEntity $posEntity): void
    {
        $this->posEntity = $posEntity;
    }

    /**
     * @required
     */
    public function setRequestStack(RequestStack $requestStack): void
    {
        $this->requestStack = $requestStack;
    }
}
