<?php

namespace App\Form\Type;

use App\Form\Type\FontType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ColorType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MetadataType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $resolver = new OptionsResolver();
        $this->configureOptions($resolver);

        $builder
            ->add('type', ChoiceType::class, [
                'choices' => [
                    'Bodový' => 'point',
                    'Spojnicový' => 'line',
                    'Hladký spojnicový' => 'smoothline',
                    'Plošný' => 'area',
                    'Hladký plošný' => 'smootharea',
                    'Koláčový' => 'pie',
                    'Prstencový' => 'donut',
                    'Sloupcový' => 'bar',
                    'Skládáný sloupcový' => 'stacked',
                ],
                'label' => 'Druh grafu',
            ])
            ->add(
                $builder->create('group0', FormType::class, [
                    'inherit_data' => true,
                    'label' => 'Nastavení odsazení',
                    'label_attr' => ['class' => 'submenuLabel arrow-down']
                ])
                    ->add('margin', IntegerType::class, [
                        'label' => 'Odsazení',
                        'required' => false,
                        'constraints' => [
                            new Assert\PositiveOrZero()
                        ],
                    ])
            )
            // Title settings
            ->add(
                $builder->create('group1', FormType::class, [
                    'inherit_data' => true,
                    'label' => 'Nastavení nadpisu',
                    'label_attr' => ['class' => 'submenuLabel arrow-down']
                ])
                    ->add('title', TextType::class, [
                        'label' => 'Nadpis',
                        'required' => false,
                        'empty_data' => ''
                    ])
                    ->add('displayTitle', CheckboxType::class, [
                        'label' => 'Zobrazit nadpis',
                        'required' => false,
                    ])
                    ->add('titleFont', FontType::class, [
                        'label' => false,
                        'required' => false,
                    ])
            )
            // Axis label settings
            ->add(
                $builder->create('group2', FormType::class, [
                    'inherit_data' => true,
                    'label' => 'Nastavení popisků os',
                    'label_attr' => ['class' => 'submenuLabel arrow-down']
                ])
                    ->add('xLabel', TextType::class, [
                        'label' => 'Popisek osy X',
                        'required' => false,
                    ])
                    ->add('yLabel', TextType::class, [
                        'label' => 'Popisek osy Y',
                        'required' => false,
                    ])
                    ->add('yStep', NumberType::class, [
                        'label' => 'Krokování hodnot na Y',
                        'required' => false,
                        'constraints' => [
                            new Assert\PositiveOrZero()
                        ],
                    ])
                    ->add('displayAxisValues', CheckboxType::class, [
                        'label' => 'Zobrazit hodnoty',
                        'required' => false,
                    ])
                    ->add('displaySupportLines', CheckboxType::class, [
                        'label' => 'Zobrazit pomocné čáry',
                        'required' => false,
                    ])
                    ->add('labelFont', FontType::class, [
                        'label' => false,
                        'required' => false,
                    ])
            )
            // Legend settings
            ->add(
                $builder->create('group3', FormType::class, [
                    'inherit_data' => true,
                    'label' => 'Nastavení legendy',
                    'label_attr' => ['class' => 'submenuLabel arrow-down']
                ])
                    ->add('displayLegend', CheckboxType::class, [
                        'label' => 'Zobrazit legendu',
                        'required' => false,
                    ])
                    ->add('legendFont', FontType::class, [
                        'label' => false,
                        'required' => false,
                    ])
            )
            // Point settings
            ->add(
                $builder->create('group4', FormType::class, [
                    'inherit_data' => true,
                    'label' => 'Nastavení bodů',
                    'label_attr' => ['class' => 'submenuLabel arrow-down']
                ])
                    ->add('displayPoints', CheckboxType::class, [
                        'label' => 'Zobrazit body',
                        'required' => false,
                    ])
                    ->add('pointSize', IntegerType::class, [
                        'label' => 'Velikost bodů',
                        'required' => false,
                        'constraints' => [
                            new Assert\PositiveOrZero()
                        ],
                    ])
                    ->add('pointBorderSize', IntegerType::class, [
                        'label' => 'Šířka ohraničení bodů',
                        'required' => false,
                        'constraints' => [
                            new Assert\PositiveOrZero()
                        ],
                    ])
                    ->add('pointBorderColor', ColorType::class, [
                        'label' => 'Barva ohraničení',
                        'required' => false,
                    ])
            )
            // Line
            ->add(
                $builder->create('group5', FormType::class, [
                    'inherit_data' => true,
                    'label' => 'Nastavení čar',
                    'label_attr' => ['class' => 'submenuLabel arrow-down']
                ])
                    ->add('lineWidth', NumberType::class, [
                        'label' => 'Tloušťka čáry',
                        'required' => false,
                    ])
            )
            // Zoom settings
            ->add(
                $builder->create('group6', FormType::class, [
                    'inherit_data' => true,
                    'label' => 'Nastavení přiblížení',
                    'label_attr' => ['class' => 'submenuLabel arrow-down']
                ])
                    ->add('horizontalZoom', CheckboxType::class, [
                        'label' => 'Zapnout horizontální přibližování',
                        'required' => false,
                    ])
                    ->add('verticalZoom', CheckboxType::class, [
                        'label' => 'Zapnout vertikální přibližování',
                        'required' => false,
                    ])
            )
            // Background settings
            ->add(
                $builder->create('group7', FormType::class, [
                    'inherit_data' => true,
                    'label' => 'Nastavení pozadí',
                    'label_attr' => ['class' => 'submenuLabel arrow-down']
                ])
                    ->add('backgroundColor', ColorType::class, [
                        'label' => 'Barva pozadí',
                        'required' => false,
                    ])
            )
            ->add('submit', SubmitType::class, ['label' => 'Uložit a vykreslit']);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            array(
                'constraints' => array(
                    new Assert\Callback(function ($data) {
                        // $data is instance of object (or array) with all properties
                        // you can compare Count1, Count2 and Count 3 
                        // and raise validation errors
                    })
                )
            )
        );
        /*$resolver->setDefaults([
            'type' => 'point',
            'margin'=> 5,
            'title'=> '',
            'displayTitle' => true,
        ]);*/
    }
}