<?php

namespace App\Controller;

use App\Core\Controller\Admin\Crud\CrudController;
use App\Core\Crud\CrudConfiguration;
use App\Core\Crud\Field;
use App\Core\Entity\EntityInterface;
use App\Core\Manager\EntityManager;
use App\Entity\StlMesh as Entity;
use App\Factory\StlMeshFactory as Factory;
use App\Form\StlMeshType as Type;
use App\Repository\StlMeshRepositoryQuery as RepositoryQuery;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;

class StlMeshAdminController extends CrudController
{
    /**
     * @Route("/admin/stl_mesh/{page}", name="admin_stl_mesh_index", methods={"GET"}, requirements={"page":"\d+"})
     */
    public function index(RepositoryQuery $query, Request $request, Session $session, int $page = 1): Response
    {
        return $this->doIndex($page, $query, $request, $session);
    }

    /**
     * @Route("/admin/stl_mesh/new", name="admin_stl_mesh_new", methods={"GET", "POST"})
     */
    public function new(Factory $factory, EntityManager $entityManager, Request $request): Response
    {
        return $this->doNew($factory->create(), $entityManager, $request);
    }

    /**
     * @Route("/admin/stl_mesh/show/{entity}", name="admin_stl_mesh_show", methods={"GET"})
     */
    public function show(Entity $entity): Response
    {
        return $this->doShow($entity);
    }

    /**
     * @Route("/admin/stl_mesh/filter", name="admin_stl_mesh_filter", methods={"GET"})
     */
    public function filter(Session $session): Response
    {
        return $this->doFilter($session);
    }

    /**
     * @Route("/admin/stl_mesh/edit/{entity}", name="admin_stl_mesh_edit", methods={"GET", "POST"})
     */
    public function edit(Entity $entity, EntityManager $entityManager, Request $request): Response
    {
        return $this->doEdit($entity, $entityManager, $request);
    }

    /**
     * @Route("/admin/stl_mesh/sort/{page}", name="admin_stl_mesh_sort", methods={"POST"}, requirements={"page":"\d+"})
     */
    public function sort(RepositoryQuery $query, EntityManager $entityManager, Request $request, Session $session, int $page = 1): Response
    {
        return $this->doSort($page, $query, $entityManager, $request, $session);
    }

    /**
     * @Route("/admin/stl_mesh/batch/{page}", name="admin_stl_mesh_batch", methods={"POST"}, requirements={"page":"\d+"})
     */
    public function batch(RepositoryQuery $query, EntityManager $entityManager, Request $request, Session $session, int $page = 1): Response
    {
        return $this->doBatch($page, $query, $entityManager, $request, $session);
    }

    /**
     * @Route("/admin/stl_mesh/delete/{entity}", name="admin_stl_mesh_delete", methods={"DELETE"})
     */
    public function delete(Entity $entity, EntityManager $entityManager, Request $request): Response
    {
        return $this->doDelete($entity, $entityManager, $request);
    }

    protected function getConfiguration(): CrudConfiguration
    {
        return CrudConfiguration::create()
            ->setPageTitle('index', 'Mesh')
            ->setPageTitle('edit', '{label}')
            ->setPageTitle('new', 'Nouveau modèle')
            ->setPageTitle('show', 'View of {id}')

            ->setPageRoute('index', 'admin_stl_mesh_index')
            ->setPageRoute('new', 'admin_stl_mesh_new')
            ->setPageRoute('edit', 'admin_stl_mesh_edit')
            ->setPageRoute('show', 'admin_stl_mesh_show')
            ->setPageRoute('sort', 'admin_stl_mesh_sort')
            ->setPageRoute('batch', 'admin_stl_mesh_batch')
            ->setPageRoute('delete', 'admin_stl_mesh_delete')
            ->setPageRoute('filter', 'admin_stl_mesh_filter')

            ->setForm('edit', Type::class, [])
            ->setForm('new', Type::class)
            // ->setForm('filter', Type::class)

            // ->setMaxPerPage('index', 20)

            ->setIsSortableCollection('index', true)
            // ->setSortableCollectionProperty('sortOrder')

            // ->setAction('index', 'new', true)
            ->setAction('index', 'show', false)
            // ->setAction('index', 'edit', true)
            // ->setAction('index', 'delete', true)

            // ->setAction('edit', 'back', true)
            ->setAction('edit', 'show', false)
            // ->setAction('edit', 'delete', true)

            // ->setAction('show', 'back', true)
            // ->setAction('show', 'edit', true)

            ->setField('index', 'Label', Field\TextField::class, [
                'property' => 'label',
                'sort' => ['label', '.label'],
                'attr' => ['class' => 'col-md-12'],
            ])

            ->setBatchAction('index', 'delete', 'Delete', function(EntityInterface $entity, EntityManager $manager) {
                $manager->delete($entity);
            })
        ;
    }

    protected function getSection(): string
    {
        return 'stl_mesh';
    }
}
