<?php

namespace App\Controller;

use App\Entity\Series;
use App\Entity\Speaker;
use App\Entity\Template;
use App\Entity\Notes;

use Doctrine\ORM\EntityManagerInterface;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class AjaxController extends AbstractController
{
    /**
     * A method to retrieve a template
     *
     * @param Request $req description
     * @param EntityManagerInterface $emi description
     *
     * @return Response
     */
    #[Route('/retrieve-template', name: 'app_retrieve_template')]
    public function retrieveTemplate(Request $req, EntityManagerInterface $emi): Response
    {
        $ret = new Response();
        $template_id = json_decode($req->getContent())->template;
        $template = $emi->getRepository(Template::class)->find($template_id);
        $ret->setContent($template->getValue());

        return $ret;
    }

    #[Route('/save-template', name: 'app_save_template')]
    public function saveTemplate(Request $req, EntityManagerInterface $emi): Response
    {
        $ret = new Response();
        $req = json_decode($req->getContent());
        $template_value = $req->template_value;
        $template_id = $req->template_id;
        $template_name = $req->template_name;

        if($template_id) {
            $template = $emi->getRepository(Template::class)->find($template_id);
            $template->setName($template_name);
            $template->setValue($template_value);
            $emi->persist($template);
            $emi->flush();
            $ret->setContent('updated');
        } else {
            $template = new Template();
            $template->setName($template_name);
            $template->setValue($template_value);
            $emi->persist($template);
            $emi->flush();
            $ret->setContent('added');
        }

        return $ret;
    }

    /**
     * Retrieve a speaker from the database based on the given request and entity manager.
     *
     * @param Request $req The request object
     * @param EntityManagerInterface $emi The entity manager
     * @return Response
     */
    #[Route('/retrieve-speaker', name: 'app_retrieve_speaker')]
    public function retrieveSpeaker(Request $req, EntityManagerInterface $emi): Response
    {
        $ret = new Response();
        $speaker_id = json_decode($req->getContent())->speaker;
        $speaker = $emi->getRepository(Speaker::class)->find($speaker_id);
        $ret->setContent($speaker->getProfile());

        return $ret;
    }

    #[Route('/save-speaker', name: 'app_save_speaker')]
    public function saveSpeaker(Request $req, EntityManagerInterface $emi): Response
    {
        $ret = new Response();
        $req = json_decode($req->getContent());
        $speaker_id = $req->speaker_id;
        $speaker_name = $req->speaker_name;

        if ($speaker_id) {
            $speaker = $emi->getRepository(Speaker::class)->find($speaker_id);
            $speaker->setName($speaker_name);
            $emi->persist($speaker);
            $emi->flush();
            $ret->setContent('updated');
        } else {
            $speaker = new Speaker();
            $speaker->setName($speaker_name);
            $emi->persist($speaker);
            $emi->flush();
            $ret->setContent('added');
        }

        return $ret;
    }

    /**
     * Method to retrieve a series
     *
     * @param Request $req description
     * @param EntityManagerInterface $emi description
     *
     * @return Response
     */
    #[Route('/retrieve-series', name: 'app_retrieve_series')]
    public function retrieveSeries(Request $req, EntityManagerInterface $emi): Response
    {
        $ret = new Response();
        $series_id = json_decode($req->getContent())->series;
        $series = $emi->getRepository(Series::class)->find($series_id);
        $ret->setContent($series->getName());

        return $ret;
    }

    #[Route('/save-series', name: 'app_save_series')]
    public function saveSeries(Request $req, EntityManagerInterface $emi): Response
    {
        $ret = new Response();
        $req = json_decode($req->getContent());
        $series_id = $req->series_id;
        $series_name = $req->series_name;

        if ($series_id) {
            $series = $emi->getRepository(Series::class)->find($series_id);
            $series->setName($series_name);
            $emi->persist($series);
            $emi->flush();
            $ret->setContent('updated');
        } else {
            $series = new Series();
            $series->setName($series_name);
            $emi->persist($series);
            $emi->flush();
            $ret->setContent('added');
        }

        return $ret;
    }

    #[Route('/autocomplete-reference', name: 'app_autocomplete_reference')]
    public function autocompleteReference(Request $req): Response
    {
        $res = new Response();

        return $res;
    }

    #[Route('/retrieve-reference', name: 'app_retrive_reference')]
    public function retrieveReference(Request $req): Response
    {
        $type = null;
        $search = null;
        $passage = null;

        $res = new Response();
        $ref = json_decode($req->getContent())->reference;
        if (count(explode(':', $ref)) > 2) {
            list($type, $search, $passage) = explode(':', $ref);
        } else {
            list($type, $search) = explode(':', $ref);
        }

        $ret = match(strtolower($type)) {
            'bible' => ReferenceController::retrieveBible("{$search}:{$passage}"),
            'hc' => ReferenceController::retrieveHC($search),
            'bc' => ReferenceController::retrieveBC($search),
            'dc' => ReferenceController::retrieveCD($search),
            'wcf' => ReferenceController::retrieveWCF($search),
            'wsc' => ReferenceController::retrieveWSC($search),
            'wlc' => ReferenceController::retrieveWLC($search),
            'creed' => ReferenceController::retrieveCreed($search)
        };

        $res->setContent(json_encode(['text' => $ret, 'title' => "{$search}"]));

        return $res;
    }

    #[Route('/open-note', name: 'app_open_note')]
    public function openNote(Request $req, EntityManagerInterface $emi): Response
    {
        $res = new Response();

        return $res;
    }

    #[Route('/save-note', name: 'app_save_note')]
    public function saveNote(Request $req, EntityManagerInterface $emi): Response
    {
        $res = new Response();
        $note = new Notes();
        $note->setTitle($req->get('title'));

        $series = $emi->getRepository(Series::class)->find($req->get('series'));
        $speaker = $emi->getRepository(Speaker::class)->find($req->get('speaker'));
        $note->setSeries($series);
        $note->setSpeaker($speaker);
        $note->setText($req->get('note'));

        $res->setContent(json_encode($note));

        return $res;
    }
}
