<?php

namespace App\Controller;

use App\Repository\ChimpRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
use Symfony\UX\Chartjs\Model\Chart;


class ChartjsController extends AbstractController
{

    /**
     * @Route("/chartjs", name="chartjs",methods={"GET"})
     */
    public function index(ChimpRepository $chimpRepository, ChartBuilderInterface $chartBuilder,EntityManagerInterface $em): Response
    {
        $chimp_controller = new ChimpController($chimpRepository,$em);
        $chimp_controller->updateDataChimp();

        $results=$chimpRepository->findBy([],[
            'sendAt'=> 'ASC'
        ]);
        $label=[];
        $opensData=[];
        $pourcentageOpensData=[];
        $pourcentageClicked=[];
        foreach($results as $result)
        {
            $label[]=date_create($result->getSendAt()->format("j F Y"))->format("j F Y");
            $opensData[]=$result->getOpens();
            $pourcentageOpensData[]=$result->getOpenRate();
            $pourcentageClicked[]=$result->getClickRate();
        }

        $chart1=$this->createChart($chartBuilder,
            $label,
            $opensData,
            "Nombre d'ouvertures du message de la feuille paroissiale");

        $chart2=$this->createChart($chartBuilder,
            $label,
            $pourcentageOpensData,
            "% d'ouvertures du message de la feuille paroissiale");

        $chart3=$this->createChart($chartBuilder,
            $label,
            $pourcentageClicked,
            "% de téléchargement des documents pa rapport au taux d'ouverture ");

         return $this->render('chartjs/index.html.twig', [
            'charts' =>[
                "chart1"    => $chart1,
                "chart2"    => $chart2,
                "chart3"    => $chart3
            ] ,
            'controller_name' => 'ChartjsController',
        ]);
    }

    private function getOptions(string $title) :array
    {
        return
            [
                'title'=>[
                    'display'=>true,
                    'fontColor' => "#000040",
                    'text'  => $title,

                ],
                'legend'=> [
                    'display'=>false
                ],
                'scales' => [
                    'yAxes' => [
                        ['ticks' =>
                            [
                                'min' => 0,
                                'fontColor' => "#000040",
                                'fontSize' => 14
                            ]
                        ],
                    ],
                    'xAxes' => [
                        ['ticks' =>
                            [
                                'fontColor' => "#000040",
                                'fontSize' => 14
                            ]
                        ],
                    ],
                ],

            ];

    }

    private function createChart(
        ChartBuilderInterface $chartBuilder,
        array $label,
        array $data,
        string $title) : Chart
    {
        $chart = $chartBuilder->createChart(Chart::TYPE_LINE);
        $chart->setData([
            'labels' => $label,

            'datasets' => [
                [
                    'fill'          => false,
                    'borderColor'   => 'rgb(75, 192, 192)',
                    'data'          => $data,
                    'tension'       => 0.1
                ]
            ]
        ]);
        $chart->setOptions($this->getOptions($title));
        return $chart;
    }
}
