package kr.co.hanwha.hifive.admin.admin.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
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.R;
import kr.co.hanwha.hifive.adapter.viewholder.MainEmptyListViewHolder;
import kr.co.hanwha.hifive.admin.admin.adapter.viewholder.AdminMemberListViewHolder;
import kr.co.hanwha.hifive.admin.network.model.AdminMemberHrInfo;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * Created by sanghoonkwon on 2018. 3. 9..
 */

public class AdminMemberListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements Filterable {
    private Context mContext;

    private final int ZERO = 0;
    private final int COUNT_EMPTY = 1;

    private final int TYPE_EMPTY = 0;
    private final int TYPE_ALARM = 1;

    private OnAdminMemberListListener mOnAdminMemberListListener;

    private ArrayList<AdminMemberHrInfo> mListData;
    private ArrayList<AdminMemberHrInfo> mListSearchData;
    private String mSearchWord = "";
    private boolean isOnlyActiveMember;

    public AdminMemberListAdapter(@NonNull Context context,
                                  @NonNull OnAdminMemberListListener onAdminMemberListListener) {
        mContext = context;
        mOnAdminMemberListListener = onAdminMemberListListener;
    }

    public void setOnlyActiveMember(boolean onlyActiveMember) {
        isOnlyActiveMember = onlyActiveMember;
    }

    /**************************************************
     * View
     **************************************************/
    @Override
    public int getItemViewType(int position) {
        if (isEmpty())
            return TYPE_EMPTY;
        else return TYPE_ALARM;
    }

    public void notifyDataAll() {
        this.notifyDataSetChanged();
    }

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

    /**************************************************
     * Model
     **************************************************/
    public void addItems(ArrayList<AdminMemberHrInfo> list) {
        if (mListData == null) mListData = new ArrayList<>();
        else mListData.clear();
        if (mListSearchData == null) mListSearchData = new ArrayList<>();
        else mListSearchData.clear();

        if (list != null)
            mListData.addAll(list);
        mListSearchData.addAll(mListData);
    }

    public void replaceItem(int index, AdminMemberHrInfo data) {
        if (mListData != null && mListData.size() > index)
            mListData.set(index, data);
    }

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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ALARM)
            return new AdminMemberListViewHolder(parent, mOnAdminMemberListListener);
        else if (viewType == TYPE_EMPTY)
            return new MainEmptyListViewHolder(parent);

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

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_ALARM) {
            int index = position;
            AdminMemberHrInfo data = mListSearchData.get(index);
            AdminMemberListViewHolder view = (AdminMemberListViewHolder) holder;
            view.setSearchWord(mSearchWord);
            view.onBind(data, index);
        } else if (getItemViewType(position) == TYPE_EMPTY) {
            int height = HFCommonUtil.getScreenHeight(mContext)
                    - HFCommonUtil.getStatusBarHeight(mContext)
                    - HFCommonUtil.getPixelFromDimension(mContext, R.dimen.main_top_toolbar_height);
            MainEmptyListViewHolder view = (MainEmptyListViewHolder) holder;
            view.onBind(mContext.getString(R.string.alarm_empty_data), height);
        }
    }

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

    private boolean isEmpty() {
        return (mListSearchData == null || mListSearchData.size() == 0);
    }

    /**************************************************
     * 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<AdminMemberHrInfo> filteredList = new ArrayList<>();

                    constraint = constraint.toString().toLowerCase(); // if we ignore case
                    for (AdminMemberHrInfo item : mListData) {
                        if ((item.getName() != null && item.getName().contains(constraint.toString())) ||
                                (item.getDepartment() != null && item.getDepartment().contains(constraint.toString())) ||
                                (item.getCompany() != null && item.getCompany().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<AdminMemberHrInfo>) results.values;
                notifyDataSetChanged();
            }
        };
        return filter;
    }


    /**************************************************
     * Interface
     **************************************************/
    public interface OnAdminMemberListListener {
        void onItemClicked(@NonNull AdminMemberHrInfo adminMemberHrInfo);
    }

}
