package kr.co.hanwha.hifive.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;

import java.util.ArrayList;

import kr.co.hanwha.hifive.adapter.contract.AddTaskSelectMemberListContract;
import kr.co.hanwha.hifive.adapter.viewholder.SelectMemberListViewHolder;
import kr.co.hanwha.hifive.data.model.MemberInfoData;

/**
 * Add TASK : 멤버 선택 리스트 어댑터
 *
 * @see kr.co.hanwha.hifive.screen.task.AddTaskSelectMemberActivity
 * <p>
 * Created by sanghoonkwon on 2018. 1. 18..
 */

public class AddTaskSelectMemberListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements AddTaskSelectMemberListContract.Model, AddTaskSelectMemberListContract.View,
        Filterable {
    private final int ZERO = 0;

    private AddTaskSelectMemberListContract.OnSelectMemberListListener mOnRequestListListener;

    private ArrayList<MemberInfoData> mListData;
    private ArrayList<MemberInfoData> mListSearchData;


    public AddTaskSelectMemberListAdapter(AddTaskSelectMemberListContract.OnSelectMemberListListener onRequestListListener) {
        mOnRequestListListener = onRequestListListener;
    }

    /**************************************************
     * ViewActivity
     **************************************************/
    @Override
    public void notifyDataAll() {
        this.notifyDataSetChanged();

    }

    @Override
    public void notifyDataRangeChanged(int positionStart, int itemCount) {
        this.notifyItemRangeChanged(positionStart, itemCount);
    }

    /**************************************************
     * Model
     **************************************************/
    @Override
    public void clearItems() {
        if (mListData != null) mListData.clear();
        if (mListSearchData != null) mListSearchData.clear();
    }

    @Override
    public void addItem(MemberInfoData memberInfoData, int index) {
        if (mListData != null && mListData.size() > index) {
            mListData.set(index, memberInfoData);
        }
    }

    @Override
    public void addItems(ArrayList<MemberInfoData> list) {
        if (mListData == null) mListData = new ArrayList<>();
        else mListData.clear();

        mListData.addAll(list);

        if (mListSearchData == null) mListSearchData = new ArrayList<>();
        else mListSearchData.clear();
        mListSearchData.addAll(mListData);
    }


    /**************************************************
     * Adapter
     **************************************************/

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SelectMemberListViewHolder(parent, mOnRequestListListener);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        int index = position;

        SelectMemberListViewHolder view = (SelectMemberListViewHolder) holder;
        view.onBind(mListSearchData.get(index), index);

    }

    @Override
    public int getItemCount() {
        return (mListSearchData != null ? mListSearchData.size() : ZERO);
    }

    /**************************************************
     * Filterable
     **************************************************/
    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();

                if (constraint == null || constraint.length() == 0) { // if your editText field is empty, return full list of FriendItem
                    results.count = mListData.size();
                    results.values = mListData;
                } else {
                    ArrayList<MemberInfoData> filteredList = new ArrayList<>();

                    constraint = constraint.toString().toLowerCase(); // if we ignore case
                    for (MemberInfoData item : mListData) {
                        if (item.getMember_name().contains(constraint.toString()) ||
                                item.getProject_name().contains(constraint.toString())) {
                            filteredList.add(item);
                        }
                    }

                    results.count = filteredList.size();
                    results.values = filteredList;
                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                mListSearchData = (ArrayList<MemberInfoData>) results.values;
                notifyDataSetChanged();
            }
        };
        return filter;
    }
}
