package com.bc_chat.contacts.ui.search

import android.support.v7.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.amap.api.mapcore.util.it
import com.bc_chat.bc_base.ImService.ImRongService
import com.bc_chat.bc_base.config.RouteConfig
import com.bc_chat.bc_base.entity.UserInfo
import com.bc_chat.bc_base.entity.group.Group
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.contacts.adapter.SearchContactListAdapter
import com.bc_chat.contacts.adapter.SearchGroupListAdapter
import com.zhaohaoting.framework.abs.adapter.AbsRecyclerAdapter
import com.zhaohaoting.framework.ui.dialog.CustomDialog
import com.zhaohaoting.framework.utils.observer.Observer
import com.zhaohaoting.framework.utils.observer.ObserverManager
import io.rong.imkit.userInfoCache.RongUserInfoManager
import io.rong.imlib.RongIMClient
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.Message

/**
 * author：Created by zhaohaoting on 2019/3/15
 * email：526309416@qq.com
 * desc：群搜索
 */
@Route(path = RouteConfig.GROUP_SEARCH_ACTIVITY)
class GroupSearchActivity : SearchActivity(), AbsRecyclerAdapter.OnItemClickListener {

    private var allGroupList: ArrayList<Group>? = null
    private var adapterGroupList: ArrayList<Group>? = null

    override fun init() {
        allGroupList = intent.getParcelableArrayListExtra<Group>("groupList")
        adapterGroupList = arrayListOf()
    }

    override fun getAdapter(): RecyclerView.Adapter<*>? {
        val searchGroupListAdapter = SearchGroupListAdapter(this, adapterGroupList as List<Group>)
        searchGroupListAdapter.addOnItemClickListener(this)
        return searchGroupListAdapter
    }

    override fun query(query: String?) {
        adapterGroupList?.clear()
        if (!query.isNullOrEmpty()) {
            allGroupList?.forEach {
                if (it.group_name!!.contains(query!!) ) {
                    adapterGroupList?.add(it)
                }
            }
        }
        mAdapter?.notifyDataSetChanged()
    }

    override fun onItemClick(vh: AbsRecyclerAdapter.BaseViewHolder<*>?, position: Int) {
        val group = (mAdapter as SearchGroupListAdapter).data[position]
        ARouter.getInstance().navigation(ImRongService::class.java).startConversation(this, Conversation.ConversationType.GROUP, group.groupId!!, group.group_name!!, null)
    }
}