package com.bc_chat.bc_base.task

import com.bc_chat.bc_base.entity.ListModel
import com.bc_chat.bc_base.entity.dao.GroupMemberDao
import com.bc_chat.bc_base.entity.group.MemberBean
import com.bc_chat.bc_base.http.Encryption
import com.bc_chat.bc_base.http.RetrofitHelper
import com.zhaohaoting.framework.abs.datasource.RxDataSource
import com.zhaohaoting.framework.abs.entity.ResultEntity
import com.zhaohaoting.framework.requset.rxjava_retrofit.RxResultSubscriber
import io.reactivex.Flowable
import io.reactivex.schedulers.Schedulers
import java.util.*

/**
 * author：Created by zhaohaoting on 2019/2/27
 * email：526309416@qq.com
 * desc：群用户
 */
class GroupMemberDataSource constructor(var groupId: String) : RxDataSource<MemberBean, ListModel<MemberBean>>() {

    override fun loadListFromNetWork(params: HashMap<String, Any>): Flowable<ResultEntity<ListModel<MemberBean>>>? {
        val params: MutableMap<String, Any?> = mutableMapOf("groupId" to groupId)
        return RetrofitHelper.api.queryGroupMember(Encryption.encryption(params)).doOnNext {
            if (it.error == RxResultSubscriber.REQUEST_SUCCESS) {
                val list: List<MemberBean> = it.data.list
                Collections.sort(list) { o1, o2 ->
                    // 按权限
                    when {
                        o1?.type!! > o2?.type!! -> 1
                        o1.type!! == o2.type!! -> 0
                        else -> -1
                    }
                }
                Schedulers.newThread().createWorker().schedule {
                    GroupMemberDao.instance.syncGroupMemberInfo(groupId, it.data.list)
                }
            }
        }
    }

    override fun hasMore(): Boolean {
        return false
    }
}
