<%@ page buffer="8kb" autoFlush="true" %>
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="zm" uri="com.zimbra.zm" %>
<%@ taglib prefix="app" uri="com.zimbra.htmlclient" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="com.zimbra.i18n" %>

<app:handleError>
    <zm:getMailbox var="mailbox"/>
    <c:choose>
    <c:when test="${not empty mailbox.prefs.locale}">
        <fmt:setLocale value='${mailbox.prefs.locale}' scope='request' />
    </c:when>
    <c:otherwise>
        <fmt:setLocale value='${pageContext.request.locale}' scope='request' />
    </c:otherwise>
    </c:choose>
    <fmt:setBundle basename="/messages/ZhMsg" scope='request' />
    <c:set var="action" value="${empty param.paction ? param.action : param.paction}" scope="request"/>
    <jsp:useBean id="expanded" scope="session" class="java.util.HashMap" />
    <%-- Compose, temp draft cleanup--%>
    <c:if test="${param.action ne 'compose' and not empty sessionScope.temp_draftid}">
        <zm:deleteMessage var="result" id="${sessionScope.temp_draftid}"/>
        <c:remove var="temp_draftid" scope="session"/>
    </c:if>
<%-- Deep linking  --%>
  <c:if test="${not empty param.app}">
	  <c:choose>
		  <c:when test="${param.app eq 'calendar' and (mailbox.features.calendar or mailbox.features.calendarUpsell)}">
			<c:redirect url="/h/calendar" />
		  </c:when>
		  <c:when test="${param.app eq 'mail' and not empty param.id  and zm:isMailEnabled(mailbox)}">
			<c:redirect url="/h/search?st=message&id=${param.id}&action=view" />
		  </c:when>
		  <c:when test="${param.app eq 'mail' and zm:isMailEnabled(mailbox)}">
			<c:redirect url="/h/search" />
		  </c:when>
		  <c:when test="${param.app eq 'voice' and mailbox.features.voice and not empty param.id}">
		            <c:catch var="ex">
		               <c:if test="${not empty param.phone}">
		                   <zm:computeSearchContext var="voicesearch" usecache="true" types="voicemail" query="phone:${param.phone}"/>
		                   <c:set var="matches" value="${false}"/>
		                   <c:forEach items="${voicesearch.searchResult.hits}" var="hit" varStatus="status">
		                       <c:if test="${hit.voiceMailItemHit.id eq param.id}">
		                           <c:set var="matches" value="${true}"/>
		                           <c:redirect url="/h/search?st=voicemail&action=listen&phone=${param.phone}&voiceId=${hit.voiceMailItemHit.serialize}"/>
		                       </c:if>    
		                   </c:forEach>
		                   <c:if test="${empty voicesearch.searchResult.hits or matches eq false}">
		                        <c:redirect url="/h/search?st=voicemail&sq=phone:${param.phone}"/>
		                   </c:if>
		               </c:if>
		               <c:if test="${empty param.phone}">
		                    <c:set var="firstPhoneAcc" value="${zm:getFirstPhoneAccount(pageContext)}"/>
		                    <c:redirect url="/h/search?st=voicemail&action=listen&phone=${firstPhoneAcc.phone.name}&voiceId=${param.id}"/>
		               </c:if>
		            </c:catch>
		            <c:if test="${!empty ex}">
		               <c:redirect url="/h/search?st=voicemail" />
		            </c:if>
		  </c:when>	
		  <c:when test="${param.app eq 'voice' and mailbox.features.voice}">
			<c:redirect url="/h/search?st=voicemail" />
		  </c:when>
		  <c:when test="${param.app eq 'options' and mailbox.features.options}">
			  <c:redirect url="/h/options?selected=${param.section}" />
		  </c:when>
		  <c:when test="${param.app eq 'contacts' and mailbox.features.contacts}">
			  <c:redirect url="/h/search?st=contact" />
		  </c:when>
          <c:when test="${param.app eq 'gal' and mailbox.features.gal}">
			  <c:redirect url="/h/search?st=gal" />
		  </c:when>
		  <c:when test="${param.app eq 'tasks' and mailbox.features.tasks}">
			  <c:redirect url="/h/search?st=task" />
		  </c:when>
          	  <c:when test="${param.app eq 'briefcases' and mailbox.features.briefcases}">
			  <c:redirect url="/h/search?st=briefcase" />
		 </c:when>
      </c:choose>
  </c:if>
  <c:if test="${not empty param.view}">
      <c:choose>
	  <c:when test="${param.view eq 'compose'}">
		  <c:redirect url="/h/search?action=compose">
			  <c:param name="subject" value="${param.subject}" />
			  <c:param name="to" value="${param.to}" />
			  <c:param name="body" value="${param.body}" />
		  </c:redirect>
	  </c:when>
      <c:otherwise>
          <c:if test="${not empty sessionScope.toAddresses}">
            <c:remove var="toAddresses" scope="session"/>
          </c:if>
      </c:otherwise>
      </c:choose>
	  <c:if test="${(param.view eq 'day') or (param.view eq 'workWeek') or (param.view eq 'week') or (param.view eq 'month')}">
		  <c:redirect url="/h/calendar" >
			  <c:param name="view" value="${param.view}" />
			  <c:param name="date" value="${param.date}" />
		  </c:redirect>
	  </c:if>
	  <c:if test="${(param.view eq 'msg') and (not empty param.id)}">
		  <c:redirect url="/h/search">
			  <c:param name="st" value='message'/>
			  <c:param name="action" value='view'/>
			  <c:param name="id" value='${param.id}'/>
		  </c:redirect>
	  </c:if>
  </c:if>

  <%-- end --%>

     <c:if test="${not empty param.expand}">
         <c:set target="${sessionScope.expanded}" property="${param.expand}" value="expand"/>
     </c:if>
     <c:if test="${not empty param.collapse}">
         <c:set target="${sessionScope.expanded}" property="${param.collapse}" value="collapse"/>
     </c:if>
    <c:if test="${param.init eq 'true'}">
        <c:if test="${mailbox.features.portalEnabled}">
            <c:redirect url="home?mesg=welcome"/>
        </c:if>
    </c:if>
     <%-- switch based on context search types --%>
     <c:set var="noComposeView" scope="session" value="${false}"/>
     <c:choose>
         <c:when test="${param.action eq 'compose'}">
             <app:composeCheck/>
         </c:when>
         <c:when test="${not empty param.doConvListViewAction}">
             <app:convListViewAction/>
         </c:when>
         <c:when test="${not empty param.doMessageAction}">
             <app:messageAction/>
         </c:when>
         <c:when test="${param.action eq 'newtask' or param.action eq 'edittask' or param.action eq 'viewtask' }">
             <app:editTaskCheck/>
         </c:when>
         <c:when test="${not empty param.doTaskAction}">
             <app:taskAction/>
         </c:when>
         <c:when test="${param.action eq 'newbrief'}">
             <app:newBriefCheck/>
         </c:when>
          <c:when test="${not empty param.doBriefcaseAction}">
             <app:briefcaseListViewAction/>
         </c:when>
         <c:when test="${not empty param.doNotebookAction}">
             <app:notebookListViewAction/>
         </c:when>
         <c:when test="${not empty param.doContactListViewAction or param.action eq 'newcontact' or param.action eq 'newcontactgroup'}">
             <app:contactListViewAction/>
         </c:when>
         <c:when test="${not empty param.doVoiceMailListViewAction}">
			 <app:voiceMailListViewAction/>
         </c:when>
         <c:when test="${not empty param.doVoiceMailViewAction}">
             <app:voiceMailViewAction/>
         </c:when>
     </c:choose>

    <c:if test="${param.st eq 'web'}">
        <fmt:message var="searchURL" key="searchURL" />
        <fmt:message var="searchParam" key="searchFieldName" />
        <c:redirect url="${searchURL}">
            <c:param name="${searchParam}" value="${param.sq}"/>
        </c:redirect>
    </c:if>

    <c:if test="${param.st eq 'calendar'}">
        <c:redirect url="/h/calendar" />            
    </c:if>

    <c:if test="${param.st eq 'home'}">
           <c:redirect url="/h/home" />
    </c:if>

    <c:if test="${param.st eq 'appointment'}">
        <c:redirect url="/h/calendar">
            <c:param name="sq" value="${param.sq}"/>
        </c:redirect>
    </c:if>
    <c:if test="${not zm:isMailEnabled(mailbox)}">
        <c:if test="${param.st eq 'message' or param.st eq 'conversation' or empty param.st}">
            <c:redirect url="/h/calendar"/>
        </c:if>
    </c:if>
    <%-- Include Shared Folder Search  --%>
    <c:set var="sfId" value=""/>
    <c:if test="${not empty param.incShared}">
        <zm:forEachFolder var="folder" skiproot="${true}" skipsystem="${true}" expanded="${sessionScope.expanded}" skiptrash="${true}">
            <c:if test="${folder.isMountPoint}">
                <c:if test="${param.st eq 'task' and folder.isTaskView}">
                    <c:if test="${not empty sfId}">
                        <c:set var="sfId" value="${sfId} OR"/>
                    </c:if>
                    <c:set var="sfId" value="${sfId} inid:${folder.id}"/>
                </c:if>
                <c:if test="${param.st eq 'contact' and folder.isContactView}">
                    <c:if test="${not empty sfId}">
                        <c:set var="sfId" value="${sfId} OR"/>
                    </c:if>
                    <c:set var="sfId" value="${sfId} inid:${folder.id}"/>
                </c:if>
                <c:if test="${(param.st eq 'message' or param.st eq 'conversation') and folder.isMailView}">
                    <c:if test="${not empty sfId}">
                        <c:set var="sfId" value="${sfId} OR"/>
                    </c:if>
                    <c:set var="sfId" value="${sfId} inid:${folder.id}"/>
                </c:if>
            </c:if>    
        </zm:forEachFolder>                            
    </c:if>
    <c:if test="${not empty sfId}">
        <c:set var="sharedSq" value="${param.sq} (${sfId} OR is:local)"/>
    </c:if>
    <%-- End - Include Shared Folder Search  --%>

    <zm:computeSearchContext var="context" usecache="true" query="${not empty param.incShared ? sharedSq : ''}"/>

</app:handleError>
<c:if test="${param.mesg eq 'welcome'}">
    <c:set var="refreshSkin" value="${true}" scope="request"/>
    <c:remove var="skin" scope="session"/>
    <app:status style="info" html="true">
    <fmt:setBundle basename="/messages/ZhMsg" scope='request' force="true"/>
        <fmt:message key="htmlWelcomeMesg"/>
    </app:status>
</c:if>
<c:choose>
<c:when test="${zm:boolean(param.actionMessageMovedTrash)}" >
    <app:status>
        <fmt:message key="actionMessageDeleted"/>
    </app:status>
</c:when>
<c:when test="${zm:boolean(param.actionMessageMoved)}" >
    <c:set var="movedFolderName" value="${zm:getFolderName(pageContext, param.movedFolderId)}"/>
    <app:status>
        <fmt:message  key="actionSingleMessageMoved">
            <fmt:param value="${movedFolderName}"/>
        </fmt:message>
    </app:status>
</c:when>
</c:choose>
 <%-- switch based on context search types --%>
<c:choose>
    <%--In case of send and cancel, noComposeView is set to true--%>
    <c:when test="${param.action eq 'compose' and noComposeView eq false}">
        <%-- do nothing --%>
    </c:when>
    <c:when test="${context.isConversationSearch and action eq 'view'}">
        <app:convView context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and action eq 'view2'}">
        <app:convView2 context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'right' and action eq 'paneView'}">
        <app:convColumnView context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'right' and action eq 'paneView2'}">
        <app:convColumnView context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'bottom' and action eq 'rowView'}">
        <app:convRowView context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'bottom' and action eq 'rowView2'}">
        <app:convRowView context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'off' and action eq 'paneView'}">
        <app:convView context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'off' and action eq 'paneView2'}">
        <app:convView2 context="${context}"/>
    </c:when>
     <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'off' and action eq 'offView'}">
        <app:convListView context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'off' and action eq 'offView2'}">
        <app:convListView context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'right'}">
        <app:convColumnView context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'bottom'}">
        <app:convRowView context="${context}"/>
    </c:when>
    <c:when test="${context.isConversationSearch and mailbox.prefs.readingPaneLocation eq 'off'}">
        <app:convListView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isMessageSearch) and action eq 'view'}">
        <app:messageView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isMessageSearch) and mailbox.prefs.readingPaneLocation eq 'right' and action eq 'paneView'}">
        <app:messageColumnView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isMessageSearch) and mailbox.prefs.readingPaneLocation eq 'bottom' and action eq 'rowView'}">
        <app:messageRowView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isMessageSearch) and mailbox.prefs.readingPaneLocation eq 'off' and action eq 'view'}">
        <app:messageView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isMessageSearch) and mailbox.prefs.readingPaneLocation eq 'right'}">
        <app:messageColumnView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isMessageSearch) and mailbox.prefs.readingPaneLocation eq 'bottom'}">
        <app:messageRowView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isMessageSearch)}">
        <app:messageListView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isContactSearch) and action eq 'view'}">
        <app:contactView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isContactSearch) or context.isGALSearch}">
        <app:contactListView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isTaskSearch)}">
        <app:taskListView context="${context}"/>
    </c:when>
	<c:when test="${zm:boolean(context.isVoiceMailSearch) and action eq 'listen'}">
		<app:voiceMailView context="${context}"/>
	</c:when>
    <c:when test="${zm:boolean(context.isVoiceMailSearch)}">
        <app:voiceMailListView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isCallSearch)}">
        <app:callListView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isBriefcaseSearch) and (param.view eq 'dv' or empty param.view)}">
        <app:briefcaseListView context="${context}"/>
    </c:when>
    <c:when test="${zm:boolean(context.isBriefcaseSearch) and param.view eq 'ev'}">
        <app:briefcaseExplorerView context="${context}"/>
    </c:when>
    <%--bug:56196
    <c:when test="${context.isWikiSearch}">
        <c:choose>
            <c:when test="${context.isTagSearch}">
                <app:notebookListView context="${context}"/>
            </c:when>
            <c:otherwise>
                <app:notebookRestView context="${context}"/>
            </c:otherwise>
        </c:choose>
    </c:when>
    --%>
    <c:otherwise>
        <fmt:message key="error" var="title"/>
        <app:view mailbox="${mailbox}" title="${title}" selected="${not mailbox.features.mail ? 'calendar' : 'mail'}" context="${context}" folders="true" tags="true" searches="true" ads="" keys="true">
            <fmt:message key="ztaglib.TAG_EXCEPTION"/>
        </app:view>
    </c:otherwise>
</c:choose>
