<?php

namespace App\Controller;

use App\Document\Chart;
use App\Form\Type\ChartType;
use App\Form\Type\DeleteType;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Redirect;
use Symfony\Component\Routing\Annotation\Route;

#[Route(path: '/charts', name: 'charts_')]
class ChartController extends AbstractController
{
    #[Route('/', name: 'list')]
    public function list(DocumentManager $dm, Request $request) : Response
    {
        if ($this->getUser() === null)
            return $this->redirectToRoute('users_login');

        $charts = $this->getUser()->getCharts();
        return $this->render('chart/list.html.twig', [
            'charts' => $charts
        ]);
    }

    #[Route('/{id}/edit', name: 'edit')]
    public function editAction(DocumentManager $dm, Request $request, Chart $chart) : Response
    {
        if ($chart->getUser() !== $this->getUser()) {
            if ($this->getUser() !== null)
                throw $this->createAccessDeniedException('Tento graf patří jinému uživateli!');
            return $this->redirectToRoute('users_login');
        }
        
        $form = $this->createForm(ChartType::class, $chart);
        $form->handleRequest($request);

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

            $dm->persist($chart);
            $dm->flush();
        }

        return $this->render('chart/edit.html.twig', [
            'chartForm' => $form,
            'id' => $chart->getId(),
        ]);
    }

    #[Route('/create', name: 'create')]
    public function createAction(DocumentManager $dm) : Response
    {
        if ($this->getUser() === null)
            throw $this->createAccessDeniedException('Uživatel musí být přihlášen!');

        $chart = new Chart();
        $dm->persist($chart);
        $this->getUser()->addChart($chart);
        $dm->flush();

        return $this->redirectToRoute('charts_edit', ['id' => $chart->getId()]);
    }

    #[Route('/{id}', name: 'display')]
    public function displayAction(DocumentManager $dm, Chart $chart) : Response
    {
        return $this->render('chart/display.html.twig', [
            'id' => $chart->getId()
        ]);
    }

    #[Route('/{id}/remove', name: 'remove')]
    public function removeAction(DocumentManager $dm, Request $request, Chart $chart) : Response
    {
        if ($chart->getUser() !== $this->getUser()) {
            if ($this->getUser() !== null)
                throw $this->createAccessDeniedException('Tento graf patří jinému uživateli!');
            return $this->redirectToRoute('users_login');
        }

        $form = $this->createForm(DeleteType::class, $chart);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $dm->remove($chart);
            $dm->flush();

            return $this->redirectToRoute('charts_list');
        }

        return $this->render('chart/remove.html.twig', [
            'form' =>$form->createView(),
            'entity' => $chart
        ]);
    }
}
