/*
 * F7cloud Talk - Android Client
 *
 * SPDX-FileCopyrightText: 2023 Marcel Hibbe <dev@mhibbe.de>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package ru.f7cloud.talk.conversationlist.data.network

import ru.f7cloud.talk.api.NcApi
import ru.f7cloud.talk.data.user.model.User
import ru.f7cloud.talk.models.json.conversations.Conversation
import ru.f7cloud.talk.utils.ApiUtils
import io.reactivex.Observable

class RetrofitConversationsNetwork(private val ncApi: NcApi) : ConversationsNetworkDataSource {
    override fun getRooms(user: User, url: String, includeStatus: Boolean): Observable<List<Conversation>> {
        val credentials: String = ApiUtils.getCredentials(user.username, user.token)!!
        val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V3, 1))

        return ncApi.getRooms(
            credentials,
            ApiUtils.getUrlForRooms(apiVersion, user.baseUrl!!),
            includeStatus
        ).map { it ->
            it.ocs?.data?.map { it } ?: listOf()
        }
    }
}
