<%--

    Copyright (C) 2000 - 2026 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="checkKmelia.jsp" %>
<%@ taglib uri="jakarta.tags.core" prefix="c" %>
<%@ taglib uri="jakarta.tags.fmt" prefix="fmt" %>
<%@ taglib uri="silverpeas.tags.viewGenerator" prefix="view"%>
<%@ taglib uri="silverpeas.tags.silverFunctions" prefix="silfn" %>
<%@ taglib tagdir="/WEB-INF/tags/silverpeas/kmelia" prefix="kmelia" %>
<%@page import="org.silverpeas.components.kmelia.SearchContext"%>
<%@page import="org.silverpeas.core.admin.user.model.SilverpeasRole"%>
<%@ page import="org.silverpeas.core.i18n.I18NHelper" %>
<%@ page import="org.silverpeas.core.webapi.node.NodeType" %>

<c:url var="mandatoryFieldUrl" value="/util/icons/mandatoryField.gif"/>
<fmt:setLocale value="${sessionScope[sessionController].language}" />
<view:setBundle bundle="${requestScope.resources.multilangBundle}" />

<c:set var='highestUserRole' value='<%=SilverpeasRole.fromString((String) request.getAttribute("Profile"))%>'/>
<c:set var="displaySearch" value="${silfn:booleanValue(requestScope.DisplaySearch)}"/>
<c:set var="componentId" value="<%=componentId%>"/>
<c:set var="currentFolderId" value="${requestScope.CurrentFolderId}"/>
<c:if test="${silfn:isNotDefined(currentFolderId)}">
  <c:set var="currentFolderId" value="0"/>
</c:if>
<%
  String   profile      = (String) request.getAttribute("Profile");
  String  translation   = (String) request.getAttribute("Language");
  boolean displayNBPublis = (Boolean) request.getAttribute("DisplayNBPublis");
  Boolean rightsOnTopics  = (Boolean) request.getAttribute("RightsOnTopicsEnabled");
  int    currentPageIndex = (Integer) request.getAttribute("PageIndex");

  SearchContext searchContext = (SearchContext) request.getAttribute("SearchContext");

  String    pubIdToHighlight  = (String) request.getAttribute("PubIdToHighlight"); //used when we have found publication from search (only toolbox)
  String language = kmeliaScc.getLanguage();

  String userId = kmeliaScc.getUserId();
%>

<view:sp-page>
  <view:sp-head-part withCheckFormScript="true">
    <view:script src="/util/javaScript/browseBarComplete.js"/>
    <view:includePlugin name="datepicker" />
    <view:includePlugin name="subscription"/>
    <view:includePlugin name="preview"/>
    <view:includePlugin name="rating" />
    <view:includePlugin name="basketSelection"/>

    <view:script src="javaScript/navigation.js"/>
    <view:script src="javaScript/searchInTopic.js"/>
    <view:script src="javaScript/publications.js"/>

    <script type="text/javascript">
      const rootNodeId = '0';
      const binNodeId = '1';
      let isSearchTopicEnabled = ${displaySearch};

      <%--The below triggered function has to be defined as soon as possible in HTML code in order to
      increase chances to perform the treatment when "menuRender" event is fired --%>
      $(document).ready(function() {
        menuRenderedPromise.then(function(){

          sp.i18n.load({
            bundle : 'org.silverpeas.kmelia.multilang.kmeliaBundle',
            language : '<%=language%>'
          });
          displayTopicContent('${currentFolderId}');
        });
      });

      function topicGoTo(id) {
        closeWindows();
        displayTopicContent(id);
      }

      function getCurrentUserId() {
        return "<%=userId%>";
      }

      function getWebContext() {
        return "<%=m_context%>";
      }

      function getComponentId() {
        return "${componentId}";
      }

      function getComponentLabel() {
        return "<%=WebEncodeHelper.javaStringToJsString(componentLabel)%>";
      }

      function getLanguage() {
        return "<%=language%>";
      }

      function getPubIdToHighlight() {
        return "<%=pubIdToHighlight%>";
      }

      function getTranslation() {
        return "<%=translation%>";
      }

      function getToValidateFolderId() {
        return "<%=KmeliaHelper.SPECIALFOLDER_TOVALIDATE%>";
      }

      function getNonVisiblePubsFolderId() {
        return "<%=KmeliaHelper.SPECIALFOLDER_NONVISIBLEPUBS%>";
      }

      function isSpecialFolder(id) {
        return id === getToValidateFolderId() || id === getNonVisiblePubsFolderId();
      }

      function focusOnSearch()
      {
        $("#topicQuery").focus();
      }

      function _isInBin(nodeId) {
        return isParentNode(binNodeId, nodeId);
      }

      function nodeDeleted(nodeId, isInBin) {

      }
    </script>
  </view:sp-head-part>

  <view:sp-body-part cssClass="yui-skin-sam treeView" id="${componentId}">
    <div compile-directive style="display: none"></div>
    <div class="<%=profile%>">
      <%
        Window window = gef.getWindow();
        BrowseBar browseBar = window.getBrowseBar();
        browseBar.setI18N("GoToCurrentTopic", translation);

        //Display operations - following lines are mandatory to init menu correctly
        OperationPane operationPane = window.getOperationPane();
        operationPane.addOperation("", resources.getString("FavoritesAdd1")+" "+kmeliaScc.getString("FavoritesAdd2"), "javaScript:addCurrentNodeAsFavorite()");

        out.println(window.printBefore());
      %>
      <view:frame>
        <div id="subTopics"></div>

        <kmelia:searchZone enabled="${displaySearch}"/>

        <div id="topicDescription" class="rich-content"></div>
        <view:areaOfOperationOfCreation/>
        <div class="dragAndDropUpload" style="min-height: 75px">
          <div id="pubList">
            <br/>
            <view:board>
              <br/>
              <div class="center"><%=resources.getString("kmelia.inProgressPublications") %>
                <br/><br/><img alt='progress' src="<%=resources.getIcon("kmelia.progress") %>"/></div>
              <br/>
            </view:board>
          </div>
        </div>
        <div id="footer" class="txtBaseline"></div>
      </view:frame>
      <%
        out.println(window.printAfter());
      %>

      <form name="topicDetailForm" method="post">
        <input type="hidden" name="Id" value="${currentFolderId}"/>
        <input type="hidden" name="ChildId"/>
        <input type="hidden" name="Status"/>
        <input type="hidden" name="Recursive"/>
      </form>

      <form name="pubForm" action="ViewPublication" method="GET">
        <input type="hidden" name="PubId"/>
        <input type="hidden" id="CheckPath" name="CheckPath"/>
      </form>

      <form name="fupload" action="fileUpload.jsp" method="post" enctype="multipart/form-data" accept-charset="UTF-8">
        <input type="hidden" name="Action" value="initial"/>
      </form>

      <script type="text/javascript">
        const icons = {};
        icons["permalink"] = "<%=resources.getIcon("kmelia.link")%>";
        icons["operation.addTopic"] = "<%=resources.getIcon("kmelia.operation.addTopic")%>";
        icons["operation.addPubli"] = "<%=resources.getIcon("kmelia.operation.addPubli")%>";
        icons["operation.importFile"] = "<%=resources.getIcon("kmelia.operation.importFile")%>";
        icons["operation.importFiles"] = "<%=resources.getIcon("kmelia.operation.importFiles")%>";
        icons["operation.subscribe"] = "<%=resources.getIcon("kmelia.operation.subscribe")%>";
        icons["operation.favorites"] = "<%=resources.getIcon("kmelia.operation.favorites")%>";

        const params = {};
        params["rightsOnTopic"] = <%=rightsOnTopics.booleanValue()%>;
        params["i18n"] = <%=I18NHelper.isI18nContentActivated()%>;
        params["nbPublisDisplayed"] = <%=displayNBPublis%>;

        let searchInProgress = <%=searchContext != null%>;
        let searchFolderId = "${currentFolderId}";

        function getComponentPermalink() {
          return "<%=URLUtil.getSimpleURL(URLUtil.URL_COMPONENT, componentId)%>";
        }

        function copyCurrentNode()  {
          top.IdleFrame.location.href = '../..<%=kmeliaScc.getComponentUrl()%>copy?Object=Node&Id='+getCurrentNodeId();
        }

        function cutCurrentNode() {
          top.IdleFrame.location.href = '../..<%=kmeliaScc.getComponentUrl()%>cut?Object=Node&Id='+getCurrentNodeId();
        }

        function changeCurrentTopicStatus() {
          changeStatus(getCurrentNodeId(), getCurrentTopicStatus());
        }

        function updateUIStatus(nodeId, newStatus) {
          setCurrentTopicStatus(newStatus);
          displayOperations(nodeId);
        }

        function displayTopicContent(id) {
          if (id !== searchFolderId) {
            // search session is over
            searchInProgress = false;
          }

          if (!searchInProgress) {
            clearSearchQuery();
          }

          setCurrentNodeId(id);

          if (isSpecialFolder(id) || id === binNodeId) {
            muteDragAndDrop(); //mute dropzone
            $("#footer").css({'visibility':'hidden'}); //hide footer
            $("#searchZone").css({'display':'none'}); //hide search
            $("#subTopics").empty();
            if (id === getToValidateFolderId()) {
              hideOperations();

              //update breadcrumb
              removeBreadCrumbElements();
              addBreadCrumbElement("#", "<%=resources.getString("ToValidate")%>");
            } else if (id === getNonVisiblePubsFolderId())  {
              hideOperations();

              //update breadcrumb
              removeBreadCrumbElements();
              addBreadCrumbElement("#", "<%=resources.getString("kmelia.folder.nonvisiblepubs")%>");
            } else {
              displayPath(id);
              displayOperations(id);
            }

            displayPublications(id);

            if (id === binNodeId) {
              displaySubTopics(id);
            }

          } else {
            if (searchInProgress) {
              doPagination(<%=currentPageIndex%>);
            } else {
              displayPublications(id);
            }
            displayPath(id);
            displayOperations(id);
            $("#searchZone").css({'display':'block'});
            displaySubTopics(id);
          }

          //display topic information
          displayTopicInformation(id);

          //display topic rich description
          displayTopicDescription(id);

          if (isSearchTopicEnabled) {
            setTimeout(focusOnSearch,500);
          }

        }


        function displaySubTopics(id) {
          const sUrl = "<%=m_context%>/services/folders/<%=componentId%>/"+id+"/children?lang="+getTranslation();
          $.ajax(sUrl, {
            type: 'GET',
            dataType : 'json',
            async : false,
            cache : false,
            success : function(data){
              $("#subTopics").empty();
              $("#subTopics").append("<ul>");
              let basket = "";
              let tovalidate = "";
              let nonVisiblePubs = "";
              $.each(data, function(i, folder) {
                const folderId = folder.attr["id"];
                if (folderId === binNodeId) {
                  basket = getSubFolder(folder);
                } else if (folderId === getToValidateFolderId()) {
                  tovalidate = getSubFolder(folder);
                } else if (folderId === getNonVisiblePubsFolderId()) {
                  nonVisiblePubs = getSubFolder(folder);
                } else {
                  $("#subTopics ul").append(getSubFolder(folder));
                }
              });
              if (id === rootNodeId) {
                $("#subTopics ul").append(tovalidate);
                $("#subTopics ul").append(nonVisiblePubs);
                $("#subTopics ul").append(basket);
              }
              $("#subTopics").append("</ul>");
              if ($("#subTopics ul li").length > 0) {
                $("#subTopics").append("<br >");
              } else {
                $("#subTopics").empty();
              }
            },
            error : function(data) {
              //alert("error");
            }
          });
        }

        function getSubFolder(folder) {
          const id = folder.attr["id"];
          const nbItems = folder.attr["nbItems"];
          const name = folder.text;
          const desc = folder.attr["description"];
          const folderType = folder["type"];

          let str = '<li id="topic_' + id + '">';
          str += '<a href="#" onclick="topicGoTo(\''+id+'\')" ';
          if (id === getToValidateFolderId()) {
            str += 'class="toValidate"';
          } else if (id === getNonVisiblePubsFolderId()) {
            str += 'class="nonVisiblePubs"';
          } else if (id === binNodeId) {
            str += 'class="trash"';
          } else if (folderType === '<%=NodeType.FOLDER_WITH_RIGHTS%>') {
            str += 'class="folder-with-rights"';
          }
          str += '>';
          str += '<strong>'+name+' ';
          if (nbItems && typeof(nbItems) !== "undefined") {
            str += '<span>'+nbItems+'</span>';
          }
          str += '</strong>';
          if (typeof(desc) !== "undefined" && desc.length > 0) {
            str += '<span title="'+desc+'">'+desc+'</span>';
          }
          str += '</a>';
          str += '</li>';
          return str;
        }

        function getString(key) {
          return sp.i18n.get(key);
        }
      </script>
    </div>
    <div id="visibleInvisible-message" style="display: none;">
      <p>
      </p>
    </div>
    <div id="addOrUpdateNode" style="display: none;">
      <form name="topicForm" action="AddTopic" method="post">
        <input type="hidden" id="<%=I18NHelper.HTML_HIDDEN_REMOVED_TRANSLATION_MODE %>" name="<%=I18NHelper.HTML_HIDDEN_REMOVED_TRANSLATION_MODE %>" value="false"/>
        <table>
          <th></th>
          <tr><td class="txtlibform"><fmt:message key="TopicPath"/> :</td>
            <td id="path"></td>
          </tr>
          <%=I18NHelper.getFormLine(resources, null, kmeliaScc.getLanguage())%>
          <tr>
            <td class="txtlibform"><fmt:message key="TopicTitle"/> :</td>
            <td><input type="text" name="Name" id="folderName" maxlength="60"/>
              <input type="hidden" name="ParentId" id="parentId"/>
              <input type="hidden" name="ChildId" id="topicId"/>&nbsp;<img alt="mandatory" src="<c:out value='${mandatoryFieldUrl}' />" width="5" height="5"/></td>
          </tr>

          <tr>
            <td class="txtlibform"><fmt:message key="TopicDescription" /> :</td>
            <td><input type="text" name="Description" id="folderDescription" maxlength="200"/></td>
          </tr>

          <% if (kmeliaScc.isNotificationAllowed()) { %>
          <tr>
            <td class="txtlibform"><fmt:message key="TopicAlert" /> :</td>
            <td>
              <select name="AlertType">
                <option value="NoAlert" selected="selected"><fmt:message key="NoAlert" /></option>
                <option value="Publisher"><fmt:message key="OnlyPubsAlert" /></option>
                <option value="All"><fmt:message key="AllUsersAlert" /></option>
              </select>
            </td>
          </tr>
          <% } %>
          <tr>
            <td colspan="2"><img alt="mandatory" src="<c:out value='${mandatoryFieldUrl}' />" width="5" height="5"/> : <fmt:message key="GML.requiredField"/></td>
          </tr>
        </table>
      </form>
    </div>

    <%@ include file="../../sharing/jsp/createTicketPopin.jsp" %>
    <view:progressMessage/>
    <kmelia:paste highestUserRole="${highestUserRole}" componentInstanceId="<%=componentId%>" />
    <kmelia:dragAndDrop highestUserRole="${highestUserRole}" componentInstanceId="<%=componentId%>" contentLanguage="<%=translation%>" />
    <script type="text/javascript">
      /* declare the module myapp and its dependencies (here in the silverpeas module) */
      window.myapp = angular.module('silverpeas.kmelia', ['silverpeas.services', 'silverpeas.directives']);
    </script>
  </view:sp-body-part>
</view:sp-page>