package mn.devfest.schedule;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import butterknife.Bind;
import butterknife.ButterKnife;
import mn.devfest.DevFestApplication;
import mn.devfest.R;
import mn.devfest.api.DevFestDataSource;
import mn.devfest.api.model.Session;
import mn.devfest.api.model.Speaker;
import mn.devfest.sessions.SessionListAdapter;
import mn.devfest.view.decoration.DividerItemDecoration;

/**
 * Fragment that displays the user's schedule
 *
 * @author bherbst
 */
public class ScheduleFragment extends Fragment implements DevFestDataSource.DataSourceListener {

    @Bind(R.id.schedule_recyclerview)
    RecyclerView mScheduleRecyclerView;

    private SessionListAdapter mAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    private List<Session> mSessions = new ArrayList<>();
    private DevFestDataSource mDataSource;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (mDataSource == null) {
            mDataSource = DevFestApplication.get(getActivity()).component().datasource();
        }
        mDataSource.setDataSourceListener(this);
        setSessions(mDataSource.getUserSchedule());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_schedule, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);

        mAdapter = new SessionListAdapter();
        mAdapter.setSessions(mSessions);
        mScheduleRecyclerView.setAdapter(mAdapter);
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mScheduleRecyclerView.setLayoutManager(mLinearLayoutManager);
        mScheduleRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
    }

    /**
     * Updates the data set, and notifies the adapter of the data set change
     * @param sessions the sessions to update the UI with
     */
    public void setSessions(List<Session> sessions) {
        mSessions = sessions;
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onSessionsUpdate(List<Session> sessions) {
        setSessions(sessions);
    }

    @Override
    public void onSpeakersUpdate(List<Speaker> speakers) {
        //Intentionally left blank; no action currently needed
    }

    @Override
    public void onUserScheduleUpdate(List<Session> userSchedule) {
        setSessions(userSchedule);
    }
}

@@ -1485,23 +1485,23 @@
     setS
-essions
+chedule
 (mDataSo
@@ -2347,24 +2347,352 @@
 T));%0A    %7D%0A%0A
+    @Override%0A    public void onResume() %7B%0A        super.onResume();%0A        //Refresh the UI with the latest data%0A        setSchedule(mDataSource.getSessions());%0A    %7D%0A%0A    /**%0A     * TODO update documentation%0A     */%0A    @Override%0A    public void onDetach() %7B%0A        super.onDetach();%0A        //TODO cleanup resources%0A    %7D%0A%0A
     /**%0A    
@@ -2850,23 +2850,23 @@
 oid setS
-essions
+chedule
 (List%3CSe
@@ -3093,23 +3093,23 @@
     setS
-essions
+chedule
 (session
@@ -3355,23 +3355,23 @@
     setS
-essions
+chedule
 (userSch

Address data in onResume() Rename method for clarity