<?php

namespace Lc\SovBundle\Form\Setting;

use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Lc\SovBundle\Definition\SiteSettingDefinitionInterface;
use Lc\SovBundle\Form\Common\FileManagerType;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TimeType;

abstract class BaseSettingType extends AbstractType
{
    protected EntityManagerInterface $em;
    protected SiteSettingDefinitionInterface $settingDefinition;
    protected TranslatorAdmin $translatorAdmin;

    public function __construct(
            EntityManagerInterface $entityManager,
            SiteSettingDefinitionInterface $settingDefinition,
            TranslatorAdmin $translatorAdmin
    ) {
        $this->em = $entityManager;
        $this->settingDefinition = $settingDefinition;
        $this->translatorAdmin = $translatorAdmin;
    }

    public function buildFormSetting($label, $form, $settingDefinition, $settingEntity)
    {
        $settingName = $settingEntity->getName();
        $setting = $settingDefinition->getSettingByName($settingName);
        $settingType = $settingDefinition->getSettingType($settingName);

        if ($settingType == 'text') {
            $form->add(
                    'text',
                    TextType::class,
                    [
                            'label' => $label,
                            'translation_domain' => 'admin',
                    ]
            );
        } elseif ($settingType == 'textarea') {
            $form->add(
                    'text',
                    TextareaType::class,
                    [
                            'label' => $label,
                            'translation_domain' => 'admin',
                    ]
            );
        } elseif ($settingType == 'textarea_advanced') {
            $form->add(
                    'text',
                    CKEditorType::class,
                    [
                            'label' => $label,
                            'translation_domain' => 'admin',
                            'attr' => [
                                    'class' => 'field-text_editor'
                            ]
                    ]
            );
        } elseif ($settingType == 'select') {
            $form->add(
                    'text',
                    ChoiceType::class,
                    [
                            'label' => $label,
                            'translation_domain' => 'admin',
                            'expanded' => false,
                            'multiple' => false,
                            'placeholder' => false,
                            'choices' => $setting['choices']
                    ]
            );
        } elseif ($settingType == 'radio') {
            $form->add(
                    'text',
                    ChoiceType::class,
                    [
                            'label' => $label,
                            'translation_domain' => 'admin',
                            'expanded' => true,
                            'multiple' => false,
                            'placeholder' => false,
                            'choices' => $setting['choices']
                    ]
            );
        } elseif ($settingType == 'date') {
            $form->add(
                    'date',
                    DateType::class,
                    [
                            'label' => $label,
                            'translation_domain' => 'admin',
                            'widget' => 'single_text',
                    ]
            );
        } elseif ($settingType == 'time') {
            $form->add(
                    'date',
                    TimeType::class,
                    [
                            'label' => $label,
                            'translation_domain' => 'admin',
                            'input' => 'datetime',
                            'widget' => 'single_text',
                    ]
            );
        } elseif ($settingType == 'file' || $settingType == 'image') {
            $form->add(
                    'file',
                    FileManagerType::class,
                    [
                            'label' => $label,
                            'translation_domain' => 'admin',
                            'attr' => [
                                    'type' => $settingType
                            ]
                    ]
            );
        }
    }

}

