<?php declare(strict_types=1);
/**
 * This file is part of the SplendidBear Websites' projects.
 *
 * Copyright (c) 2019 @ www.splendidbear.org
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Controller;

use App\Entity\ContactMessage;
use App\Form\ContactFormType;
use App\Service\Email\SendContactMailService;
use App\Service\MercureJwtService;
use App\Service\ResolveUserNamesService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Attribute\Route;

/**
 * Class GameController
 *
 * @package  App\Controller
 * @author   Lang <https://www.splendidbear.org>
 * @category Class
 * @license  https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
 * @link     www.splendidbear.org
 * @since    2019. 10. 27.
 */
#[AsController]
class GameController extends AbstractController
{
    public function __construct(
        #[Autowire(env: 'APP_ENV')]
        private readonly string                  $env,
        #[Autowire(env: 'MERCURE_PUBLIC_URL')]
        private readonly string                  $mercurePublicUrl,
        private readonly MercureJwtService       $mercureJwtService,
        private readonly ResolveUserNamesService $opponentNameService,
        private readonly SendContactMailService  $contactMailService,
    ) {
    }

    #[Route('/', name: 'MineSeekerBundle_homepage')]
    public function index(): Response
    {
        return $this->render('Game/index.html.twig');
    }

    #[Route('/play', name: 'MineSeekerBundle_gamePlay')]
    #[Route('/play/{gameAssoc}', name: 'MineSeekerBundle_gamePlayWId')]
    public function play(?string $gameAssoc = null): Response
    {
        return $this->render('Game/play.html.twig', [
            'env'                    => $this->env,
            'mercure_hub_url'        => $this->mercurePublicUrl,
            'mercure_subscriber_jwt' => $this->mercureJwtService->mintSubscriberToken(
                $gameAssoc ?? '', $this->opponentNameService->resolveUserName(),
            ),
            'opponent_name'          => $this->opponentNameService->opponentName($gameAssoc),
        ]);
    }

    #[Route('/privacy-policy', name: 'MineSeekerBundle_privacy')]
    public function privacy(): Response
    {
        return $this->render('Official/privacy.html.twig');
    }

    #[Route('/terms-of-service', name: 'MineSeekerBundle_terms')]
    public function terms(): Response
    {
        return $this->render('Official/terms.html.twig');
    }

    #[Route('/contact', name: 'MineSeekerBundle_contact')]
    public function contact(
        Request                $request,
        EntityManagerInterface $em,
        MailerInterface        $mailer,
    ): Response {
        $contactMessage = new ContactMessage();
        $form = $this->createForm(ContactFormType::class, $contactMessage);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $contactMessage->ipAddress = $request->getClientIp();

            $em->persist($contactMessage);
            $em->flush();

            $this->contactMailService->send($contactMessage);
            $this->addFlash('contact_success', 'Thank you for your message! We will get back to you soon.');

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

        return $this->render('Official/contact.html.twig', [
            'form' => $form,
        ]);
    }

    #[Route('/landing-page', name: 'MineSeekerBundle_landing')]
    public function landing(): Response
    {
        return $this->render('Official/landing.html.twig');
    }

    #[Route('/rules', name: 'MineSeekerBundle_rules')]
    public function rules(): Response
    {
        return $this->render('Official/rules.html.twig');
    }
}
