<?php

namespace App\Controller;

use App\Entity\CommentLandingPage;
use App\Form\CommentLandingPageType;
use App\Service\CommentLandingPageService;
use App\Service\DataGrid\DataGrid;
use App\Service\DataGrid\DataGridFilterMultiselect;
use App\Service\DataGrid\DataGridFilterQuery;
use App\Service\DataGrid\DataGridHeader;
use App\Service\DataGrid\DataGridNavigation;
use App\Service\DataGrid\DataGridSort;
use Doctrine\ORM\EntityNotFoundException;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
use Limenius\Liform\Liform;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class CommentLandingPageController extends AbstractFOSRestController
{
    private $service;

    public function __construct(CommentLandingPageService $service)
    {
        $this->service = $service;
    }

    /**
     * Retrieves a collection of CommentLandingPage resource.
     *
     * @Rest\Get("/comment/landing/grid")
     * @Rest\View(serializerGroups={"grid"})
     *
     * @param Request $request
     *
     * @return View
     */
    public function getGridAction(Request $request): View
    {
        $dataGrid = new DataGrid();

        $dataGrid
            ->setName('Comment Content')
            ->addHeader(new DataGridHeader(['label' => 'Content', 'id' => 'content', 'isSortable' => true, 'type' => 'content']))
            ->addHeader(new DataGridHeader(['label' => 'Created By', 'id' => 'createdBy', 'isSortable' => true, 'type' => 'user']))
            ->addHeader(new DataGridHeader(['label' => 'Description', 'id' => 'description', 'isSortable' => true]));

        $dataGrid->setNavigation(new DataGridNavigation(['page' => $request->get('page'), 'rpp' => $request->get('rpp')]));

        $dataGrid->addSorter(new DataGridSort(['sort' => $request->get('sort'), 'order' => $request->get('order')]));

        $dataGrid->addFilter(new DataGridFilterQuery($request->get('q')));

        $dataGrid->addFilter(new DataGridFilterMultiselect(
            'content',
            [],
            $request->get('content') ?? []));

        $dataGrid->addFilter(new DataGridFilterMultiselect(
            'createdBy',
            [],
            $request->get('createdBy') ?? []));

        $dataGrid->setData($this->service->getByGrid($dataGrid))
            ->setTotal($this->service->countByGrid($dataGrid));

        return View::create($dataGrid, Response::HTTP_OK);
    }

    /**
     * Retrieves a collection of CommentLandingPage resource.
     *
     * @Rest\Get("/comment/landing")
     *
     * @return View
     */
    public function getAllAction(): View
    {
        $items = $this->service->getAll();

        return View::create($items, Response::HTTP_OK);
    }

    /**
     * Retrieves an CommentLandingPage resource.
     *
     * @Rest\Get("/comment/landing/{id}", requirements={"id"="\d+"}))
     *
     * @param int $id
     *
     * @return View
     */
    public function getAction(int $id): View
    {
        try {
            $commentLandingPage = $this->service->get($id);

            return View::create($commentLandingPage, Response::HTTP_OK);
        } catch (EntityNotFoundException $e) {
            return View::create(['code' => Response::HTTP_NOT_FOUND, 'message' => $e->getMessage()], Response::HTTP_NOT_FOUND);
        }
    }

    /**
     * Creates an CommentLandingPage resource.
     *
     * @Rest\Post("/comment/landing")
     *
     * @param Request $request
     *
     * @return View
     */
    public function postAction(Request $request): View
    {
        $form = $this->createForm(CommentLandingPageType::class, null, ['csrf_protection' => false]);
        $form->submit($request->request->all());

        if ($form->isSubmitted() && $form->isValid()) {
            $commentLandingPage = $this->service->save($form->getData());

            return View::create($commentLandingPage, Response::HTTP_OK);
        }

        return View::create($form, Response::HTTP_BAD_REQUEST);
    }

    /**
     * Replaces CommentLandingPage resource.
     *
     * @Rest\Put("/comment/landing/{id}")
     *
     * @param Request $request
     * @param int     $id
     *
     * @return View
     */
    public function putAction(Request $request, int $id): View
    {
        try {
            $commentLandingPage = $this->service->get($id);
        } catch (EntityNotFoundException $e) {
            return View::create(['code' => Response::HTTP_NOT_FOUND, 'message' => $e->getMessage()], Response::HTTP_NOT_FOUND);
        }

        $form = $this->createForm(CommentLandingPageType::class, $commentLandingPage);
        $form->submit($request->request->all());

        if ($form->isSubmitted() && $form->isValid()) {
            $commentLandingPage = $this->service->save($form->getData());

            return View::create($commentLandingPage, Response::HTTP_OK);
        }

        return View::create($form, Response::HTTP_BAD_REQUEST);
    }

    /**
     * Updates CommentLandingPage resource.
     *
     * @Rest\Patch("/comment/landing/{id}")
     *
     * @param Request $request
     * @param int     $id
     *
     * @return View
     */
    public function patchAction(Request $request, int $id): View
    {
        try {
            $commentLandingPage = $this->service->get($id);
        } catch (EntityNotFoundException $e) {
            return View::create(['code' => Response::HTTP_NOT_FOUND, 'message' => $e->getMessage()], Response::HTTP_NOT_FOUND);
        }

        $form = $this->createForm(CommentLandingPageType::class, $commentLandingPage, ['csrf_protection' => false]);
        $form->submit($request->request->all(), false);

        if ($form->isSubmitted() && $form->isValid()) {
            $commentLandingPage = $this->service->save($form->getData());

            return View::create($commentLandingPage, Response::HTTP_OK);
        }

        return View::create($form, Response::HTTP_BAD_REQUEST);
    }

    /**
     * Removes the CommentLandingPage resource.
     *
     * @Rest\Delete("/comment/landing/{id}")
     *
     * @param int $id
     *
     * @return View
     */
    public function deleteAction(int $id): View
    {
        try {
            $commentLandingPage = $this->service->get($id);
        } catch (EntityNotFoundException $e) {
            return View::create(['code' => Response::HTTP_BAD_REQUEST, 'message' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
        }

        $this->service->delete($commentLandingPage);

        return View::create([], Response::HTTP_NO_CONTENT);
    }

    /**
     * Return serialized form to create resource.
     *
     * @Rest\Get("/comment/landing/schema")
     *
     * @param Liform $liform
     *
     * @return View
     */
    public function getSchemaAction(Liform $liform): View
    {
        $commentLandingPage = new CommentLandingPage();

        $form = $this->createForm(CommentLandingPageType::class, $commentLandingPage);

        $form = $liform->transform($form);

        return View::create($form, Response::HTTP_OK);
    }
}
