<?php

declare(strict_types=1);

/*
 * This file is part of dacore bundle for Contao.
 *
 * (c) Benjamin Roth
 *
 * @license commercial
 */

namespace vossmedien\DacoreBundle\API;

use Contao\System;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use vossmedien\DacoreBundle\Model\KatalogModel;
use vossmedien\DacoreBundle\Model\StelleModel;
use vossmedien\DacoreBundle\Model\StellenModel;

class Softgarden
{
    /**
     * @var SerializerInterface
     */
    protected $serializer;

    /**
     * @var HttpClientInterface
     */
    protected $httpClient;

    protected $clientId;

    protected $apiDomain;

    public function __construct(SerializerInterface $serializer, HttpClientInterface $httpClient)
    {
        $this->serializer = $serializer;
        $this->httpClient = $httpClient;

        $this->clientId = System::getContainer()->getParameter('vossmedien_dacore.softgarden.client_id');
        $this->apiDomain = System::getContainer()->getParameter('vossmedien_dacore.softgarden.api_domain');
    }

    protected function getClientId()
    {
        return $this->clientId;
    }

    protected function getApiDomain()
    {
        return rtrim($this->apiDomain,'/');
    }

    protected function getBasicAuthorization()
    {
        return [$this->getClientId(),''];
    }

    protected function sendRequest(string $relEndpoint, array $options, string $method = 'GET')
    {
        $relEndpoint = '/' . ltrim($relEndpoint,'/');

        return $this->httpClient->request($method,$this->getApiDomain().$relEndpoint,$options);
    }

    public function testApiRequest()
    {
        $options = [
            'auth_basic' => $this->getBasicAuthorization(),
            'headers' => ['Content-Type: application/json']
        ];

        $response = $this->sendRequest('jobslist/102581_extern',$options);

        if ($response->getStatusCode() == 200)
        {
            $content = $response->getContent();

            return json_decode($content);
        }

        return null;
    }

    public function getStellenFiltered(array $params=[]): ?StellenModel
    {
        $options = [
            'headers' => $this->getAuthorizeRequestHeader()
        ];

        if (count($params))
        {
            $options['query'] = $params;
        }

        $response = $this->sendRequest('Stelle/GetStellenFiltered',$options);



        if ($response->getStatusCode() == 200)
        {
            $content = $response->getContent();

            /** @var StellenModel $collection */
            $collection = $this->serializer->deserialize($content,StellenModel::class,'json');

            return $collection;
        }

        return null;
    }

    public function getStelleById(string $uuid, array $params=[]): ?StelleModel
    {
        $options = [
            'headers' => $this->getAuthorizeRequestHeader(),
            'query' => [
                'stelleUuid' => $uuid
            ]
        ];

        if (count($params))
        {
            $options['query'] = array_merge($options['query'],$params);
        }

        $response = $this->sendRequest('Stelle/GetStelleById',$options);



        if ($response->getStatusCode() == 200)
        {
            $content = $response->getContent();

            /** @var StelleModel $model */
            $model = $this->serializer->deserialize($content,StelleModel::class,'json');

            return $model;
        }

        return null;
    }

    public function getKatalogByRelationName(string $entityName, string $relationName): ?KatalogModel
    {
        $options = [
            'headers' => $this->getAuthorizeRequestHeader(),
            'query' => [
                'entityName' => $entityName,
                'relationName' => $relationName
            ]
        ];

        $response = $this->sendRequest('Katalog/GetByRelationName',$options);



        if ($response->getStatusCode() == 200)
        {
            $content = $response->getContent();

            /** @var KatalogModel $collection */
            $collection = $this->serializer->deserialize($content,KatalogModel::class,'json');

            return $collection;
        }

        return null;
    }

}