package edu.byu.suite.features.learningSuite.fragment;

import android.os.Bundle;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import edu.byu.suite.R;
import edu.byu.suite.features.learningSuite.model.CalendarItem;
import edu.byu.suite.features.learningSuite.model.Course;
import edu.byu.support.ByuRecyclerView;
import edu.byu.support.adapter.ByuRecyclerAdapter;
import edu.byu.support.adapter.DefaultHeaderAdapter;
import edu.byu.support.adapter.HeaderAdapter;

/**
 * Created by cwoodfie on 10/31/16
 */
public class CalendarFragment extends LearningSuiteListFragment {
	private DefaultHeaderAdapter<CalendarItem> adapter = new DefaultHeaderAdapter<>(null, R.layout.default_list_item_indicator, (calendarItem) -> calendarItem.getText().toString(), null,
																					(data, view, position) -> replaceFragment(CalendarItemFragment.newInstance(data)));

	public static CalendarFragment newInstance(Course course) {
		Bundle args = new Bundle();
		args.putString(Course.COURSE_TAG, course.getId());

		CalendarFragment fragment = new CalendarFragment();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		enqueueCall(getApi().getCalendarItems(getArguments().getString(Course.COURSE_TAG)), new LsCallback<>(this, list -> {
			List<Object> data = new ArrayList<>(list.size());

			Integer todaysPosition = null;
			Date todaysDate = getDateWithoutTime(new Date());

			Date currentEndDate = null;
			for (CalendarItem calendarItem : list) {
				Date itemEndDate = getDateWithoutTime(calendarItem.getEndDate());

				// add headers to the list based on what day the calendar item falls on (multiple items on one day go under same header)
				if (itemEndDate != null && !itemEndDate.equals(currentEndDate)) {
					// If this is the first item after or on the current day, we will scroll to this item
					if (todaysPosition == null && itemEndDate.getTime() >= todaysDate.getTime()) {
						todaysPosition = data.size();
					}
					String formattedDate = DateFormat.getDateInstance(DateFormat.LONG).format(itemEndDate);
					currentEndDate = itemEndDate;
					data.add(new HeaderAdapter.Header(formattedDate));
				}

				data.add(calendarItem);
			}

			adapter.setList(data);
			if (getView() != null && todaysPosition != null) {
				((ByuRecyclerView) getView().findViewById(R.id.ls_recycler_view)).getLayoutManager().scrollToPosition(todaysPosition);
			}
		}));
	}

	private Date getDateWithoutTime(Date date) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	@Override
	protected String getDataType() {
		return "calendar events";
	}

	@Override
	protected ByuRecyclerAdapter getAdapter() {
		return adapter;
	}
}
