<?php declare(strict_types=1);

namespace App\Controller;

use Doctrine\Persistence\ManagerRegistry;
use LogicException;
use App\Entity\Camera;
use App\Form\CameraType;
use Doctrine\ORM\ORMInvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Camera controller.
 */
#[Route(path: 'camera')]
class CameraController extends AbstractController
{
	use FormControllerTrait;
	protected const ENTITY = Camera::class;

	protected const FORM = CameraType::class;

	public function __construct(private readonly ManagerRegistry $managerRegistry)
	{
	}

	/**
	 * Lists all camera entities.
	 */
	#[Route(path: '/', name: 'camera_index', methods: ['GET'])]
	public function indexAction() : Response
	{
		$em = $this->managerRegistry->getManager();
		$receivedItems = $em->getRepository(self::ENTITY)->findBy([
			'received' => true
		], [
			'isWorking' => 'ASC',
			'brand' => 'ASC',
			'mount' => 'ASC',
			'model' => 'ASC',
		]);
		$newItems = $em->getRepository(self::ENTITY)->findBy([
			'received' => false
		], [
			'brand' => 'ASC',
			'mount' => 'ASC',
			'model' => 'ASC',
		]);
		$working = array_filter($receivedItems, [$this, 'isWorking']);
		$notWorking = array_filter($receivedItems, [$this, 'isNotWorking']);
		return $this->render('camera/index.html.twig', [
			'not_received' => $newItems,
			'not_working' => $notWorking,
			'working' => $working,
		]);
	}

	/**
	 * Creates a new camera entity.
	 */
	#[Route(path: '/new', name: 'camera_new', methods: ['GET', 'POST'])]
	public function newAction(Request $request)
	{
		return $this->itemCreate($request, 'camera/new.html.twig', 'camera', 'camera_show');
	}

	/**
	 * Finds and displays a camera entity.
	 */
	#[Route(path: '/{id}', name: 'camera_show', methods: ['GET'])]
	public function showAction(Camera $camera)
	{
		return $this->itemView($camera, 'camera/show.html.twig', 'camera');
	}

	/**
	 * Displays a form to edit an existing camera entity.
	 *
	 * @throws LogicException
	 */
	#[Route(path: '/{id}/edit', name: 'camera_edit', methods: ['GET', 'POST'])]
	public function editAction(Request $request, Camera $camera)
	{
		return $this->itemUpdate($request, $camera, 'camera/edit.html.twig', 'camera', 'camera_show');
	}

	/**
	 * Deletes a camera entity.
	 *
	 * @throws LogicException
	 */
	#[Route(path: '/{id}', name: 'camera_delete', methods: ['DELETE'])]
	public function deleteAction(Request $request, Camera $camera) : RedirectResponse
	{
		return $this->itemDelete($request, $camera, 'camera_index');
	}

	/**
	 * Moves a camera to the previouslyOwned table
	 *
	 * @throws LogicException
	 * @throws ORMInvalidArgumentException
	 */
	#[Route(path: '/{id}/deacquire', name: 'camera_deacquire', methods: ['POST'])]
	public function deacquireAction(Request $request, Camera $camera) : RedirectResponse
	{
		return $this->itemDeacquire($request, $camera, 'previously-owned-camera_index');
	}

	/**
	 * Creates a form to delete a camera entity.
	 *
	 * @param Camera $camera The camera entity
	 *
	 * @return FormInterface The form
	 */
	private function createDeleteForm(Camera $camera): FormInterface
	{
		return $this->buildForm($camera, 'camera_delete', 'DELETE');
	}

	/**
	 * Creates a form to move
	 *
	 * @param Camera $camera The camera entity
	 */
	private function createDeacquireForm(Camera $camera): FormInterface
	{
		return $this->buildForm($camera, 'camera_deacquire');
	}

	private function isWorking(Camera $camera): bool
	{
		return $camera->getIsWorking();
	}

	private function isNotWorking(Camera $camera): bool
	{
		return !$this->isWorking($camera);
	}
}