/*
 * F7cloud Talk - Android Client
 *
 * SPDX-FileCopyrightText: 2022 Andy Scherzinger <info@andy-scherzinger.de>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package ru.f7cloud.talk.remotefilebrowser.repositories

import ru.f7cloud.talk.filebrowser.webdav.ReadFolderListingOperation
import ru.f7cloud.talk.data.user.model.User
import ru.f7cloud.talk.remotefilebrowser.model.RemoteFileBrowserItem
import io.reactivex.Observable
import okhttp3.OkHttpClient
import javax.inject.Inject

class RemoteFileBrowserItemsRepositoryImpl @Inject constructor(private val okHttpClient: OkHttpClient) :
    RemoteFileBrowserItemsRepository {

    override fun listFolder(user: User, path: String): Observable<List<RemoteFileBrowserItem>> {
        return Observable.fromCallable {
            val operation =
                ReadFolderListingOperation(
                    okHttpClient,
                    user,
                    path,
                    1
                )
            val davResponse = operation.readRemotePath()
            if (davResponse.getData() != null) {
                return@fromCallable davResponse.getData() as List<RemoteFileBrowserItem>
            }
            return@fromCallable emptyList()
        }
    }
}
