<?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\Project as Entity;
use App\Factory\ProjectFactory as Factory;
use App\Form\ProjectType as Type;
use App\Repository\ProjectRepositoryQuery as RepositoryQuery;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Project;

class ProjectAdminController extends CrudController
{
    /**
     * @Route("/admin/project/{page}", name="admin_project_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/project/new", name="admin_project_new", methods={"GET", "POST"})
     */
    public function new(Factory $factory, EntityManager $entityManager, Request $request): Response
    {
        return $this->doNew($factory->create(), $entityManager, $request);
    }

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

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

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

    /**
     * @Route("/admin/project/sort/{page}", name="admin_project_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/project/batch/{page}", name="admin_project_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/project/delete/{entity}", name="admin_project_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', 'Projets')
            ->setPageTitle('edit', '{label}')
            ->setPageTitle('new', 'Nouveau projet')
            // ->setPageTitle('show', 'View of {id}')

            ->setPageRoute('index', 'admin_project_index')
            ->setPageRoute('new', 'admin_project_new')
            ->setPageRoute('edit', 'admin_project_edit')
            // ->setPageRoute('show', 'admin_project_show')
            ->setPageRoute('sort', 'admin_project_sort')
            ->setPageRoute('batch', 'admin_project_batch')
            ->setPageRoute('delete', 'admin_project_delete')
            ->setPageRoute('filter', 'admin_project_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',
            ])
            ->setField('index', 'Status', Field\TextField::class, [
                'view' => 'blog/project_admin/field/status.html.twig',
                'sort' => ['status', '.status'],
                'attr' => ['class' => 'miw-100'],
            ])

            ->setBatchAction('index', 'delete', 'Delete', function(EntityInterface $entity, EntityManager $manager) {
                $manager->delete($entity);
            })
            ->setBatchAction('index', 'draft', 'Statut : publier', function(EntityInterface $entity, EntityManager $manager) {
                $manager->update($entity->setStatus(Project::PUBLISHED));
            })
            ->setBatchAction('index', 'publish', 'Statut : brouillon', function(EntityInterface $entity, EntityManager $manager) {
                $manager->update($entity->setStatus(Project::DRAFT));
            })
        ;
    }

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