<?php

namespace Mautic\ReportBundle\Form\Type;

use Mautic\CoreBundle\Form\Type\ButtonGroupType;
use Mautic\ReportBundle\Builder\MauticReportBuilder;
use Mautic\ReportBundle\Entity\Report;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * @extends AbstractType<array<mixed>>
 */
class DynamicFiltersType extends AbstractType
{
    public function __construct(
        private TranslatorInterface $translator,
    ) {
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        foreach ($options['report']->getFilters() as $filter) {
            if (isset($filter['dynamic']) && 1 === $filter['dynamic']) {
                $column     = $filter['column'];
                $definition = $options['filterDefinitions']->definitions[$column];

                $operatorGroup = $definition['operatorGroup'] ?? $definition['type'];

                if (!array_key_exists($operatorGroup, MauticReportBuilder::OPERATORS)) {
                    $operatorGroup = 'default';
                }

                $operatorLabelKey = $definition['operators'][$filter['condition']] ?? MauticReportBuilder::OPERATORS[$operatorGroup][$filter['condition']] ?? null;
                $operatorLabel    = $operatorLabelKey ? $this->translator->trans($operatorLabelKey) : null;

                $label = $definition['label'];
                if ($operatorLabel) {
                    $label .= ' ('.$operatorLabel.')';
                }

                $args = [
                    'label'      => $label,
                    'label_attr' => ['class' => 'control-label'],
                    'attr'       => [
                        'class'    => 'form-control',
                        'onchange' => "Mautic.filterTableData('report.".$options['report']->getId()."','".$column."',mQuery(this).val(),'list','.report-content');",
                    ],
                    'required' => false,
                ];

                if (!empty($filter['value'])) {
                    $args['attr']['placeholder'] = $filter['value'];
                } elseif (!empty($definition['defaultValue'])) {
                    $args['attr']['placeholder'] = $definition['defaultValue'];
                }

                switch ($definition['type']) {
                    case 'bool':
                    case 'boolean':
                        $type            = ButtonGroupType::class;
                        $args['choices'] = [
                            [
                                'mautic.core.form.no'      => false,
                                'mautic.core.form.yes'     => true,
                                'mautic.core.filter.clear' => '2',
                            ],
                        ];

                        if (isset($options['data'][$definition['alias']])) {
                            $args['data'] = (1 == (int) $options['data'][$definition['alias']]);
                        } elseif (isset($filter['value']) && '' !== $filter['value']) {
                            $args['data'] = (int) $filter['value'];
                        }
                        break;
                    case 'date':
                        $type           = DateType::class;
                        $args['input']  = 'string';
                        $args['widget'] = 'single_text';
                        $args['html5']  = false;
                        $args['format'] = 'y-MM-dd';
                        $args['attr']['class'] .= ' datepicker';
                        break;
                    case 'datetime':
                        $type           = DateTimeType::class;
                        $args['input']  = 'string';
                        $args['widget'] = 'single_text';
                        $args['html5']  = false;
                        $args['format'] = 'y-MM-dd HH:mm:ss';
                        $args['attr']['class'] .= ' datetimepicker';
                        break;
                    case 'multiselect':
                        $args['multiple']         = true;
                        // no break
                    case 'select':
                        $type            = ChoiceType::class;
                        $args['choices'] = array_flip($definition['list']);
                        break;
                    default:
                        $type = TextType::class;
                        break;
                }

                $builder->add($definition['alias'], $type, $args);
            }
        }
    }

    public function getBlockPrefix(): string
    {
        return 'report_dynamicfilters';
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults(
            [
                'filterDefinitions' => [],
                'report'            => new Report(),
            ]
        );
    }
}
