<?php
namespace App\Form\Type;

use App\Document\Chart;
use App\Form\Type\MetadataType;
use App\Form\Type\ColumnType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ChartType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Jméno grafu'
            ])
            ->add('metadata', MetadataType::class, [
                'label' => false
            ]);

        if (array_key_exists('data', $options) && sizeof($options['data']->getTable()[0]['values']) <= 1000)
            $builder
                ->add('table', CollectionType::class, [
                    'entry_type' => ColumnType::class,
                    'allow_add' => true,
                    /*'prototype' => true,
                    'prototype_data' => 'Placeholder'*/
                ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Chart::class,
        ]);
    }
}