<?php

declare(strict_types=1);

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

namespace vossmedien\AloxBundle\API;

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

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

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

    protected $apiKey;

    protected $apiDomain;

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

        $this->apiKey = System::getContainer()->getParameter('vossmedien_alox.zvoove.api_key');
        $this->apiDomain = System::getContainer()->getParameter('vossmedien_alox.zvoove.api_domain');
    }

    protected function getApiKey()
    {
        return $this->apiKey;
    }

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

    protected function getAuthorizeRequestHeader()
    {
        $header = [
            'X-ApiKey' => $this->getApiKey()
        ];

        return $header;
    }

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

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

    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;
    }

}