<?php

namespace App\Controller\Admin;

use App\Entity\Settings;
use App\Form\SettingsType;
use App\Utils\SettingsHelper;
use App\Entity\Setting;
use App\Form\SettingOneWysiwygType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

#[Route("/admin", name: "admin_")]
class SettingsController extends AbstractController
{
    #[Route('/l-art-du-collectif', name: 'whoarewe')]
    public function whoarewe(EntityManagerInterface $manager, Request $request, TranslatorInterface $t) {
        // return $this->_one_wysiwyg_edit("whoarewe", $manager, $request, $t);
        return $this->_settings_edit("admin.settings.pages.whoarewe", ["www-subtitle", "whoarewe", "whoarewe-sponsored-by", "seo-keywords", "seo-description"], $manager, $request, $t);
    }

    #[Route('/mentions-legales', name: 'legal')]
    public function legal(EntityManagerInterface $manager, Request $request, TranslatorInterface $t) {
        return $this->_settings_edit("admin.settings.pages.whoarewe", ["legal"], $manager, $request, $t);
    }
    
    public function _settings_edit(string $page_title, array $settings_name, EntityManagerInterface $manager, Request $request, TranslatorInterface $t) {

        if (!$this->getUser()) { return $this->redirectToRoute('auth_login');}
        if (!in_array("ROLE_ADMIN", $this->getUser()->getRoles())) throw new AccessDeniedException();

        $settHelper = new SettingsHelper();
        // vérification des settings
        foreach ($settings_name as $setting) {
            $settHelper->checkExistance($setting);
        }

        $settings = $manager->getRepository(Setting::class)->findBy(["setting" => $settings_name]);

        $collection = new Settings;

        foreach ($settings_name as $setting_name) {
            $found = false;
            foreach ($settings as $sett) {
                if ($sett->getSetting() == $setting_name) {
                    $found = true; 
                    $collection->addSetting($sett);
                    break;
                }
            }
            if (!$found) {
                $sett = new Setting();
                $sett->setSetting($setting_name);
                $sett->setType($settHelper->getType($setting_name));

                $collection->addSetting($sett);
            }
        }
        // if (!$settings) {
        // }

        $form = $this->createForm(SettingsType::class, $collection);
        $form->handleRequest($request);

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

            foreach ($collection->getSettings() as $setting) {
                $manager->persist($setting);
            }
            $manager->flush();

            $this->addFlash('success', $t->trans('admin.settings.flash.updated-ok', domain: 'admin'));

            // on redirige vers la liste
            return $this->redirectToRoute("admin_home");
        }

        return $this->render('admin/settings/settings-edit.html.twig',
            [ 
                "form" => $form->createView(),
                "title" => $page_title,
                "page_title" => $page_title
             ]
        );
    }
}
