package com.renrenliao.im.model.repository

import com.renrenliao.im.bean.*
import com.renrenliao.im.config.Const
import com.renrenliao.im.ext.callRequest
import com.renrenliao.im.ext.handleResponse
import com.renrenliao.im.http.BaseResult
import com.renrenliao.im.http.RetrofitClient
import com.renrenliao.im.http.SealedResult
import com.renrenliao.im.im.GroupEntity
import com.renrenliao.im.model.api.ApiService
import com.renrenliao.im.utils.MapUtils
import retrofit2.http.Body

/**
 **@ClassName RemoteDataSource
 **@Description
 **@Author ganzhe
 **@Date 2022/5/15 14:50
 **/
class RemoteDataSource {

    companion object{
        var apiService = RetrofitClient.createApi(ApiService::class.java, Const.BASE_URL)
    }

    //获取线路配置
    suspend fun getLines(): LineEntity {
        return apiService.getLines(Const.SERVER_LINES)
    }


    //版本配置
    private suspend fun reqAppConfig(map: Map<String,String>) =
        handleResponse(apiService.appConfig(map))
    suspend fun getAppConfig(map: Map<String,String>): SealedResult<AppConfigEntity> =
        callRequest { reqAppConfig(map) }
    

    //登陆
    private suspend fun reqLogin(map: Map<String,String>) =
        handleResponse(apiService.login(map))
    suspend fun login(map: Map<String,String>): SealedResult<AuthInfo> =
        callRequest { reqLogin(map) }


    //获取广告
    suspend fun getAd(map: Map<String,String>): BaseResult<Any> {
        return  apiService.getAd(map)
    }
    //银行卡
    suspend fun getBankCardList(map: Map<String, String>):BaseResult<List<BankCardEntity>>{
        val body = MapUtils.map2JsonRequestBody(map)
        return apiService.getBankCardList(map)
    }
    //获取广告  异步用的 单独判断异常
    private suspend fun reqAds(map: Map<String,String>) =
        handleResponse(apiService.getAd(map))
    suspend fun getAds(map: Map<String,String>): SealedResult<Any> =
        callRequest { reqAds(map) }
    //银行卡  异步用的 单独判断异常
    private suspend fun reqBankCard(map: Map<String,String>) =
        handleResponse(apiService.getBankCardList(map))
    suspend fun getBankCards(map: Map<String,String>): SealedResult<List<BankCardEntity>> =
        callRequest { reqBankCard(map) }

    //获取在线好友
    suspend fun getFriendList(map: Map<String, String>):SealedResult<List<UserInfo>> =
        callRequest { reqFriendList(map) }
    private suspend fun reqFriendList(map: Map<String, String>) =
        handleResponse(apiService.getFriendList(map))

    //获取群主列表
    suspend fun getGroupList(map: Map<String, String>):SealedResult<List<GroupEntity>> =
        callRequest { reqGroupList(map) }
    private suspend fun reqGroupList(map: Map<String, String>) =
        handleResponse(apiService.getGroupList(map))

    //退出登陆
    suspend fun logout(map: Map<String, String>):BaseResult<Any>{
        return apiService.logOut(map)
    }

    //更新用户信息
    suspend fun updateUserInfo(map: Map<String, String>):BaseResult<UserInfo>{
        return apiService.updateUserInfo(map)
    }

    //创建群组
    suspend fun createGroup(map: Map<String, String>):BaseResult<GroupEntity>{
        return apiService.createGroup(map)
    }

    //查询好友
    suspend fun friendFind(map: HashMap<String, String>):BaseResult<UserInfo>{
        val map = MapUtils.getComParam(map)
        return apiService.friendFind(map)
    }

    //删除好友
    suspend fun deleteFriend(map: Map<String, String>):BaseResult<Any>{
        return apiService.deleteFriend(map)
    }

    //添加好友
    suspend fun addFriend(map: Map<String, String>):BaseResult<Any>{
        return apiService.addFriend(map)
    }
    //拒绝添加好友
    suspend fun rejectAddFriend(map: Map<String, String>):BaseResult<Any>{
        return apiService.rejectAddFriend(map)
    }

    //备注好友
    suspend fun friendRemark(map: Map<String, String>):BaseResult<Any>{
        return apiService.friendRemark(map)
    }

    //朋友圈数据
    suspend fun getFriendCircle( map: Map<String, String>):BaseResult<Any>{
        return apiService.getFriendCircle(map)
    }

    //获取未读加好友请求数
    suspend fun getOfflineFriend():SealedResult<List<UserInfo>> =
        callRequest { reqOfflineFriend(MapUtils.getComParam(HashMap())) }
    private suspend fun reqOfflineFriend(map: Map<String, String>) =
        handleResponse(apiService.getOfflineFriend(map))

    //同意添加好友
    suspend fun agreeAddFriend(map: Map<String, String>):BaseResult<UserInfo>{
        return apiService.agreeAddFriend(map)
    }

}