<?php

namespace AutoAdmin\Bundle\Admin\Controller\Admin;

use AutoAdmin\Bundle\Admin\Configuration\ConfigManager;
use AutoAdmin\Bundle\Admin\Entity\CookieConsent;
use AutoAdmin\Bundle\Admin\Factory\BuilderFactory;
use AutoAdmin\Bundle\Admin\Factory\ContentFactory;
use AutoAdmin\Bundle\Admin\Finder\EntityFinder;
use AutoAdmin\Bundle\Admin\Form\CookieConsentType;
use AutoAdmin\Bundle\Admin\Helper\AccessHelper;
use AutoAdmin\Bundle\Admin\Helper\EntityHelper;
use AutoAdmin\Bundle\Admin\Repository\CookieConsentRepository;
use AutoAdmin\Bundle\Admin\Router\AdminRouter;
use AutoAdmin\Bundle\Content\Routing\ContentRouter;
use AutoAdmin\Bundle\Content\Site\SiteContextHelper;
use AutoAdmin\Bundle\Seo\Generator\GeneratorManager;
use DateTime;
use Symfony\Component\HttpFoundation\Request;

class CookieConsentController extends AbstractAdminController
{
    public function index(Request $request)
    {
        $from = (new DateTime())->setTime(0, 0);
        $to = (new DateTime())->setTime(23, 59, 59);

        $form = $this->createForm(CookieConsentType::class, [
            'from' => $from,
            'to' => $to,
        ]);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();

            $from = $data['from'];
            $to = $data['to'];
        }

        /** @var CookieConsentRepository $cookieConsentRepository */
        $cookieConsentRepository = $this->entityHelper->getManager()->getRepository(CookieConsent::class);

        $from = $from->setTime(0, 0);
        $to = $to->setTime(23, 59, 59);

        $cookieConsents = $cookieConsentRepository->findBetweenDate($from, $to);
        $countCookieConsentTotal = count($cookieConsents);
        $countCookieConsentEmpty = 0;
        $countCookieConsentTrue = 0;
        $countCookieConsentFalse = 0;

        foreach ($cookieConsents as $cookieConsent) {
            if (CookieConsent::COOKIE_CONSENT_EMPTY === $cookieConsent->getType()) {
                $countCookieConsentEmpty++;
            } elseif (CookieConsent::COOKIE_CONSENT_TRUE === $cookieConsent->getType()) {
                $countCookieConsentTrue++;
            } elseif (CookieConsent::COOKIE_CONSENT_FALSE === $cookieConsent->getType()) {
                $countCookieConsentFalse++;
            }
        }

        $countCookieConsentEmptyPercent = 0;
        $countCookieConsentTruePercent = 0;
        $countCookieConsentFalsePercent = 0;

        if ($countCookieConsentTotal > 0) {
            $countCookieConsentEmptyPercent = round($countCookieConsentEmpty / $countCookieConsentTotal * 100, 2);
            $countCookieConsentTruePercent = round($countCookieConsentTrue / $countCookieConsentTotal * 100, 2);
            $countCookieConsentFalsePercent = round($countCookieConsentFalse / $countCookieConsentTotal * 100, 2);
        }

        return $this->render('@AutoAdminAdmin/cookie_consent/index.html.twig', [
            'mainTitle' => 'Consentement aux cookies',
            'countCookieConsentTotal' => $countCookieConsentTotal,
            'countCookieConsentEmpty' => $countCookieConsentEmpty,
            'countCookieConsentEmptyPercent' => $countCookieConsentEmptyPercent,
            'countCookieConsentTrue' => $countCookieConsentTrue,
            'countCookieConsentTruePercent' => $countCookieConsentTruePercent,
            'countCookieConsentFalse' => $countCookieConsentFalse,
            'countCookieConsentFalsePercent' => $countCookieConsentFalsePercent,
            'form' => $form->createView(),
        ]);
    }
}
