package com.example.spartahack.spartahack2016.Fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.spartahack.spartahack2016.Adapters.EventListAdapter;
import com.example.spartahack.spartahack2016.Adapters.SimpleSectionedRecyclerViewAdapter;
import com.example.spartahack.spartahack2016.Model.Event;
import com.example.spartahack.spartahack2016.R;
import com.example.spartahack.spartahack2016.Retrofit.GSONMock;
import com.example.spartahack.spartahack2016.Retrofit.ParseAPIService;

import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class ScheduleFragment extends BaseFragment {

    /** Recycler view that displays all objects */
    @Bind(android.R.id.list) RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View v =  inflater.inflate(R.layout.fragment_schedule, container, false);

        ButterKnife.bind(this, v);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        ParseAPIService.INSTANCE.getRestAdapter().getSchedule()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<GSONMock.Schedules>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("ScheduleFragment", e.toString());
                    }

                    @Override
                    public void onNext(GSONMock.Schedules schedules) {
                        ArrayList<Event> events = schedules.events;
                        // TODO: 1/5/16 fix this being empty
                        if (events == null || events.isEmpty())
                            return;
                        Collections.sort(events, new Comparator<Event>() {
                            @Override
                            public int compare(Event lhs, Event rhs) {
                                return DateTimeComparator.getInstance().compare(lhs.getTime(), rhs.getTime());
                            }
                         });


                        EventListAdapter eventListAdapter = new EventListAdapter(getActivity(), events);

                        ArrayList<SimpleSectionedRecyclerViewAdapter.Section> sections = new ArrayList<>();

                        DateTime date = events.get(0).getTime();
                        SimpleDateFormat formatDate = new SimpleDateFormat("EEEE, MMMM d, y");
                        sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, formatDate.format(events.get(0).getTime().toDate())));
                        // location the header should go at
                        int sectionLoc = 0;
                        for (Event e : events){
                            if (DateTimeComparator.getDateOnlyInstance().compare(e.getTime(), date) == 1){
                                sections.add(new SimpleSectionedRecyclerViewAdapter.Section(sectionLoc, formatDate.format(e.getTime().toDate())));
                                date = e.getTime();
                            }
                            sectionLoc++;
                        }

                        SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()];

                        SimpleSectionedRecyclerViewAdapter adapter = new SimpleSectionedRecyclerViewAdapter(getActivity(), R.layout.date_section, R.id.section_text, eventListAdapter);

                        adapter.setSections(sections.toArray(dummy));

                        recyclerView.setAdapter(adapter);

                    }
                });

        return v;
    }

}

@@ -3154,19 +3154,8 @@
 EEEE
-, MMMM d, y
 %22);%0A

change date format in schedule