<?php

namespace Lc\SovBundle\Controller\Setting;

use Doctrine\ORM\EntityManagerInterface;
use Lc\SovBundle\Definition\SiteSettingDefinitionInterface;
use Lc\SovBundle\Form\Setting\SiteSettingsFormType;
use Lc\SovBundle\Repository\Setting\SiteSettingRepository;
use Lc\SovBundle\Repository\Site\SiteRepository;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class SettingAdminController extends AbstractController
{
    protected $em;
    protected $translatorAdmin;
    protected $siteSettingRepository;
    protected $siteSettingDefinition;
    protected $siteRepository;

    public function __construct(
            EntityManagerInterface $em,
            TranslatorAdmin $translatorAdmin,
            SiteSettingRepository $siteSettingRepository,
            SiteSettingDefinitionInterface $siteSettingDefinition,
            SiteRepository $siteRepository
    ) {
        $this->em = $em;
        $this->translatorAdmin = $translatorAdmin;
        $this->siteSettingRepository = $siteSettingRepository;
        $this->siteSettingDefinition = $siteSettingDefinition;
        $this->siteRepository = $siteRepository;
    }

    /**
     * @Route("/admin/setting/site", name="sov_admin_setting_site")
     */
    public function manageGlobal(Request $request)
    {
        $site = $this->siteRepository->findOneByDevAlias('default') ;
        $form = $this->createForm(SiteSettingsFormType::class, $site);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            $this->em->update($site);
            $this->em->flush();

            $this->addFlash('success', $this->translatorAdmin->transFlashMessage('settings_saved'));
        }

        return $this->render(
                '@LcSov/admin/setting/edit_site.html.twig' ,
                [
                        'setting_definition' => $this->siteSettingDefinition,
                        'form' => $form->createView()
                ]
        );
    }

}