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

import com.gh.common.util.PackageUtils
import com.gh.gamecenter.forum.home.follow.FollowDynamicBbsItem
import com.gh.gamecenter.forum.home.follow.FollowDynamicPersonalItem
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.retrofit.RetrofitManager
import com.gh.gamecenter.retrofit.service.ApiService
import com.halo.assistant.HaloApp

class FollowDynamicRepository(
    private val apiService: ApiService
) {

    fun loadData(userId: String, bbsId: String, pageNo: Int) =
        if (userId.isNotBlank()) {
            loadPersonDynamic(userId, pageNo)
        } else {
            loadBbsDynamic(bbsId, pageNo)
        }

    private fun loadPersonDynamic(userId: String, pageNo: Int) =
        apiService.getPersonalHistory(
            userId,
            pageNo,
            HaloApp.getInstance().channel,
            PERSON_DYNAMIC_FILTER
        ).map {
            it.map(::FollowDynamicPersonalItem)
        }

    private fun loadBbsDynamic(bbsId: String, pageNo: Int) =
        apiService.getAllForumList(bbsId, BBS_DYNAMIC_SORT, pageNo, mapOf())
            .map {
                it.map(::FollowDynamicBbsItem)
            }

    companion object {

        private const val PERSON_DYNAMIC_FILTER = "scene:follow_personal,type:all"
        private const val BBS_DYNAMIC_SORT = "time.reply:-1&type=community_article|question|video"

        fun newInstance(): FollowDynamicRepository = FollowDynamicRepository(
            RetrofitManager.getInstance().api
        )
    }
}