package com.bc_chat.contacts.ui.choose

import com.bc_chat.bc_base.entity.ListModel
import com.bc_chat.bc_base.entity.UserInfo
import com.bc_chat.bc_base.task.ChooseContactDataSource
import com.bc_chat.bc_base.task.ChooseGroupFilterDataSource
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.bc_base.utils.gone
import com.bc_chat.bc_base.utils.visible
import com.bc_chat.contacts.adapter.SelectContactListAdapter
import com.bc_chat.contacts.dataSource.GroupInviteDataSource
import com.shizhefei.view.coolrefreshview.CoolRefreshView
import com.zhaohaoting.framework.abs.BaseIndexBarFragment
import com.zhaohaoting.framework.abs.adapter.AbsRecyclerAdapter
import com.zhaohaoting.framework.mvchelper.mvc.IAsyncDataSource
import com.zhaohaoting.framework.mvchelper.mvc.IDataAdapter
import com.zhaohaoting.framework.mvchelper.mvc.MVCHelper
import com.zhaohaoting.framework.mvchelper.mvc.OnRefreshEndListener
import com.zhaohaoting.framework.ui.helper.MVCCoolHelper

/**
 * author：Created by zhaohaoting on 2019/2/28
 * email：526309416@qq.com
 * desc：选择好友
 */
class ChooseFriendFragment : BaseIndexBarFragment<UserInfo, ListModel<UserInfo>>(), OnRefreshEndListener<ListModel<UserInfo>> {


    companion object {
        const val IS_SING_SELECT = "isSingSelect"
    }

    var groupId: String? = null
    var action: String? = null


    private val allList: ArrayList<UserInfo> = ArrayList()

    private val selectList: ArrayList<UserInfo> = ArrayList()

    private val adapterList: ArrayList<UserInfo> = ArrayList()

    private var singSelect: Boolean? = false

    override fun onEndRefresh(adapter: IDataAdapter<ListModel<UserInfo>>?, result: ListModel<UserInfo>?) {
        if (result != null && result.list != null) {
            allList.addAll(result.list)
            adapterList.addAll(result.list)
            indexBar.setLetters(result?.index)
        }
    }

    override fun getAdapter(): AbsRecyclerAdapter<UserInfo, ListModel<UserInfo>> = SelectContactListAdapter(activity, singSelect)
    override fun getDataSource(): IAsyncDataSource<ListModel<UserInfo>> {
        when (action) {
            Const.Action.CREATE_GROUP -> {
                return ChooseContactDataSource()
            }
            Const.Action.INVITE_GROUP_MEMBER -> {
                return GroupInviteDataSource(groupId!!)
            }
            Const.Action.KICK_GROUP_MEMBER -> {
                return ChooseGroupFilterDataSource(groupId!!, 3)
            }
            Const.Action.GROUP_ASSIGNMENT -> {
                return ChooseGroupFilterDataSource(groupId!!, 2)
            }
            Const.Action.SET_GROUP_ADMIN -> {
                return ChooseGroupFilterDataSource(groupId!!, 1)
            }
            Const.Action.ALTER_GROUP_MEMBER->{
                return ChooseGroupFilterDataSource(groupId!!, 4)
            }

            else -> {
                return ChooseContactDataSource()
            }
        }
    }


    override fun onInit() {
        funnyRefreshView.isEnabled = false
        singSelect = arguments?.getBoolean(IS_SING_SELECT, false)
        setOnStateChangeListener(this)
        groupId = arguments?.getString("groupId")
        action = arguments?.getString("action")
    }

    override fun getListData(): List<UserInfo>? {
        return allList
    }

    override fun getMvcHelper(funnyRefreshView: CoolRefreshView?): MVCHelper<ListModel<UserInfo>> {
        val mvcCoolHelper = MVCCoolHelper<ListModel<UserInfo>>(funnyRefreshView)
        mvcCoolHelper.setmLoadMoreView(null)
        return mvcCoolHelper
    }

    fun filter(key: CharSequence?) {
        adapterList.clear()
        if (!key.isNullOrEmpty()) {
            indexBar.gone()
            allList.forEach {
                val name = it.remark ?: it.nickname
                if (name!!.contains(key!!)) {
                    if (selectList.contains(it)) {
                        it.isSelect = true
                    }
                    adapterList.add(it)
                }
            }
            (adapter as SelectContactListAdapter).isShowTitle = false
        } else {
            adapterList.addAll(allList)
            indexBar.visible()
            (adapter as SelectContactListAdapter).isShowTitle = true
        }
        adapter.notifyListDataChanged(adapterList, true)
    }

    /**
     * 点击事件 如果是单选 点击走回传值流程
     * 多选 点击走选中流程
     */
    override fun itemClicked(vh: AbsRecyclerAdapter.BaseViewHolder<*>?, position: Int) {
        val contacts = adapter.data.list[position]
        if (singSelect!!) {
            onSelectListener?.onItemSelect(contacts)
        } else {
            if (contacts.isSelect) {
                selectList.add(contacts)
            } else {
                if (contacts in selectList) {
                    selectList.remove(contacts)
                }
            }
            onSelectListener?.onItemSelectList(selectList)
        }
    }

    fun setSelectListener(selectListener: OnSelectListener) {
        this.onSelectListener = selectListener
    }

    private var onSelectListener: OnSelectListener? = null

    interface OnSelectListener {
        fun onItemSelect(contacts: UserInfo)
        fun onItemSelectList(contactList: ArrayList<UserInfo>)
    }
}

