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.AddTaskSelectCampaignListContract;
import kr.co.hanwha.hifive.adapter.viewholder.SelectCampaignListViewHolder;
import kr.co.hanwha.hifive.data.model.CampaignData;
import kr.co.hanwha.hifive.data.model.ProjectCampaignData;

/**
 * Created by sanghoonkwon on 2018. 2. 21..
 */

public class AddTaskSelectCampaignListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements AddTaskSelectCampaignListContract.Model, AddTaskSelectCampaignListContract.View,
        Filterable {
    private final int ZERO = 0;
    private String mSearchWord = "";
    private String mSelectedCampaignKey = "";

    private AddTaskSelectCampaignListContract.OnSelectCampaignListListener mOnSelectCampaignListListener;

    private ArrayList<ProjectCampaignData> mListData;
    private ArrayList<CampaignData> mListSearchData;

    public AddTaskSelectCampaignListAdapter(AddTaskSelectCampaignListContract.OnSelectCampaignListListener onSelectCampaignListListener) {
        mOnSelectCampaignListListener = onSelectCampaignListListener;
    }

    /**************************************************
     * 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(ProjectCampaignData data, int index) {
        if (mListData != null && mListData.size() > index) {
            mListData.set(index, data);
        }
    }

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

        if (list == null) return;

        int lastProjectId = 0;
        for (int i = 0; i < list.size(); i++) {
            ProjectCampaignData data = list.get(i);
            data.setFirstItem(lastProjectId != data.getProject_id());
            mListData.add(data);
            lastProjectId = data.getProject_id();
        }
//        mListData.addAll(list);

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

    @Override
    public void setSelectedCampaignKey(String selectedCampaignKey) {
        this.mSelectedCampaignKey = selectedCampaignKey;
    }

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

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

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

        int index = position;

        SelectCampaignListViewHolder view = (SelectCampaignListViewHolder) holder;
        view.setSearchWord(mSearchWord);
        view.setSelectedCampaignKey(mSelectedCampaignKey);
        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;
                    mSearchWord = "";
                } else {
                    mSearchWord = constraint.toString();
                    ArrayList<CampaignData> filteredList = new ArrayList<>();

                    int lastProjectId = 0;
                    constraint = constraint.toString().toLowerCase(); // if we ignore case
                    for (ProjectCampaignData item : mListData) {
                        if (item.getCampaign_desc().contains(constraint.toString()) ||
                                item.getProject_name().contains(constraint.toString())) {

                            boolean isFirstItem = lastProjectId != item.getProject_id();
                            filteredList.add(new CampaignData(item.getCampaign_id(), item.getCampaign_desc(), item.getCampaign_due_to(), item.getProject_id(), item.getProject_name(),
                                    isFirstItem));

                            lastProjectId = item.getProject_id();
                        }
                    }

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

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

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