<?php

namespace App\Controller\Admin;


use App\Controller\Admin\IndividualData\IndividualDataAdminController;
use App\Entity\Site\Block;
use App\Entity\CollectifData\CollectifData;
use App\Entity\Dream\Dream;
use App\Entity\ProjectBoost\ProjectBoost;
use App\Entity\ProjectInspiring\ProjectInspiring;
use App\Entity\Revolt\Revolt;
use App\Entity\Site\Page;
use App\Entity\Thematic\Subthematic;
use App\Entity\Territory\Territory;
use App\Entity\Thematic\Thematic;
use App\Entity\User\User;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Lc\SovBundle\Controller\Dashboard\DashboardAdminController as SovDashboardController;
use Symfony\Component\HttpFoundation\Response;

class DashboardController extends SovDashboardController
{
    const ROLE_ADMIN = "ROLE_ADMIN";
    const ROLE_SUPER_ADMIN = "ROLE_SUPER_ADMIN";
    const ROLE_ANIMATOR = "ROLE_ANIMATOR";

    private $adminUrlGenerator;

    public function __construct(AdminUrlGenerator $adminUrlGenerator)
    {
        $this->adminUrlGenerator = $adminUrlGenerator;
    }

    public function index(): Response
    {
        $repoCollectifData = $this->getDoctrine()->getRepository(CollectifData::class);
        $repoDream = $this->getDoctrine()->getRepository(Dream::class);
        $repoRevolt = $this->getDoctrine()->getRepository(Revolt::class);
        $repoProjectBoost = $this->getDoctrine()->getRepository(ProjectBoost::class);
        $repoProjectsInsp = $this->getDoctrine()->getRepository(ProjectInspiring::class);
        $repoUser = $this->getDoctrine()->getRepository(User::class);

        $formUnvalid = $repoCollectifData->findByStatus(0);

        $formValid = $repoCollectifData->findByStatus(1);

        $dream = $repoDream->findAll();
        $revolt = $repoRevolt->findAll();
        $projectBoost = $repoProjectBoost->findAll();
        $projectsInsp = $repoProjectsInsp->findAll();

        $animators = $repoUser->findByRole(self::ROLE_ANIMATOR);

        return $this->render(
            '/adminlte/dashboard.html.twig',
            [
                'nbFormUnvalid' => count($formUnvalid),
                'nbFormValid' => count($formValid),
                'nbDream' => count($dream),
                'nbRevolt' => count($revolt),
                'nbProjectBoost' => count($projectBoost),
                'nbProjectsInsp' => count($projectsInsp),
                'nbAnimator' => count($animators)
            ]
        );
    }

    public function configureAssets(): Assets
    {
        $assets = parent::configureAssets();

        $assets->addWebpackEncoreEntry('app-backend');

        return $assets;
    }

    public function configureMenuItems(): iterable
    {
        $urlNewCollectif = $this->adminUrlGenerator
            ->unsetAll()
            ->setController(CollectifDataCrudController::class)
            ->setAction(Action::NEW)
            ->set('menuIndex', 4)
            ->set('submenuIndex', 0)
            ->generateUrl();

        $urlOfflineCollectif = $this->adminUrlGenerator
            ->unsetAll()
            ->setController(CollectifDataCrudController::class)
            ->setAction(Action::INDEX)
            ->set('status', 0)
            ->set('menuIndex', 4)
            ->set('submenuIndex', 0)
            ->generateUrl();

        $urlOnlineCollectif = $this->adminUrlGenerator
            ->unsetAll()
            ->setController(CollectifDataCrudController::class)
            ->setAction(Action::INDEX)
            ->set('status', 1)
            ->set('menuIndex', 4)
            ->set('submenuIndex', 1)
            ->generateUrl();

        $urlNewIndividuel = $this->adminUrlGenerator
            ->unsetAll()
            ->setController(IndividualDataAdminController::class)
            ->setAction(Action::NEW)
            ->set('menuIndex', 3)
            ->set('submenuIndex', 0)
            ->generateUrl();

        $urlOfflineIndividuel = $this->adminUrlGenerator
            ->unsetAll()
            ->setController(IndividualDataAdminController::class)
            ->setAction(Action::INDEX)
            ->set('status', 0)
            ->set('menuIndex', 3)
            ->set('submenuIndex', 0)
            ->generateUrl();

        $urlOnlineIndividuel = $this->adminUrlGenerator
            ->unsetAll()
            ->setController(IndividualDataAdminController::class)
            ->setAction(Action::INDEX)
            ->set('status', 1)
            ->set('menuIndex', 3)
            ->set('submenuIndex', 1)
            ->generateUrl();

        $menuItems = [
            MenuItem::linkToDashboard('dashboard', 'far fa-circle'),
            MenuItem::subMenu('account', 'fas fa-user')
                ->setSubItems(
                    [
                        MenuItem::linkToRoute('account_profile', 'fas fa-user-cog', 'sov_admin_account_profile'),
                        MenuItem::linkToRoute('account_password', 'fas fa-key', 'sov_admin_account_password'),
                    ]
                ),
            MenuItem::linkToCrud('user', 'fas fa-users', User::class)
                ->setPermission(self::ROLE_ADMIN),
            MenuItem::subMenu('data_individual', 'fas fa-database')
                ->setPermission(self::ROLE_ADMIN)
                ->setSubItems(
                    [
                        //MenuItem::linkToUrl('data_individual_new', 'fas fa-plus', $urlNewIndividuel),
                        MenuItem::linkToUrl('data_individual_waiting', 'fas fa-hourglass-half', $urlOfflineIndividuel),
                        MenuItem::linkToUrl('data_individual_validate', 'fas fa-check', $urlOnlineIndividuel),
                    ]
                ),
            MenuItem::subMenu('data_collectif', 'fas fa-database')
                ->setPermission(self::ROLE_ADMIN)
                ->setSubItems(
                    [
                        ///MenuItem::linkToUrl('data_collectif_new', 'fas fa-plus', $urlNewCollectif),
                        MenuItem::linkToUrl('data_collectif_waiting', 'fas fa-hourglass-half', $urlOfflineCollectif),
                        MenuItem::linkToUrl('data_collectif_validate', 'fas fa-check', $urlOnlineCollectif),
                    ]
                ),
            MenuItem::linkToCrud('thematic', 'fas fa-book', Thematic::class)
                ->setPermission(self::ROLE_ADMIN),
            MenuItem::linkToCrud('subthematic', 'fas fa-book', Subthematic::class)
                ->setPermission(self::ROLE_ADMIN),
            MenuItem::linkToCrud('territory', 'far fa-copy', Territory::class)
                ->setPermission(self::ROLE_SUPER_ADMIN),
            MenuItem::linkToCrud('page', 'far fa-copy', Page::class)
                ->setPermission(self::ROLE_ADMIN),
            MenuItem::linkToCrud('blocs', 'fas fa-tags', Block::class)
                ->setPermission(self::ROLE_ADMIN),
        ];

        /*$this->setMenuIndexes($menuItems);*/

        return $menuItems;
    }
}
