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

import android.content.Intent;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import edu.byu.suite.R;
import edu.byu.suite.features.learningSuite.model.CalendarItem;

/**
 * Created by cwoodfie on 11/2/16
 */

public class CalendarItemFragment extends LearningSuiteDetailFragment<CalendarItem> {

	public static CalendarItemFragment newInstance(CalendarItem calendarItem) {
		Bundle args = new Bundle();
		args.putParcelable(DATA, calendarItem);
		CalendarItemFragment fragment = new CalendarItemFragment();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.add_event, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.add_event) {
			Intent intent = new Intent(Intent.ACTION_EDIT).setData(CalendarContract.Events.CONTENT_URI)
														  .setType("vnd.android.cursor.dir/event")
														  .putExtra(CalendarContract.Events.TITLE, data.getText())
														  .putExtra(CalendarContract.Events.DESCRIPTION, data.getDetailText() != null ? data.getDetailText().toString() : "")
														  // If there is just one time, put it in for both so that the generated event in the calendar app has logical and related start/end times
														  .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, data.getBeginDate() != null ? data.getBeginDate().getTime() : data.getEndDate().getTime())
														  .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, data.getEndDate() != null ? data.getEndDate().getTime() : data.getBeginDate().getTime());

			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
