<?php

namespace Lc\SovBundle\Definition\Field;

use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Lc\SovBundle\Field\CollectionField;
use Lc\SovBundle\Translation\TranslatorAdmin;
use PhpParser\Node\Expr\Throw_;
use Symfony\Component\Form\Extension\Core\Type\TextType;

abstract class AbstractFieldDefinition
{
    protected array $panels;
    protected array $fieldsByPanel;
    protected array $fieldList;
    protected bool $isPopulate = false;
    protected TranslatorAdmin $translatorAdmin;

    abstract public function configurePanels(): array;

    abstract public function configureFields(): array;

    public function __construct(TranslatorAdmin $translatorAdmin)
    {
        $this->translatorAdmin = $translatorAdmin;
    }

    protected function populate(): void
    {
        if ($this->isPopulate === false) {
            $this->fieldList = $this->configureFields();
            $this->panels = $this->configurePanels();

            foreach ($this->panels as $panel) {

                $method = 'configureFieldsPanel' . ucfirst($panel);
                if (method_exists($this, $method)) {
                    $this->fieldsByPanel[$panel] = array_merge(
                        array('panel_' . $panel => FormField::addPanel($panel)),
                        $this->$method()
                    );
                    $this->fieldList = array_merge($this->fieldList, $this->fieldsByPanel[$panel]);
                }else{
                    throw new \Exception($method . ' n\'esxite pas ');
                }
            }
            $this->isPopulate = false;
        }
    }

    public function getFieldList()
    {
        $this->populate();
        return $this->fieldList;
    }

    public function getFieldListByPanel($panel)
    {
        $this->populate();

        return $this->fieldsByPanel[$panel];
    }

    public function getPanelList()
    {
        $this->populate();
        return $this->fieldsByPanel;
    }

    public function configureFieldsPanelSeo(): array
    {
        return array(
            'metaTitle' => TextField::new('metaTitle')
                ->setLabel('Meta Title')
                ->setHelp('Affiché dans les résultats de recherche Google')
                ->hideOnIndex(),
            'metaDescription' => TextareaField::new('metaDescription')
                ->setLabel('Meta description')
                ->setHelp('Affiché dans les résultats de recherche Google')
                ->hideOnIndex(),
            'oldUrls' => CollectionField::new('oldUrls')
                ->setFormTypeOption('entry_type', TextType::class)
                ->setLabel('Anciennes urls du document')
                ->hideOnIndex(),
        );
    }

    public function configureFieldsPanelConf(): array
    {
        return [
            //FormField::addPanel('configuration')->setTemplateName('crud/field/generic'),
            'devAlias' => TextField::new('devAlias')->hideOnIndex(),
        ];
    }
}