package com.gh.gamecenter.forum.home.follow.model

import com.gh.common.util.PackageUtils
import com.gh.gamecenter.common.utils.toRequestBody
import com.gh.gamecenter.entity.FollowOperateTopRequest
import com.gh.gamecenter.forum.home.follow.AllFollowedNormalItem
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.retrofit.service.ApiService
import com.halo.assistant.HaloApp
import io.reactivex.Single
import okhttp3.ResponseBody

class AllFollowRepository(private val apiService: ApiService) {

    fun loadMyFollowedUser(pageNo: Int) =
        apiService.getMyFollowedUsers(
            UserManager.getInstance().userId,
            PackageUtils.getGhVersionName(),
            HaloApp.getInstance().channel,
            pageNo,
            true,
            mapOf()
        )
            .map { list ->
                list.map {
                    AllFollowedNormalItem(it, it.isTop)
                }
            }

    fun operateTop(tops: List<FollowOperateTopRequest>): Single<ResponseBody> {
        val body = mapOf("top" to tops).toRequestBody()
        return apiService.operateTop(
            UserManager.getInstance().userId,
            PackageUtils.getGhVersionName(),
            HaloApp.getInstance().channel,
            body
        )
    }

    fun postRead(type: String, typeId: String) =
        apiService.postRead(UserManager.getInstance().userId, type, typeId)
}