<?php

class Dalten_Export_Api_NewRealitymix extends Dalten_Export_Api_SrealityLike
{
    protected function _convertUserData(array $userData)
    {
        $converted = parent::_convertUserData($userData);
        $converted['client_login'] = $userData['sreality_like_login'];

        if (!empty($userData['sekundarni_email'])) {
            // tohle jde na RealityMixu do soukromého e-mailu makléře
            $converted['client_login'] = $userData['sekundarni_email'];
            // a contact_email je email
        }

        if (!empty($userData['datova_schranka_id'])) {
            $converted['client_data_box'] = $userData['datova_schranka_id'];
        }

        return $converted;
    }

    protected function _convertListingData(array $listingData, array $additionalParams)
    {
        $originalListingData = $listingData;
        $listingData = parent::_convertListingData($listingData, $additionalParams);

        $hidePortals = '00';
        if (isset($additionalParams['listing']['hide_portals'])) {
            // data dostáváme v podobě pole integerů, když v něm je integer s danou hodnotou pošleme ho
            // 1=Prima HbbTV
            $hidePortals[1] = in_array(1, $additionalParams['listing']['hide_portals']) ? 1 : 0;
        }
        $listingData['hide_portals'] = $hidePortals;

        // z původních (nepřevedených) dat vytáhneme video
        $youtubeIdVideo = Dalten_Export_Helper_YoutubeIdExtractor::extractYoutubeId($originalListingData['nemovitost_video_prohlidka']);
        if ($youtubeIdVideo) {
            $listingData['video_url'] = $originalListingData['nemovitost_video_prohlidka'];
        }

        $youtubeIdVirtualniProhlidka = Dalten_Export_Helper_YoutubeIdExtractor::extractYoutubeId($originalListingData['nemovitost_virtualni_prohlidka']);
        if ($youtubeIdVirtualniProhlidka) {
            $listingData['video_url'] = $originalListingData['nemovitost_virtualni_prohlidka'];
        }

        if (isset($originalListingData['videos'])) {
            $firstVideo = $originalListingData['videos'][0];
            $listingData['video_url_fullhd'] = $firstVideo['url_fullhd'] ?? null;
            $listingData['video_url_hd'] = $firstVideo['url_hd'] ?? null;
            $listingData['video_url_sd'] = $firstVideo['url_sd'] ?? null;
            unset($listingData['videos']);
        }

        return $listingData;
    }

    protected function getProjectUrl($remoteId)
    {
        return sprintf('https://realitymix.cz/detail-projektu/lokalita/nahled-%d.html', $remoteId);
    }


    /**
     * {@inheritDoc}
     */
    public function topListing(array $listingData, array $additionalParams = [])
    {
        $response = $this->_backend->topAdvert(
            $this->_getSessionId(),
            null,
            isset($listingData['kod']) ? $listingData['kod'] : null
        );

        return new Dalten_Export_Api_ServerResponse(
            $response->status == 200,
            $response->status,
            $response->statusMessage,
            $response->output
        );
    }
}
