<?php

namespace App\Controller;

use App\Entity\Assetname;
use App\Form\AssetnameType;
use App\Repository\AssetnameRepository;
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("/assetname")
 */
class AssetnameController extends AbstractController
{   

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

        $assetname = $this->getDoctrine()
                         ->getRepository(Assetname::class)
                         ->getAllAssetnames($page, $filter);

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


    /**
     * @Route("/new", name="assetname_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $assetname = new Assetname();
        $form = $this->createForm(AssetnameType::class, $assetname);
        $form->handleRequest($request);

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

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

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

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

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

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

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

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

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

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

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

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

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

    /**
     * @Route("/searchBar", name="assetname_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('assetname/searchBar.html.twig', [
            'form' => $form->createView()
        ]);
    }

    /**
     * @Route("/getassetbysubgroupid", name="assetname_getassetbysubgroupid")
     * @param Request $request
     * @return JsonResponse
     */
    public function getAssetBySubgroupId(Request $request)
    {
        $assetnames = $this->getDoctrine()
                           ->getRepository(Assetname::class)
                           ->getAssetBySubgroupId($request->query->get("subGroupId"));
       
        // 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($assetnames as $assetname){
            $responseArray[] = array(
                "id" => $assetname->getId(),
                "name" => $assetname->getName()
            );
        }
        
        // Return array with structure of the neighborhoods of the providen city id
        return new JsonResponse($responseArray);
       
    }

}
