<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<%
PortletURL iteratorURL = PortletURLUtil.clone(portletURL, renderResponse);

List<String> headerNames = new ArrayList<String>();

headerNames.add("date");
headerNames.add("time");
headerNames.add("title");
headerNames.add("type");
headerNames.add("details");
headerNames.add(StringPool.BLANK);

SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, SearchContainer.DEFAULT_DELTA, iteratorURL, headerNames, null);

int total = CalEventLocalServiceUtil.getEventsCount(scopeGroupId, eventType);

searchContainer.setTotal(total);

List results = CalEventLocalServiceUtil.getEvents(scopeGroupId, eventType, searchContainer.getStart(), searchContainer.getEnd());

searchContainer.setResults(results);

List resultRows = searchContainer.getResultRows();

for (int i = 0; i < results.size(); i++) {
	CalEvent event = (CalEvent)results.get(i);

	event = event.toEscapedModel();

	ResultRow row = new ResultRow(event, event.getEventId(), i);

	PortletURL rowURL = renderResponse.createRenderURL();

	rowURL.setParameter("struts_action", "/calendar/view_event");
	rowURL.setParameter("redirect", currentURL);
	rowURL.setParameter("eventId", String.valueOf(event.getEventId()));

	// Date

	if (event.isTimeZoneSensitive()) {
		row.addText(dateFormatDate.format(Time.getDate(event.getStartDate(), timeZone)), rowURL);
	}
	else {
		row.addText(dateFormatDate.format(event.getStartDate()), rowURL);
	}

	// Time

	boolean allDay = CalUtil.isAllDay(event, timeZone, locale);

	if (allDay) {
		row.addText(LanguageUtil.get(pageContext, "all-day"), rowURL);
	}
	else {
		if (event.isTimeZoneSensitive()) {
			row.addText(
				dateFormatTime.format(Time.getDate(event.getStartDate(), timeZone)) + " &#150; " + dateFormatTime.format(Time.getDate(CalUtil.getEndTime(event), timeZone)),
				rowURL);
		}
		else {
			row.addText(
				dateFormatTime.format(event.getStartDate()) + " &#150; " + dateFormatTime.format(CalUtil.getEndTime(event)),
				rowURL);
		}
	}

	// Title and type

	row.addText(event.getTitle(), rowURL);
	row.addText(LanguageUtil.get(pageContext, HtmlUtil.unescape(event.getType())), rowURL);

	// Details

	row.addJSP("left", SearchEntry.DEFAULT_VALIGN, "/html/portlet/calendar/view_event_recurrence.jsp");

	// Action

	row.addJSP("right", SearchEntry.DEFAULT_VALIGN, "/html/portlet/calendar/event_action.jsp");

	// Add result row

	resultRows.add(row);
}
%>

<c:if test="<%= CalendarPermission.contains(permissionChecker, scopeGroupId, ActionKeys.ADD_EVENT) %>">
	<div>
		<portlet:renderURL var="editEventURL">
			<portlet:param name="struts_action" value="/calendar/edit_event" />
			<portlet:param name="redirect" value="<%= currentURL %>" />
			<portlet:param name="month" value="<%= String.valueOf(selMonth) %>" />
			<portlet:param name="day" value="<%= String.valueOf(selDay) %>" />
			<portlet:param name="year" value="<%= String.valueOf(selYear) %>" />
		</portlet:renderURL>

		<aui:button onClick="<%= editEventURL %>" value="add-event" />
	</div>

	<br />
</c:if>

<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />

<%
PortalUtil.setPageSubtitle(LanguageUtil.get(pageContext, "events"), request);
%>