package com.bc_chat.bc_base.task

import android.net.Uri
import com.bc_chat.bc_base.entity.dao.GroupMemberDao
import com.bc_chat.bc_base.entity.group.Group
import com.bc_chat.bc_base.http.Encryption
import com.bc_chat.bc_base.http.RetrofitHelper
import com.zhaohaoting.framework.abs.entity.ResultEntity
import com.zhaohaoting.framework.abs.task.RxDataAsyncTask
import com.zhaohaoting.framework.requset.rxjava_retrofit.RxResultSubscriber
import io.reactivex.Flowable
import io.reactivex.schedulers.Schedulers
import io.rong.imkit.RongIM

/**
 * author：Created by zhaohaoting on 2019/2/27
 * email：526309416@qq.com
 * desc：群信息
 */
class GroupInfoTask constructor(var groupId: String) : RxDataAsyncTask<Group, ResultEntity<Group>>() {
    override fun loadDataAsyncTask(): Flowable<ResultEntity<Group>> {
        val params: MutableMap<String, Any?> = mutableMapOf("groupId" to groupId)
        return RetrofitHelper.api.groupInfo(Encryption.encryption(params)).doOnNext {
            if (it.error == RxResultSubscriber.REQUEST_SUCCESS) {
                Schedulers.newThread().createWorker().schedule {
                    GroupMemberDao.instance.syncGroupInfo(groupId, it.data)
                    RongIM.getInstance().refreshGroupInfoCache(io.rong.imlib.model.Group(groupId, it.data.group_name, Uri.parse(it.data.photo)))
                }
            }
        }
    }
}
