<?php

namespace App\EventSubscriber;

use App\Core\Event\Setting\SettingEvent;
use App\Core\EventSubscriber\SettingEventSubscriber as EventSubscriber;
use App\Core\Setting\SettingManager;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use App\Core\Form\FileManager\FilePickerType;

/**
 * class SettingEventSubscriber.
 *
 * @author Simon Vieille <simon@deblan.fr>
 */
class SettingEventSubscriber extends EventSubscriber
{
    protected SettingManager $manager;

    public function __construct(SettingManager $manager)
    {
        $this->manager = $manager;
    }

    public function onInit(SettingEvent $event)
    {
        $this->manager->init('blog_footer', '🥾 Pied de page', 'Contenu', '');

        $this->manager->init('avatar_image', '👦 Avatar', 'Image', '');

        $this->manager->init('email_sender', '🤖 E-mail', 'Expéditeur', '');
        $this->manager->init('email_contact', '🤖 E-mail', 'Destinataire formulaire de contact', '');
        $this->manager->init('email_comment', '🤖 E-mail', 'Destinataire nouveau commentaire', '');

        $this->manager->init('stats_umami_url', '📊 Statistiques', 'Adresse tableau de bord Umami', '');
        $this->manager->init('stats_umami_tag', '📊 Statistiques', 'Script Umami', '');

        $this->manager->init('post_author_description', '🖊️ Article', 'Description auteur', '');

        // $this->manager->init('giphy_api_key', '🌐 API', 'Clé d\'API Giphy', '');
    }

    public function onFormInit(SettingEvent $event)
    {
        $data = $event->getData();
        $builder = $data['builder'];
        $entity = $data['entity'];

        if (in_array($entity->getCode(), ['email_sender', 'email_contact', 'email_comment'])) {
            $builder->add(
                'value',
                EmailType::class,
                [
                    'label' => $entity->getLabel(),
                ]
            );
        }

        if (in_array($entity->getCode(), ['avatar_image'])) {
            $builder->add(
                'value',
                FilePickerType::class,
                [
                    'label' => $entity->getLabel(),
                ]
            );
        }

        if (in_array($entity->getCode(), ['giphy_api_key', 'stats_umami_url'])) {
            $builder->add(
                'value',
                TextType::class,
                [
                    'label' => $entity->getLabel(),
                ]
            );
        }

        if (in_array($entity->getCode(), ['stats_umami_tag'])) {
            $event->setOption('view', 'large');

            $builder->add(
                'value',
                TextareaType::class,
                [
                    'label' => $entity->getLabel(),
                    'attr' => [
                        'rows' => 3,
                    ],
                ]
            );
        }

        if (in_array($entity->getCode(), ['blog_footer', 'post_author_description'])) {
            $event->setOption('view', 'large');

            $builder->add(
                'value',
                TextareaType::class,
                [
                    'label' => $entity->getLabel(),
                    'attr' => [
                        'rows' => 20,
                    ],
                ]
            );
        }
    }
}
