<?php

namespace App\Controller;

use App\Entity\FileAssociationComment;
use App\Form\FileAssociationCommentType;
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 App\Service\FileAssociationCommentTaskService;
use Doctrine\ORM\EntityNotFoundException;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\View;
use Limenius\Liform\Liform;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class FileAssociationCommentTaskController extends FOSRestController
{
    private $service;

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

    /**
     * @Rest\Get("/file/comment/grid")
     * @Rest\View(serializerGroups={"grid"})
     *
     * @param Request $request
     *
     * @return View
     */
    public function getGridAction(Request $request): View
    {
        $dataGrid = new DataGrid();

        $dataGrid
            ->setName('File Association Comment Task')
            ->addHeader(new DataGridHeader(['label' => 'File', 'id' => 'file', 'isSortable' => false, 'type' => 'file']))
            ->addHeader(new DataGridHeader(['label' => 'Comment', 'id' => 'comment', 'isSortable' => true, 'type' => 'comment']))
            ->addHeader(new DataGridHeader(['label' => 'Type', 'id' => 'type', '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(
            'comment',
            [],
            $request->get('comment') ?? []));

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

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

    /**
     * @Rest\Get("/file/comment")
     *
     * @return View
     */
    public function getAllAction(): View
    {
        $items = $this->service->getAll();

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

    /**
     * @Rest\Get("/file/comment/{id}", requirements={"id"="\d+"}))
     *
     * @param int $id
     *
     * @return View
     */
    public function getAction(int $id): View
    {
        try {
            $fileAssociationCommentTask = $this->service->get($id);

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

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

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

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

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

    /**
     * @Rest\Put("/file/comment/{id}")
     *
     * @param Request $request
     * @param int     $id
     *
     * @return View
     */
    public function putAction(Request $request, int $id): View
    {
        try {
            $fileAssociationCommentTask = $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(FileAssociationCommentType::class, $fileAssociationCommentTask, ['csrf_protection' => false]);
        $form->submit($request->request->all());

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

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

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

    /**
     * @Rest\Patch("/file/comment/{id}")
     *
     * @param Request $request
     * @param int     $id
     *
     * @return View
     */
    public function patchAction(Request $request, int $id): View
    {
        try {
            $fileAssociationCommentTask = $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(FileAssociationCommentType::class, $fileAssociationCommentTask, ['csrf_protection' => false]);
        $form->submit($request->request->all(), false);

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

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

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

    /**
     * @Rest\Delete("/file/comment/{id}")
     *
     * @param int $id
     *
     * @return View
     */
    public function deleteAction(int $id): View
    {
        try {
            $fileAssociationCommentTask = $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($fileAssociationCommentTask);

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

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

        $form = $this->createForm(FileAssociationCommentType::class, $fileAssociationCommentTask);

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

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