<%--

    Copyright (C) 2000 - 2024 Silverpeas

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    As a special exception to the terms and conditions of version 3.0 of
    the GPL, you may redistribute this Program in connection with Free/Libre
    Open Source Software ("FLOSS") applications as described in Silverpeas's
    FLOSS exception.  You should have received a copy of the text describing
    the FLOSS exception, and it is also available here:
    "http://www.silverpeas.org/docs/core/legal/floss_exception.html"

    This program 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

--%>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="check.jsp" %>

<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib uri="jakarta.tags.fmt" prefix="fmt" %>
<%@ taglib uri="silverpeas.tags.silverFunctions" prefix="silfn" %>
<%@ taglib uri="silverpeas.tags.viewGenerator" prefix="view" %>
<%@ taglib tagdir="/WEB-INF/tags/silverpeas/infoLetter" prefix="infoLetterTags" %>
<%@ page import="org.silverpeas.core.util.DateUtil" %>
<%@ page import="org.silverpeas.components.infoletter.model.InfoLetterPublication" %>
<%@ page import="java.util.stream.Collectors" %>

<c:set var="resources" value="${requestScope.resources}"/>
<jsp:useBean id="resources" type="org.silverpeas.core.util.MultiSilverpeasBundle"/>
<c:set var="userLanguage" value="${resources.language}"/>
<fmt:setLocale value="${userLanguage}"/>
<view:setBundle bundle="${resources.multilangBundle}"/>
<view:setBundle bundle="${resources.iconsBundle}" var="icons"/>

<c:set var="showHeader" value="${requestScope.showHeader}"/>
<jsp:useBean id="showHeader" type="java.lang.Boolean"/>
<c:set var="isSuscriber" value="${requestScope.userIsSuscriber}"/>
<jsp:useBean id="isSuscriber" type="java.lang.Boolean"/>

<c:set var="publications" value="${requestScope.listParutions}"/>
<jsp:useBean id="publications" type="java.util.List<org.silverpeas.components.infoletter.model.InfoLetterPublication>"/>

<c:set var="lastNSent" value="${resources.getSetting('user.lastNSent', 4)}"/>
<jsp:useBean id="lastNSent" type="java.lang.Integer"/>

<c:set var="isAnonymous" value="${requestScope.isAnonymous}"/>
<c:set var="isAccessGuest" value="${requestScope.isAccessGuest}"/>
<c:set var="letterName" value="${requestScope.letterName}"/>
<c:set var="letterDescription" value="${requestScope.letterDescription}"/>
<c:set var="letterFrequence" value="${requestScope.letterFrequence}"/>
<c:set var="sentPublications" value="<%=publications.stream().filter(InfoLetterPublication::isValid).collect(Collectors.toList())%>"/>
<jsp:useBean id="sentPublications" type="java.util.List<org.silverpeas.components.infoletter.model.InfoLetterPublication>"/>
<c:set var="lastNSentPublications" value="<%=sentPublications.stream().limit(lastNSent).collect(Collectors.toList())%>"/>

<fmt:message key="infoLetter.desabonner" var="unsubscribeLabel"/>
<fmt:message key="infoLetter.desabonner" var="unsubscribeIcon" bundle="${icons}"/>
<c:url var="unsubscribeIcon" value="${unsubscribeIcon}"/>
<fmt:message key="infoLetter.abonner" var="subscribeLabel"/>
<fmt:message key="infoLetter.abonner" var="subscribeIcon" bundle="${icons}"/>
<c:url var="subscribeIcon" value="${subscribeIcon}"/>
<fmt:message key="infoLetter.section.sent.title" var="sentSectionTitle"/>
<fmt:message key="infoLetter.section.lastNSent.title" var="lastNSentSectionTitle">
  <fmt:param value="${lastNSent}"/>
</fmt:message>

<c:set var="componentId" value="<%=componentId%>"/>

<view:sp-page angularJsAppName="silverpeas.infoLetter">
  <view:sp-head-part withCheckFormScript="true">
    <view:includePlugin name="toggle"/>
    <script type="text/javascript">
      function openViewParution(par) {
        sp.navRequest('View').withParam('parution', par).go();
      }
    </script>
  </view:sp-head-part>
  <view:sp-body-part>
    <view:operationPane>
      <c:if test="${not isAnonymous and not isAccessGuest}">
        <c:choose>
          <c:when test="${isSuscriber}">
            <view:operation action="UnsuscribeMe" icon="${unsubscribeIcon}" altText="${unsubscribeLabel}"/>
          </c:when>
          <c:otherwise>
            <view:operation action="SuscribeMe" icon="${subscribeIcon}" altText="${subscribeLabel}"/>
          </c:otherwise>
        </c:choose>
      </c:if>
    </view:operationPane>
    <view:window>
      <view:frame>
        <view:componentInstanceIntro componentId="${componentId}" language="${userLanguage}"/>
        <c:if test="${showHeader}">
          <div class="headerInfoLetter">
            <h2 class="name">${silfn:escapeHtml(letterName)}</h2>
            <div class="frequence">${silfn:escapeHtml(letterFrequence)}</div>
            <p class="description componentInstanceIntro">${silfn:escapeHtml(letterDescription)}</p>
          </div>
        </c:if>
        <view:areaOfOperationOfCreation/>
        <div id="infoletter-home-app">
          <div id="infoletter-lastNSent">
            <div class="header">
              <h3 class="infoletter-last-title">${lastNSentSectionTitle}</h3>
            </div>
            <infoLetterTags:infoLetterList newsletters="${lastNSentPublications}" readonly="true"/>
          </div>
          <div id="infoletter-sended-reader">
            <div class="header">
              <h3 class="infoletter-sending-title">${sentSectionTitle}</h3>
            </div>
            <fmt:message key="infoLetter.name" var="nameLabel"/>
            <fmt:message key="GML.date" var="dateLabel"/>
            <fmt:message key="GML.operation" var="operationLabel"/>
            <fmt:message key="infoLetter.minicone" var="newsletterIcon" bundle="${icons}"/>
            <c:url var="newsletterIcon" value="${newsletterIcon}"/>
            <fmt:message key="infoLetter.permalink" var="permlinkIcon" bundle="${icons}"/>
            <c:url var="permlinkIcon" value="${permlinkIcon}"/>
            <fmt:message key="infoLetter.nonParu" var="notReleaseLabel"/>
            <fmt:message key="infoLetter.nonvisible" var="notReleaseIcon" bundle="${icons}"/>
            <c:url var="notReleaseIcon" value="${notReleaseIcon}"/>
            <fmt:message key="infoLetter.paru" var="releaseLabel"/>
            <fmt:message key="infoLetter.visible" var="releaseIcon" bundle="${icons}"/>
            <c:url var="releaseIcon" value="${releaseIcon}"/>
            <div id="newsletter-list">
              <view:arrayPane var="InfoLetter" routingAddress="Main">
                <view:arrayColumn title="" sortable="false"/>
                <view:arrayColumn title="${nameLabel}" compareOn="${n -> n.title}"/>
                <view:arrayColumn title="${dateLabel}" compareOn="${n -> n.parutionDate}"/>
                <view:arrayLines var="pub" items="${sentPublications}">
                  <jsp:useBean id="pub" type="org.silverpeas.components.infoletter.model.InfoLetterPublication"/>
                  <c:set var="pubId" value="${pub.getPK().id}"/>
                  <c:set var="accessUrl" value="javascript:openViewParution('${pubId}')"/>
                  <view:arrayLine>
                    <view:arrayCellText>
                      <a href="${accessUrl}">
                        <img src="${newsletterIcon}" alt=""/>
                      </a>
                    </view:arrayCellText>
                    <view:arrayCellText>
                      <a href="${accessUrl}">${silfn:escapeHtml(pub.title)}</a>
                      <a href="${pub.getPermalink()}" class="sp-permalink">
                        <img src="${permlinkIcon}" alt=""/>
                      </a>
                    </view:arrayCellText>
                    <view:arrayCellText>
                      <c:if test="${pub.isValid()}">
                        <c:set var="parutionDate" value="<%=DateUtil.parse(pub.getParutionDate())%>"/>
                        ${silfn:formatDate(parutionDate, userLanguage)}
                      </c:if>
                    </view:arrayCellText>
                  </view:arrayLine>
                </view:arrayLines>
              </view:arrayPane>
              <script type="text/javascript">
                whenSilverpeasReady(function() {
                  sp.arrayPane.ajaxControls('#newsletter-list');
                });
              </script>
            </div>
          </div>
        </div>
      </view:frame>
    </view:window>
    <view:progressMessage/>
  </view:sp-body-part>
</view:sp-page>