<?php
namespace App\Controller;


use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;

use OAT\Library\Lti1p3Core\Message\Launch\Builder\PlatformOriginatingLaunchBuilder;
use OAT\Library\Lti1p3Core\Message\LtiMessageInterface;
use OAT\Library\Lti1p3Core\Message\Payload\Claim\ContextClaim;
use OAT\Library\Lti1p3Core\Registration\RegistrationRepositoryInterface;
use OAT\Library\Lti1p3Core\Resource\LtiResourceLink\LtiResourceLink;
use OAT\Library\Lti1p3Core\Registration\Registration;
use OAT\Library\Lti1p3Core\Platform\Platform;
use OAT\Library\Lti1p3Core\Tool\Tool;
use OAT\Library\Lti1p3Core\Security\Key\KeyChain;
use OAT\Library\Lti1p3Core\Security\Key\Key;
use OAT\Library\Lti1p3Core\Message\Launch\Builder\LtiResourceLinkLaunchRequestBuilder;
use OAT\Library\Lti1p3Core\Security\User\UserAuthenticatorInterface;
use OAT\Library\Lti1p3Core\Security\Oidc\OidcAuthenticator;
use OAT\Library\Lti1p3Core\Security\Oidc\OidcInitiator;
use OAT\Library\Lti1p3Core\Security\User;
use OAT\Library\Lti1p3Core\User\UserIdentity;

use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ServerRequestInterface;

class DefaultController extends AbstractController
{
    private $repo;
    private $builder;

    public function __construct(RegistrationRepositoryInterface $repo, LtiResourceLinkLaunchRequestBuilder $builder)
    {
        $this->repo = $repo;
        $this->builder = $builder;
    }

    private function createServerRequest(string $method, string $uri, array $params = [], array $headers = []): ServerRequestInterface 
    {
        $serverRequest =  (new Psr17Factory())->createServerRequest($method, $uri);

        foreach ($headers as $headerName => $headerValue) {
            $serverRequest = $serverRequest->withAddedHeader($headerName, $headerValue);
        }

        $method = strtoupper($method);

        if ($method === 'GET') {
            return $serverRequest->withQueryParams($params);
        }

        if ($method === 'POST') {
            return $serverRequest->withParsedBody($params);
        }

        return $serverRequest;
    }

    /**
     * @Route("/default/{learnerId}/{quizId}/{quizEndDate}", name="default_list")
     */   
    public function list($learnerId, $quizId, $quizEndDate)
    {
        // Create a builder instance
        $builder = new PlatformOriginatingLaunchBuilder();

        // Get related registration of the launch
        /** @var RegistrationRepositoryInterface $registrationRepository */

        $registration = $this->repo->find('tao');

        $ltiResourceLink = new LtiResourceLink(
            'resourceLinkIdentifier', //needs to be unique
            [
                'url' => 'https://testrunner-ignite.prod.gcp-eu.taocloud.org/api/v1/auth/launch-lti-1p3/' . $quizId,
                'title' => 'LTI Resource Link'
            ]
        );

        $message = $this->builder->buildLtiResourceLinkLaunchRequest(
            $ltiResourceLink,
            $registration,
            $learnerId, // 'loginHint', //json data with learnerid - to fetch learner instance from the repo. implement logic in user authenticator
            "1", //deploymentId1
            [
                'http://purl.imsglobal.org/vocab/lis/v2/membership#Learner' // role
            ],
            [
                'https://purl.imsglobal.org/spec/lti/claim/launch_presentation' => [
                    'return_url' => 'https://portaltest.clicklearning.org/click-home-learning?learner_id=' . $learnerId . '&quiz_id=' . $quizId
                ],
                'https://purl.imsglobal.org/spec/lti/claim/custom' => [
                    'deliverySettings.closeOn' => $quizEndDate . 'T00:00:00+00:00' // Delivery end date
                ]
            ]
        );
        
        // Auto redirection to the tool via the user's browser
        //return new Response(urldecode($quizEndDate));
        return new Response($message->toHtmlRedirectForm());
    }
}

?>