package ru.dyatel.tsuschedule;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.solovyev.android.views.llm.LinearLayoutManager;
import ru.dyatel.tsuschedule.parsing.Lesson;
import ru.dyatel.tsuschedule.parsing.util.Filter;
import ru.dyatel.tsuschedule.parsing.util.IterableFilter;
import ru.dyatel.tsuschedule.parsing.util.KeyExtractor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class WeekAdapter extends RecyclerView.Adapter<WeekAdapter.Holder> {

    // TODO: proper sorting by weekday
    private List<String> order = new ArrayList<String>();

    private Map<String, Set<Lesson>> weekdays = new HashMap<String, Set<Lesson>>();

    class Holder extends RecyclerView.ViewHolder {

        TextView weekday;
        RecyclerView list;

        public Holder(View v) {
            super(v);

            weekday = (TextView) v.findViewById(R.id.weekday);
            list = (RecyclerView) v.findViewById(R.id.lesson_list);
            list.setLayoutManager(new LinearLayoutManager(v.getContext()));
            list.setAdapter(new WeekdayAdapter());
        }

    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.weekday_layout, parent, false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        String weekday = order.get(position);

        holder.weekday.setText(weekday);
        WeekdayAdapter adapter = (WeekdayAdapter) holder.list.getAdapter();
        adapter.updateData(weekdays.get(weekday));
    }

    @Override
    public int getItemCount() {
        return weekdays.size();
    }

    public void updateData(Set<Lesson> lessons) {
        Set<String> keys = new KeyExtractor<Lesson, String>() {
            @Override
            protected String getKey(Lesson element) {
                return element.getWeekday();
            }
        }.extract(lessons);

        for (final String key : keys) {
            IterableFilter<Lesson> filter = new IterableFilter<Lesson>();
            filter.apply(new Filter<Lesson>() {
                @Override
                public boolean accept(Lesson obj) {
                    return obj.getWeekday().equals(key);
                }
            });
            weekdays.put(key, filter.filter(lessons));
            order.add(key); // TODO: remove this
        }

        notifyDataSetChanged();
    }

}

@@ -673,42 +673,508 @@
     
-// TODO: proper sorting by weekday
+private static final String%5B%5D normalWeekdayOrder = %7B%0A            %22%5Cu043f%5Cu043e%5Cu043d%5Cu0435%5Cu0434%5Cu0435%5Cu043b%5Cu044c%5Cu043d%5Cu0438%5Cu043a%22,%0A            %22%5Cu0432%5Cu0442%5Cu043e%5Cu0440%5Cu043d%5Cu0438%5Cu043a%22,%0A            %22%5Cu0441%5Cu0440%5Cu0435%5Cu0434%5Cu0430%22,%0A            %22%5Cu0447%5Cu0435%5Cu0442%5Cu0432%5Cu0435%5Cu0440%5Cu0433%22,%0A            %22%5Cu043f%5Cu044f%5Cu0442%5Cu043d%5Cu0438%5Cu0446%5Cu0430%22,%0A            %22%5Cu0441%5Cu0443%5Cu0431%5Cu0431%5Cu043e%5Cu0442%5Cu0430%22,%0A            %22%5Cu0432%5Cu043e%5Cu0441%5Cu043a%5Cu0440%5Cu0435%5Cu0441%5Cu0435%5Cu043d%5Cu044c%5Cu0435%22%0A    %7D;
 %0A   
@@ -1195,17 +1195,24 @@
 String%3E 
-o
+weekdayO
 rder = n
@@ -2107,17 +2107,24 @@
 ekday = 
-o
+weekdayO
 rder.get
@@ -2442,24 +2442,88 @@
  lessons) %7B%0A
+        weekdayOrder.clear();%0A%0A        // Get all used weekdays%0A
         Set%3C
@@ -2734,24 +2734,185 @@
 (lessons);%0A%0A
+        // Skip a weekday if there is no lessons%0A        for (String s : normalWeekdayOrder) %7B%0A            if (keys.contains(s)) weekdayOrder.add(s);%0A        %7D%0A%0A
         for 
@@ -3230,16 +3230,16 @@
     %7D);%0A
+
         
@@ -3289,57 +3289,8 @@
 ));%0A
-            order.add(key); // TODO: remove this%0A
     

Fix #2