<?php

namespace Lc\SovBundle\Form\Setting;

use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Lc\SovBundle\Definition\SiteSettingDefinitionInterface;
use Lc\SovBundle\Form\Common\FileManagerType;
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;

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

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

}

