package mil.nga.giat.mage.event;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;

import org.apache.commons.lang3.StringUtils;

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

import mil.nga.giat.mage.R;
import mil.nga.giat.mage.sdk.datastore.user.Event;

/**
 * Created by wnewman on 2/23/18.
 */

public class EventListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public interface OnEventClickListener {
        void onEventClick(Event event);
    }

    static final int ITEM_TYPE_HEADER = 1;
    static final int ITEM_TYPE_EVENT = 2;

    private List<Event> events;
    private List<Event> filteredEvents = new ArrayList<>();
    private List<Event> recentEvents;
    private List<Event> filteredRecentEvents = new ArrayList<>();
    private OnEventClickListener listener;

    private static Predicate<Event> eventFilterPredicate(String text){
        final String lowerCaseText = text.toLowerCase();
        return event -> event.getName().toLowerCase().contains(lowerCaseText) ||
                event.getDescription().toLowerCase().contains(lowerCaseText);
    }

    EventListAdapter(List<Event> events, List<Event> recentEvents, OnEventClickListener listener) {
        Collections.sort(events, new Ordering<Event>() {
            @Override
            public int compare(Event e1, Event e2) {
                return e1.getName().toLowerCase().compareTo(e2.getName().toLowerCase());
            }
        });

        this.events = events;
        this.filteredEvents.addAll(events);
        this.recentEvents = recentEvents;
        this.filteredRecentEvents.addAll(recentEvents);
        this.listener = listener;
    }

    public void filter(String text) {
        filteredEvents.clear();
        filteredRecentEvents.clear();

        if (text.isEmpty()){
            filteredEvents.addAll(events);
            filteredRecentEvents.addAll(recentEvents);
        } else {
            filteredEvents = Lists.newArrayList(Iterables.filter(events, eventFilterPredicate(text)));
            filteredRecentEvents = Lists.newArrayList(Iterables.filter(recentEvents, eventFilterPredicate(text)));
        }

        notifyDataSetChanged();
    }

    private class SectionViewHolder extends RecyclerView.ViewHolder {
        private TextView sectionName;

        private SectionViewHolder(View view) {
            super(view);

            sectionName = view.findViewById(R.id.section_name);
        }
    }

    private class EventViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView description;

        private EventViewHolder(View view) {
            super(view);

            name = view.findViewById(R.id.event_name);
            description = view.findViewById(R.id.event_description);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_EVENT) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_list_item, parent, false);
            return new EventViewHolder(itemView);
        } else if (viewType == ITEM_TYPE_HEADER) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_list_section_header, parent, false);
            return new SectionViewHolder(itemView);
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof EventViewHolder) {
            bindEventViewHolder((EventViewHolder) holder, position);
        } else if (holder instanceof SectionViewHolder) {
            bindSectionViewHolder((SectionViewHolder) holder, position);
        }
    }

    @Override
    public int getItemCount() {
        return filteredEvents.size() + filteredRecentEvents.size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position == filteredRecentEvents.size() + 1) {
            return ITEM_TYPE_HEADER;
        } else {
            return ITEM_TYPE_EVENT;
        }
    }

    private void bindEventViewHolder(EventViewHolder holder, int position) {
        final Event event;
        if (position <= filteredRecentEvents.size()) {
            event = filteredRecentEvents.get(position - 1);
        } else {
            event = filteredEvents.get(position - filteredRecentEvents.size() - 2);
        }

        holder.name.setText(event.getName());

        if (StringUtils.isEmpty(event.getDescription())) {
            holder.description.setVisibility(View.GONE);
        } else {
            holder.description.setVisibility(View.VISIBLE);
            holder.description.setText(event.getDescription());
        }

        holder.itemView.setOnClickListener(v -> listener.onEventClick(event));
    }

    private void bindSectionViewHolder(SectionViewHolder holder, int position) {
        holder.sectionName.setText(position == 0 ? "Recent Events" : "Events");
    }
}

@@ -1197,18 +1197,19 @@
 ng text)
+ 
 %7B%0A
-
         
@@ -1253,24 +1253,25 @@
 owerCase();%0A
+%0A
         retu
@@ -1282,31 +1282,158 @@
 vent -%3E 
-event.getName()
+%7B%0A            String name = event.getName();%0A            String description = event.getDescription();%0A            return (name != null && name
 .toLower
@@ -1462,16 +1462,17 @@
 aseText)
+)
  %7C%7C%0A    
@@ -1483,26 +1483,45 @@
         
-event.getD
+    (description != null && d
 escripti
@@ -1522,18 +1522,16 @@
 cription
-()
 .toLower
@@ -1560,16 +1560,28 @@
 aseText)
+);%0A        %7D
 ;%0A    %7D%0A

Fix event filter