package com.bc_chat.bc_base.task

import com.bc_chat.bc_base.R
import com.bc_chat.bc_base.entity.ListModel
import com.bc_chat.bc_base.entity.UserInfo
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.bc_chat.bc_base.utils.AppConfig
import com.bc_chat.bc_base.utils.Const
import com.zhaohaoting.framework.ZhtLibrary
import com.zhaohaoting.framework.abs.datasource.RxDataSource
import com.zhaohaoting.framework.abs.entity.ResultEntity
import io.reactivex.Flowable
import java.util.*

/**
 * author：Created by zhaohaoting on 2019/2/27
 * email：526309416@qq.com
 * desc：不是管理员
 */
class ChooseGroupFilterDataSource constructor(var groupId: String, var type: Int? = 0) : RxDataSource<UserInfo, ListModel<UserInfo>>() {
    //1  非管理员  2 转让群-- 排除自己   3. 踢人--排除权限大于等于自己的
    override fun loadListFromNetWork(params: HashMap<String, Any?>): Flowable<ResultEntity<ListModel<UserInfo>>>? {
        val params: MutableMap<String, Any?> = mutableMapOf("groupId" to groupId)
        val userId = AppConfig.instance.getUser().userId
        return RetrofitHelper.api.queryGroupMember(Encryption.encryption(params)).map {
            val resultEntity: ResultEntity<ListModel<UserInfo>> = ResultEntity()
            val listModel = ListModel<UserInfo>()
            resultEntity.data = listModel
            resultEntity.message = it.message
            resultEntity.error = it.error
            if (it.data == null) return@map resultEntity
            val mutableList = it.data.list
            val mineMember = findMemberById(userId, mutableList)
            it.data.list.forEach {
                if (type == 1 && Const.GROUP_ROLE.ROLE_GROUP_ORDINARY == it.type) { //当类型为1 是普通用户
                    resultEntity.data.list.add(it)
                } else if (type == 2 && !userId.equals(it.userId)) {//当类型为2 不是自己
                    resultEntity.data.list.add(it)
                } else if (type == 3 && it.userId != userId) {//当类型为3 不是自己
                    if (Const.GROUP_ROLE.ROLE_GROUP_LORDE == mineMember?.type) {//自己为群主
                        resultEntity.data.list.add(it)
                    } else if (Const.GROUP_ROLE.ROLE_GROUP_ADMIN == mineMember?.type && Const.GROUP_ROLE.ROLE_GROUP_ORDINARY == it?.type) {//自己为管理
                        resultEntity.data.list.add(it)
                    }
                } else if (type == 4 && !userId.equals(it.userId)) {
                    resultEntity.data.list.add(it)
                }
            }
            var indexList: ArrayList<String> = arrayListOf()
            listModel.list.sort()

            //按照字母排序，中文转换为拼音后按字母排序
            var oldName: String? = null
            for (j in listModel.list.indices) {
                val code = listModel.list[j]
                if (code.firstLetter != oldName) {
                    code.isTop = true
                    indexList.add(code.firstLetter)
                }
                oldName = code.firstLetter
            }
            listModel.index.addAll(indexList)
            if (type == 4&&Const.GROUP_ROLE.ROLE_GROUP_ORDINARY != mineMember?.type) {
                val allUserInfo = UserInfo("0")
                allUserInfo.photo = "android.resource://" + ZhtLibrary.getContext().packageName + "/" + R.mipmap.choose_all
                allUserInfo.nickname = "所有人"
                listModel.list.add(0, allUserInfo)
            }
            return@map resultEntity
        }
    }

    private fun findMemberById(userId: String, memberList: List<MemberBean>): MemberBean? {
        memberList.forEach {
            if (it.userId.equals(userId)) {
                return it
            }
        }
        return null
    }

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