<?php

namespace App\Controller;

use App\Entity\Archive;
use App\Entity\Commune;
use App\Entity\Ministry;
use App\Entity\Department;
use App\Entity\Goal;
use App\Entity\Target;
use App\Utils\YearsUtils;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
 * @Route("/{_locale}", requirements={ "_locale": "%locale_regex%" })
 */
class IndexController extends AbstractController
{
    private $params;

    public function __construct(ParameterBagInterface $params)
    {
        $this->params = $params;
    }

    /**
     * @Route("/old", name="homepage")
     */
    public function indexAction()
    {
        $goals = $this->getDoctrine()->getRepository(Goal::class)->findBy(['agenda' => 2030]);

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

    /**
     * @Route("/carteold", name="carteold")
     */
    public function carteAction()
    {
        $departements = $this->getDoctrine()->getRepository(Department::class)->findAll();
        $communes = $this->getDoctrine()->getRepository(Commune::class)->findAll();

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

    /**
     * @Route("/services", name="services")
     */
    public function servicesAction()
    {
        $goals = $this->getDoctrine()->getRepository(Goal::class)->findBy(['agenda' => 2030]);

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

    /**
     * @Route("/index/goals/{id}", name="index_goal")
     */
    public function indexGoalAction($id, Request $request)
    {
        $goals = $this->getDoctrine()->getRepository(Goal::class)->findBy(['agenda' => 2030]);

        if ($id == 0) {
            $startYear = $this->params->get('mesodd_year');
            $years = YearsUtils::choicesYears($startYear);
            foreach ($years as $year) {
                $results = $this->getDoctrine()
                    ->getRepository(Archive::class)
                    ->findBy(['year' => $year]);
                if ($results) {
                    $rows[$year] = $results;
                }
            }

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

        $goal = $this->getDoctrine()
            ->getRepository(Goal::class)
            ->findOneBy(['id' => $id]);

        $targets = $this->getDoctrine()
            ->getRepository(Target::class)
            ->findBy(['goal' => $id]);

        return $this->render('index/show_odd.html.twig', [
            'goals' => $goals,
            'goal' => $goal,
            'targets' => $targets,
        ]);
    }

    /**
     * @Route("/index/targets/{id}", name="index_target")
     */
    public function indexTargetAction($id, Request $request)
    {
        $target = $this->getDoctrine()->getRepository(Target::class)->findOneBy(['id' => $id]);
        $goal=$target->getGoal();
        $targets=$goal->getTargets();

        return $this->render('index/show_target.html.twig', [
            'goal' => $goal,
            'target' => $target,
            'targets' => $targets,
        ]);
    }

    /**
     * @Route("/index/articles/{id}", name="index_article")
     */
    public function indexArticleAction($id, Request $request)
    {
        return $this->render('index/show_article.html.twig', [
            'article' => $id,
        ]);
    }

    /**
     * @Route("/about", name="about")
     */
    public function aboutAction()
    {
        return $this->render('index/about.html.twig', [
        ]);
    }

    /**
     * @Route("/team", name="team")
     */
    public function teamAction()
    {
        return $this->render('index/team.html.twig', [
        ]);
    }

    /**
     * @Route("/contactold", name="contactold")
     */
    public function contactAction()
    {
        return $this->render('index/contact.html.twig', [
        ]);
    }



   /**
     * @Route("/apps", name="app_apps")
     */
    public function appsAction(Request $request)
    {
        if($this->isGranted('ROLE_INSTAD')){
            $request->getSession()->set('_application', 1);
            return $this->redirectToRoute('app_suiodd_dashboard');
        }

        $ministry = $request->getSession()->get('_ministry');
        if($ministry){
            $ministry = $this->getDoctrine()->getRepository(Ministry::class)->findOneBy(['id' => $ministry->getId()]);
            return $this->render('apps.html.twig', [
                'ministry' => $ministry
            ]);
        }

        $departement = $request->getSession()->get('_departement');
        if($departement){
            $departement = $this->getDoctrine()->getRepository(Department::class)->findOneBy(['id' => $departement->getId()]);
            return $this->render('apps_departements.html.twig', [
                'departement' => $departement,
            ]);
        }

        $commune = $request->getSession()->get('_commune');
        if($commune){
            $commune = $this->getDoctrine()->getRepository(Commune::class)->findOneBy(['id' => $commune->getId()]);
            return $this->render('apps_communes.html.twig', [
                'commune' => $commune,
            ]);
        }

        return $this->render('apps.html.twig', [
            'ministry' => $ministry
        ]);

        /*if($this->getUser()->getCommune()){
            $request->getSession()->set('_application', 1);
            return $this->redirectToRoute('app_commune_dashboard');
        }
        if($this->isGranted('ROLE_INSTAD')){
            $request->getSession()->set('_application', 1);
            return $this->redirectToRoute('app_suiodd_dashboard');
        }
        if($this->isGranted('ROLE_DDD')){
            $request->getSession()->set('_application', 1);
            return $this->redirectToRoute('app_departement_dashboard');
        }
        $ministry = $request->getSession()->get('_ministry');
        if($ministry){
            $ministry = $this->getDoctrine()->getRepository(Ministry::class)->findOneBy(['id' => $ministry->getId()]);
        }
        return $this->render('apps.html.twig', [
            'ministry' => $ministry,
        ]);*/
    }


}