<%
/**
 * 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.
 */
%>

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

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

SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, SearchContainer.DEFAULT_DELTA, portletURL, headerNames, "there-are-no-events-on-this-day");

List results = CalEventLocalServiceUtil.getEvents(scopeGroupId, selCal, eventType);

results = ListUtil.sort(results, new EventTimeComparator(timeZone, locale));

searchContainer.setTotal(results.size());

results = ListUtil.subList(results, 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()));

	// 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);

	// Action

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

	// Add result row

	resultRows.add(row);
}
%>

<%
boolean showAddEventButtonButton = CalendarPermission.contains(permissionChecker, scopeGroupId, ActionKeys.ADD_EVENT);
boolean showPermissionsButton = GroupPermissionUtil.contains(permissionChecker, scopeGroupId, ActionKeys.PERMISSIONS);
%>

<c:if test="<%= showAddEventButtonButton || showPermissionsButton %>">
	<c:if test="<%= showAddEventButtonButton %>">
		<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" />
	</c:if>

	<c:if test="<%= showPermissionsButton %>">
		<liferay-security:permissionsURL
			modelResource="com.liferay.portlet.calendar"
			modelResourceDescription="<%= HtmlUtil.escape(themeDisplay.getScopeGroupName()) %>"
			resourcePrimKey="<%= String.valueOf(scopeGroupId) %>"
			var="permissionsURL"
		/>

		<aui:button onClick="<%= permissionsURL %>" value="permissions" />
	</c:if>

	<br /><br />
</c:if>

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