package io.rong.contactcard.choose;

import android.text.TextUtils;
import android.view.View;

import com.bc_chat.bc_base.entity.ListModel;
import com.bc_chat.bc_base.entity.UserInfo;
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;

import java.util.ArrayList;
import java.util.List;

import io.rong.contactcard.adapter.SelectContactListAdapter;
import com.bc_chat.bc_base.task.ChooseContactSelfDataSource;

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

    private List<UserInfo> allList = new ArrayList<>();

    private List<UserInfo> selectList = new ArrayList<>();

    private List<UserInfo> adapterList = new ArrayList<>();

    @Override
    public void onEndRefresh(IDataAdapter<ListModel<UserInfo>> adapter, ListModel<UserInfo> result) {
        if (result != null && result.getList() != null) {
            allList.addAll(result.getList());
            adapterList.addAll(result.getList());
            indexBar.setLetters(result.getIndex());
        }
    }

    @Override
    public AbsRecyclerAdapter<UserInfo, ListModel<UserInfo>> getAdapter() {
        return new SelectContactListAdapter(activity);
    }

    @Override
    public IAsyncDataSource<ListModel<UserInfo>> getDataSource() {
        return new ChooseContactSelfDataSource();
    }

    @Override
    public void onInit() {
        mvcHelper.setAutoLoadMore(false);
        funnyRefreshView.setEnabled(false);
        setOnStateChangeListener(this);
    }

    @Override
    public List<UserInfo> getListData() {
        return allList;
    }

    @Override
    public MVCHelper<ListModel<UserInfo>> getMvcHelper(CoolRefreshView funnyRefreshView) {
        MVCHelper mvcCoolHelper = new MVCCoolHelper<ListModel<UserInfo>>(funnyRefreshView);
        mvcCoolHelper.setmLoadMoreView(null);
        return mvcCoolHelper;
    }

    public void filter(CharSequence key) {
        adapterList.clear();
        if (!TextUtils.isEmpty(key)) {
            indexBar.setVisibility(View.GONE);
            for (UserInfo userInfo : allList) {
                String name = userInfo.getRemark() == null ? userInfo.getNickname() : userInfo.getRemark();
                if (name.contains(key)) {
                    if (selectList.contains(userInfo)) {
                        userInfo.setSelect(true);
                    }
                    adapterList.add(userInfo);
                }
            }
            ((SelectContactListAdapter)adapter).setShowTitle(false);
        } else {
            adapterList.addAll(allList);
            indexBar.setVisibility(View.VISIBLE);
            ((SelectContactListAdapter)adapter).setShowTitle(true);
        }
        adapter.notifyListDataChanged(adapterList, true);
    }

    /**
     * 点击事件 如果是单选 点击走回传值流程
     * 多选 点击走选中流程
     */
    @Override
    public void itemClicked(AbsRecyclerAdapter.BaseViewHolder vh, int position) {
        UserInfo contacts = adapter.getData().getList().get(position);
        onSelectListener.onItemSelect(contacts,adapter.getData().getList().size());
//        } else{
//            if (contacts.isSelect) {
//                selectList.add(contacts)
//            } else {
//                if (contacts in selectList){
//                    selectList.remove(contacts)
//                }
//            }
//            onSelectListener ?.onItemSelectList(selectList)
//        }
    }

    public void setSelectListener(OnSelectListener selectListener) {
        this.onSelectListener = selectListener;
    }

    private OnSelectListener onSelectListener;

    interface OnSelectListener {
        void onItemSelect(UserInfo contacts, int size);

        void onItemSelectList(ArrayList<UserInfo> contactList);
    }
}

