<?php

namespace App\Controller\Frontend;

use App\Form\SearchListForm;
use App\Repository\DreamStore;
use App\Repository\ProjectBoostStore;
use App\Repository\ProjectInspiringStore;
use App\Repository\RevoltStore;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Lc\SovBundle\Generator\CsvGenerator;
use Symfony\Component\HttpFoundation\Request;

class CartoController extends DefaultController
{
    protected EntityManagerInterface $em;
    protected DreamStore $dreamStore;
    protected RevoltStore $revoltStore;
    protected ProjectBoostStore $projectBoostStore;
    protected ProjectInspiringStore $projectInspiringStore;
    protected PaginatorInterface $paginator;

    public function __construct(
        EntityManagerInterface $em,
        DreamStore $dreamStore,
        RevoltStore $revoltStore,
        ProjectBoostStore $projectBoostStore,
        ProjectInspiringStore $projectInspiringStore,
        PaginatorInterface $paginator
    ) {
        $this->em = $em;
        $this->dreamStore = $dreamStore;
        $this->revoltStore = $revoltStore;
        $this->projectBoostStore = $projectBoostStore;
        $this->projectInspiringStore = $projectInspiringStore;
        $this->paginator = $paginator;
    }

    public function cartoInteractive()
    {
        return $this->render(
            'frontend/carto-int.html.twig',
            [
                'nbContrib' => $this->countContrib()
            ]
        );
    }

    public function cartoListe(Request $request)
    {
        $dreamArray = $revoltArray = $projectBoostArray = $projectInspiringArray = $resultArray = $resultArrayPagination = array();

        $form = $this->createForm(SearchListForm::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();
            $page = $data['page'];
            $description = $data['search'];
            $categoryArray = $data['category'];
            $territoryArray = $data['territory'];
            $thematicArray = $data['thematic'];

            if (in_array('dream', $categoryArray) || empty($categoryArray)) {
                $dreamArray = $this->dreamStore->filterSearch($description, $territoryArray, $thematicArray);
            }
            if (in_array('revolt', $categoryArray) || empty($categoryArray)) {
                $revoltArray = $this->revoltStore->filterSearch($description, $territoryArray, $thematicArray);
            }
            if (in_array('projectBoost', $categoryArray) || empty($categoryArray)) {
                $projectBoostArray = $this->projectBoostStore->filterSearch(
                    $description,
                    $territoryArray,
                    $thematicArray
                );
            }
            if (in_array('projectInspiring', $categoryArray) || empty($categoryArray)) {
                $projectInspiringArray = $this->projectInspiringStore->filterSearch(
                    $description,
                    $territoryArray,
                    $thematicArray
                );
            }

            $resultArray = array_merge($dreamArray, $revoltArray, $projectBoostArray, $projectInspiringArray);

            $resultArrayPagination = $this->paginator->paginate(
                $resultArray,
                $page,
                10
            );
        }

        return $this->render(
            'frontend/carto-liste.html.twig',
            [
                'form' => $form->createView(),
                'nbContrib' => $this->countContrib(),
                'resultArray' => $resultArrayPagination
            ]
        );
    }

    public function cartoCarte()
    {
        return $this->render(
            'frontend/carto-carte.html.twig',
            [
                'nbContrib' => $this->countContrib()
            ]
        );
    }

    public function exportListCsv()
    {
        $csv = new CsvGenerator();
        $csv->enableConvertEncoding('ISO-8859-1');
        $csv->setTitle('Export_Liste', true);

        $columns = [
            'category' => 'Catégorie',
            'thematic' => 'Thématique',
            'subthematic' => 'Contribution',
            'territory' => 'Lieu'
        ];
        $csv->setColumns($columns);

        $data = [

        ];
        $csv->row($data);

        return $csv->getReponse();
    }
}