<?php

/**
 * Created by PhpStorm.
 * User: ondra
 * Date: 4.1.18
 * Time: 9:16.
 */

namespace KupShop\ContentBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class VersionController extends AbstractController
{
    /**
     * @Route("/view/{type}/", name="kupshop_content_version_switchversion", requirements={"type"="desktop|default"})
     */
    public function switchVersionAction(Request $request, $type)
    {
        $url = $request->headers->get('referer');
        if (!$url) {
            $url = '/';
        }

        $redirectResponse = new RedirectResponse($url);

        switch ($type) {
            case 'desktop':
                $redirectResponse->headers->setCookie(new Cookie('web_version', '1'));
                break;
            case 'default':
                $redirectResponse->headers->setCookie(new Cookie('web_version', '0'));
                break;
        }

        return $redirectResponse;
    }
}
