<?php

namespace Lc\CaracoleBundle\Controller\Merchant;

use Lc\CaracoleBundle\Container\Merchant\MerchantContainer;
use Lc\CaracoleBundle\Controller\AbstractController;
use Lc\CaracoleBundle\Definition\MerchantSettingDefinition;
use Lc\CaracoleBundle\Form\Merchant\SwitchMerchantFormType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class SwitchMerchantController extends AbstractController
{
    /**
     * @Route("/merchant/switch", name="carac_merchant_switch")
     */
    public function switchMerchant(Request $request)
    {
        $form = $this->createForm(SwitchMerchantFormType::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $merchant = $form->get('merchant')->getData();
            $context = $form->get('context')->getData();

            if ($merchant) {
                $url = $this->getSettingValue(
                        $merchant,
                        MerchantSettingDefinition::SETTING_URL
                );

                if ($context == 'admin') {
                    $url .= 'admin';
                }

                if ($url) {
                    return $this->redirect($url);
                }
            }
        }
    }

}