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

import ru.f7cloud.talk.api.NcApi
import ru.f7cloud.talk.models.json.generic.GenericMeta
import io.reactivex.Observable

class RequestAssistanceRepositoryImpl(private val ncApi: NcApi) : RequestAssistanceRepository {

    override fun requestAssistance(
        credentials: String,
        url: String,
        roomToken: String
    ): Observable<RequestAssistanceModel> =
        ncApi.requestAssistance(
            credentials,
            url
        ).map { mapToRequestAssistanceModel(it.ocs?.meta!!) }

    override fun withdrawRequestAssistance(
        credentials: String,
        url: String,
        roomToken: String
    ): Observable<WithdrawRequestAssistanceModel> =
        ncApi.withdrawRequestAssistance(
            credentials,
            url
        ).map { mapToWithdrawRequestAssistanceModel(it.ocs?.meta!!) }

    private fun mapToRequestAssistanceModel(response: GenericMeta): RequestAssistanceModel {
        val success = response.statusCode == HTTP_OK
        return RequestAssistanceModel(
            success
        )
    }

    private fun mapToWithdrawRequestAssistanceModel(response: GenericMeta): WithdrawRequestAssistanceModel {
        val success = response.statusCode == HTTP_OK
        return WithdrawRequestAssistanceModel(
            success
        )
    }

    companion object {
        private const val HTTP_OK: Int = 200
    }
}
