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 java.util.Collections;
import java.util.Comparator;

import kr.co.hanwha.hifive.adapter.contract.AddTaskSelectCampaignListContract;
import kr.co.hanwha.hifive.adapter.model.OkrListViewHolderData;
import kr.co.hanwha.hifive.adapter.viewholder.SelectCampaignListKeyResultViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.SelectCampaignListObjectViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.SelectCampaignListUnitViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.SelectCampaignListViewHolder;
import kr.co.hanwha.hifive.data.model.CampaignData;
import kr.co.hanwha.hifive.data.model.CampaignTreeData;
import kr.co.hanwha.hifive.data.model.MemberCampaignsTree;
import kr.co.hanwha.hifive.data.model.MemberInfoData;
import kr.co.hanwha.hifive.data.model.ProjectCampaignData;
import kr.co.hanwha.hifive.data.model.ProjectCampaignsTree;
import kr.co.hanwha.hifive.enums.ViewHolderOkrType;

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

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

    private AddTaskSelectCampaignListContract.OnSelectCampaignListListener mOnSelectCampaignListListener;

    private ArrayList<OkrListViewHolderData> mListData = new ArrayList<>();
    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<CampaignTreeData> okrListData) {
        if (mListData == null) mListData = new ArrayList<>();
        else mListData.clear();

        if (okrListData == null) return;

        addProjectItem(okrListData);

//        Collections.sort(list, sortProject);
//
//        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);
    }

    private void addProjectItem(ArrayList<CampaignTreeData> okrListData) {
        if (okrListData != null && okrListData.size() > 0) {
            for (CampaignTreeData treeData : okrListData) {
                mProjectId = treeData.getProject_id();
                mProjectName = treeData.getProject_name();
                OkrListViewHolderData viewHolderData = new OkrListViewHolderData(
                        treeData.getProject_id(),
                        treeData.getProject_name(),
                        null,
                        ViewHolderOkrType.UNIT
                );
                mListData.add(viewHolderData);
                addProjectCampaignItem(treeData.getListProjectCampaignsTree());
            }
        }
    }

    private void addProjectCampaignItem(ArrayList<ProjectCampaignsTree> projectCampaignsListData) {
        if (projectCampaignsListData != null && projectCampaignsListData.size() > 0) {
            for (ProjectCampaignsTree projectCampaignData : projectCampaignsListData) {
                OkrListViewHolderData viewHolderData = new OkrListViewHolderData(
                        projectCampaignData.getProject_campaign_id(),
                        null,
                        projectCampaignData.getProject_campaign_desc(),
                        ViewHolderOkrType.OBJECT
                );
                mListData.add(viewHolderData);
                addMemberCampaignItem(projectCampaignData.getListMemberCampaignsTree());
            }
        }
    }

    private void addMemberCampaignItem(ArrayList<MemberCampaignsTree> memberCampaignsListData) {
        if (memberCampaignsListData != null && memberCampaignsListData.size() > 0) {
            for (int i = 0; i < memberCampaignsListData.size(); i++) {
                MemberCampaignsTree memberCampaignData = memberCampaignsListData.get(i);
                OkrListViewHolderData viewHolderData = new OkrListViewHolderData(
                        memberCampaignData.getMember_campaign_id(),
                        null,
                        memberCampaignData.getMember_campaign_desc(),
                        ViewHolderOkrType.KEY_RESULT
                );
                if (i == memberCampaignsListData.size() - 1) {
                    viewHolderData.setLast(true);
                }
                viewHolderData.setProjectId(mProjectId);
                viewHolderData.setProjectName(mProjectName);
                mListData.add(viewHolderData);
            }
        }
    }

    private Comparator<ProjectCampaignData> sortProject = new Comparator<ProjectCampaignData>() {
        @Override
        public int compare(ProjectCampaignData o1, ProjectCampaignData o2) {
            return Long.compare(o1.getProject_id(), o2.getProject_id());
        }
    };

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

    @Override
    public int getItemViewType(int position) {
        switch (mListData.get(position).getType()) {
            case UNIT:
                return ViewHolderOkrType.UNIT.getCode();
            case OBJECT:
                return ViewHolderOkrType.OBJECT.getCode();
            case KEY_RESULT:
                return ViewHolderOkrType.KEY_RESULT.getCode();
        }
        return 0;
    }

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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ViewHolderOkrType.UNIT.getCode()) {
            return new SelectCampaignListUnitViewHolder(parent);
        } else if (viewType == ViewHolderOkrType.OBJECT.getCode()) {
            return new SelectCampaignListObjectViewHolder(parent);
        } else if (viewType == ViewHolderOkrType.KEY_RESULT.getCode()) {
            return new SelectCampaignListKeyResultViewHolder(parent, mOnSelectCampaignListListener);
        } /*else if (viewType == ViewHolderOkrType.FOOTER_MARGIN.getCode()) {
            return new MarginViewHolder(parent);
        }*/

        throw new RuntimeException("############################################################\n" +
                "There is no type that matches the type " + viewType + " + make sure your using types correctly\n" +
                "############################################################");
//        return new SelectCampaignListViewHolder(parent, mOnSelectCampaignListListener);
    }

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

        if (holder instanceof SelectCampaignListUnitViewHolder) {
            SelectCampaignListUnitViewHolder view = (SelectCampaignListUnitViewHolder) holder;
            view.onBind(mListData.get(position), position);
        } else if (holder instanceof SelectCampaignListObjectViewHolder) {
            SelectCampaignListObjectViewHolder view = (SelectCampaignListObjectViewHolder) holder;
            view.onBind(mListData.get(position), position);
        } else if (holder instanceof SelectCampaignListKeyResultViewHolder) {
            SelectCampaignListKeyResultViewHolder view = (SelectCampaignListKeyResultViewHolder) holder;
            view.setSelectedCampaignKey(mSelectedCampaignKey);
            view.onBind(mListData.get(position), position);
        } /*else if (holder instanceof MarginViewHolder) {
            MarginViewHolder view = (MarginViewHolder) holder;
            view.onBind(null, (int) HFCommonUtil.getPixelFromDp(mContext, FOOTER_HEIGHT));
        }*/

//        int index = position;
//
//        SelectCampaignListViewHolder view = (SelectCampaignListViewHolder) holder;
//        view.setSearchWord(mSearchWord);
//        view.setSelectedCampaignKey(mSelectedCampaignKey);
//        view.onBind(mListSearchData.get(index), index);

    }

    @Override
    public int getItemCount() {
        return mListData.size();
//        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;
    }*/
}
