<?php


namespace Opentalent\OtCore\Tests\Unit\Fixtures;


use TYPO3\CMS\Core\Http\HtmlResponse;


/**
 * This class return responses that mime the Opentalent API
 *
 * @package Opentalent\OtCore\Tests\Unit\Fixtures
 */
class ApiResponseFixtures
{
    private $responses = [
        'stub' => '{"@context": "/api/contexts/PortailOrganization"}',
        'org' => '
        {
            "@context": "/api/contexts/PortailOrganization",
            "@id": "/api/public/organizations?_format=json&filter[where][id]=1",
            "@type": "hydra:PagedCollection",
            "hydra:totalItems": 1,
            "hydra:itemsPerPage": 30,
            "hydra:firstPage": "/api/public/organizations?_format=json&filter%5Bwhere%5D%5Bid%5D=1",
            "hydra:lastPage": "/api/public/organizations?_format=json&filter%5Bwhere%5D%5Bid%5D=1",
            "hydra:member": [
                {
                    "@id": "/api/public/organizations/1",
                    "@type": "PortailOrganization",
                    "subDomain": "org",
                    "name": "Name",
                    "slug": "org-1",
                    "principalType": "ARTISTIC_PRACTICE_ONLY",
                    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi faucibus nulla et nulla maximus tempor. Nullam eros nunc, sodales nec turpis vel, semper consectetur eros. Vivamus id porttitor turpis. Cras at urna faucibus, cursus eros ac, eleifend enim. Maecenas id leo id ante lacinia tempor. Cras est eros, pellentesque et luctus non, tempor in turpis. Maecenas interdum, nisi eu posuere vestibulum, ex erat accumsan sem, in tempor sem mi scelerisque sapien. ",
                    "categories": [
                        "1MC"
                    ],
                    "addressCity": "CITY",
                    "postalCode": "00000",
                    "streetAddress": "street",
                    "latitude": 1.2345,
                    "longitude": 1.2345,
                    "country": "France",
                    "logo": "https://api.opentalent.fr/_internal/secure/files/1",
                    "parentId": 1,
                    "parentName": "Network",
                    "parentSubDomain": "network",
                    "publicationDirectors": ""
                }],
        "hydra:search": {}
        }',
        'event' => '
        {
            "@context": "/api/contexts/PortailEvent",
            "@id": "/api/public/events?_format=json&filter[where][id]=2",
            "@type": "hydra:PagedCollection",
            "hydra:totalItems": 1,
            "hydra:itemsPerPage": 30,
            "hydra:firstPage": "/api/public/events?_format=json&filter%5Bwhere%5D%5Bid%5D=2",
            "hydra:lastPage": "/api/public/events?_format=json&filter%5Bwhere%5D%5Bid%5D=2",
            "hydra:member": [
                {
                    "@id": "/api/public/events/2",
                    "@type": "PortailEvent",
                    "organizationId": 1,
                    "subDomain": "org",
                    "name": "The Event",
                    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi faucibus nulla et nulla maximus tempor. Nullam eros nunc, sodales nec turpis vel, semper consectetur eros. Vivamus id porttitor turpis. Cras at urna faucibus, cursus eros ac, eleifend enim. Maecenas id leo id ante lacinia tempor. Cras est eros, pellentesque et luctus non, tempor in turpis. Maecenas interdum, nisi eu posuere vestibulum, ex erat accumsan sem, in tempor sem mi scelerisque sapien. ",
                    "categories": [],
                    "url": "http://www.org.fr/",
                    "rule": null,
                    "datetimeStart": "2021-01-01T17:00:00+00:00",
                    "datetimeEnd": "2021-01-01T18:00:00+00:00",
                    "dates": null,
                    "placeName": "Place",
                    "placeDescription": null,
                    "placeFloorSize": null,
                    "placeCapacity": null,
                    "city": "City",
                    "postalCode": "00000",
                    "streetAddress": "street",
                    "longitude": 1.2345,
                    "latitude": 1.2345,
                    "roomName": null,
                    "roomDescription": null,
                    "roomLocalisation": null,
                    "roomCapacity": null,
                    "roomFloorSize": null,
                    "zupId": null,
                    "deepLink": null,
                    "image": "https://api.opentalent.fr/_internal/secure/files/2",
                    "priceMini": null,
                    "meetingSchedule": [],
                    "api": true,
                    "parentName": "Network",
                    "parentSubDomain": "network"
                }
        ],
        "hydra:search":{}
        }',
        'donor' => '
        {
            "@context": "/api/contexts/PortailDonor",
            "@id": "/api/public/donors?_format=json&organizationId=1",
            "@type": "hydra:PagedCollection",
            "hydra:totalItems": 1,
            "hydra:itemsPerPage": 30,
            "hydra:firstPage": "/api/public/donors?_format=json&organizationId=1",
            "hydra:lastPage": "/api/public/donors?_format=json&organizationId=1",
            "hydra:member": [
                {
                    "@id": "/api/public/donors/3",
                    "@type": "PortailDonor",
                    "organizationId": 1,
                    "name": "Donor",
                    "email": "email@domain.com",
                    "telphone": "0000000000",
                    "website": "www.website.com",
                    "wording": null,
                    "displayedOn": "NONE",
                    "logo": "https://api.opentalent.fr/_internal/secure/files/1"
                }
            ],
            "hydra:search": {}
        }',
        'member' => '
        {
            "@context": "/api/contexts/PortailMemberBySpeciality",
            "@id": "/api/public/members?_format=json&filter[where][id]=4",
            "@type": "hydra:PagedCollection",
            "hydra:totalItems": 1,
            "hydra:itemsPerPage": 30,
            "hydra:firstPage": "/api/public/members?_format=json&filter%5Bwhere%5D%5Bid%5D=4",
            "hydra:lastPage": "/api/public/members?_format=json&filter%5Bwhere%5D%5Bid%5D=4",
            "hydra:member": [
                {
                    "@id": "/api/public/members/5",
                    "@type": "PortailMemberBySpeciality",
                    "organizationId": 1,
                    "gender": "MISS",
                    "givenName": "Marie-Aiglantine",
                    "name": "De Saint-Marcellin",
                    "startDate": "2010-01-01T00:00:00+00:00",
                    "endDate": null,
                    "mission": "STUDENT",
                    "instrumentGroup": "OBOE",
                    "instrument": "OBOE",
                    "personId": 4,
                    "image": "https://api.opentalent.fr/_internal/secure/files/1"
                }
            ],
            "hydra:search": {}
        }'
    ];

    /**
     * Do the mapping between URI and responses
     * /!\ Each of these uri shall be tested against the Opentalent API. Without
     *     this, those tests won't prove anything...
     *
     * @var string[]
     */
    private $map = [
        'api/public/organizations' => 'stub',
        'api/public/organizations?_format=json' => 'stub',
        'api/public/organizations?_format=json&itemsPerPage=10&foo=1' => 'stub',
        'api/public/organizations?_format=json&itemsPerPage=8' => 'stub',
        'api/public/organizations?_format=json&filter%5Bwhere%5D%5Bid%5D=1&page=1&totalItems=1&itemsPerPage=8' => 'org',
        'api/public/organizations?_format=json&filter%5Bwhere%5D%5Bname%5D=a+name&page=1&totalItems=1&itemsPerPage=8' => 'org',
        'api/public/organizations?_format=json&parentId=1&children=1&page=1&itemsPerPage=8' => 'org',
        'api/public/organizations?_format=json&filter%5Bwhere%5D%5Bid%5D=1&itemsPerPage=8' => 'org',
        'api/public/organizations?_format=json&filter%5Bwhere%5D%5Bname%5D=a+unknown+name&page=1&totalItems=1&itemsPerPage=8' => 'org',
        'api/public/events?_format=json&filter%5Bwhere%5D%5Bid%5D=2&page=1&totalItems=1&itemsPerPage=8' => 'event',
        'api/public/events?_format=json&organizationId=1&page=1&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&itemsPerPage=8' => 'event',
        'api/public/events?_format=json&organizationId=1&page=1&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&parent=1&itemsPerPage=8' => 'event',
        'api/public/events?_format=json&organizationId=1&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&children=1&itemsPerPage=8' => 'event',
        'api/public/events?_format=json&organizationId=1&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&filter%5Bwhere%5D%5Bid%5D=1&itemsPerPage=8' => 'event',
        'api/public/events?_format=json&organizationId=1&page=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC' => 'event',
        'api/public/events?_format=json&organizationId=1&page=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&parent=1' => 'event',
        'api/public/events?_format=json&organizationId=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&children=1' => 'event',
        'api/public/donors?_format=json&organizationId=1&page=1&itemsPerPage=99' => 'donor',
        'api/public/donors?_format=json&organizationId=1&parent=1&page=1&itemsPerPage=99' => 'donor',
        'api/public/members?_format=json&filter%5Bwhere%5D%5BorganizationId%5D=1&itemsPerPage=20000' => 'member',
        'api/public/members_ca?_format=json&filter%5Bwhere%5D%5BorganizationId%5D=1&itemsPerPage=20000' => 'member'
    ];

    public function get($url): HtmlResponse
    {
        foreach ($this->map as $needle => $response_key) {
            if (str_ends_with($url, $needle))
            {
                return new HtmlResponse(
                    $this->responses[$response_key],
                    200,
                    []
                );
            }
        }
        throw new \RuntimeException("No fixture result for uri " . $url);
    }
}
