<?php

namespace App\Controller;

use App\Entity\Subgroup;
use App\Form\SubgroupType;
use App\Repository\SubgroupRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * @Route("/subgroup")
 */
class SubgroupController extends AbstractController
{  

    /**
     * @Route("/{page<[1-9]\d*>}", defaults={"page"=1}, name="subgroup_index")
     */
    public function index(Request $request, int $page)
    {
        $data = $request->request->get('form');
        if(isset($data['search']))
        {
            $filter = $data['search'];
        }
        else
        {
            $filter = '';
        }

        $subgroup = $this->getDoctrine()
                         ->getRepository(Subgroup::class)
                         ->getAllSubgroups($page, $filter);

        return $this->render('subgroup/index.html.twig', [
            'subgroups' => $subgroup,
        ]);
    }


    /**
     * @Route("/new", name="subgroup_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $subgroup = new Subgroup();
        $form = $this->createForm(SubgroupType::class, $subgroup);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($subgroup);
            $entityManager->flush();

            $this->addFlash('notice', 'Subgroup Created successfully!');

            return $this->redirectToRoute('subgroup_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->renderForm('subgroup/new.html.twig', [
            'subgroup' => $subgroup,
            'form' => $form,
        ]);
    }

    /**
     * @Route("/{id}/show", name="subgroup_show", methods={"GET"})
     */
    public function show(Subgroup $subgroup): Response
    {
        return $this->render('subgroup/show.html.twig', [
            'subgroup' => $subgroup,
        ]);
    }

    /**
     * @Route("/{id}/edit", name="subgroup_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, Subgroup $subgroup): Response
    {
        $form = $this->createForm(SubgroupType::class, $subgroup);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            $this->addFlash('notice', 'Subgroup Updated successfully!');

            return $this->redirectToRoute('subgroup_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->renderForm('subgroup/edit.html.twig', [
            'subgroup' => $subgroup,
            'form' => $form,
        ]);
    }

    /**
     * @Route("/{id}/delete", name="subgroup_delete", methods={"POST"})
     */
    public function delete(Request $request, Subgroup $subgroup): Response
    {
        if ($this->isCsrfTokenValid('delete'.$subgroup->getId(), $request->request->get('_token'))) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($subgroup);
            $entityManager->flush();

            $this->addFlash('notice', 'Subgroup Deleted successfully!');
        }

        return $this->redirectToRoute('subgroup_index', [], Response::HTTP_SEE_OTHER);
    }

    /**
     * @Route("/searchBar", name="subgroup_searchBar")
     */
    public function SearchBar()
    {
        $form = $this->createFormBuilder(null)
                     ->add('search', TextType::class, array(
                            'attr' => [
                                'class' => 'search-input',
                                'placeholder' => 'Search by any value'
                            ],
                            'label'=>false,
                        ))
                     ->getForm();

        return $this->render('subgroup/searchBar.html.twig', [
            'form' => $form->createView()
        ]);
    }

    /**
     * @Route("/getsubgroupbygroupid", name="subgroup_getsubgroupbygroupid")
     * @param Request $request
     * @return JsonResponse
     */
    public function getSubgroupByGroupId(Request $request)
    {
        $subgroups = $this->getDoctrine()
                           ->getRepository(Subgroup::class)
                           ->getSubgroupByGroupId($request->query->get("groupId"));

        // Serialize into an array the data that we need, in this case only name and id
        // Note: you can use a serializer as well, for explanation purposes, we'll do it manually
        $responseArray = array();
        foreach($subgroups as $subgroup){
            $responseArray[] = array(
                "id" => $subgroup->getId(),
                "name" => $subgroup->getName()
            );
        }
        
        // Return array with structure of the neighborhoods of the providen city id
        return new JsonResponse($responseArray);

       
    }

}
