<%
  import java.util.List;
  import javax.jcr.Node;
  import javax.jcr.version.Version;
  import org.exoplatform.services.wcm.publication.lifecycle.stageversion.ui.UIPublicationPanel;
  import org.exoplatform.services.wcm.publication.PublicationDefaultStates;
  import org.exoplatform.ecm.utils.text.Text;
  Node currentRevision = uicomponent.getCurrentRevision();
  Node cNode = uicomponent.getCurrentNode();  
  public boolean isCurrentRevision(String inputStatus, Node revision) throws Exception {    
  	String state = uicomponent.getRevisionState(revision);
    return inputStatus.equals(state) ? true : false;
 }  
%>
<% uicomponent.begin(); %>
<div class="UIPublicationPanel" id="$uicomponent.id">
  <fieldset class="StatusTable">
    <% String versionName = currentRevision.getName();
       String verName = Text.unescapeIllegalJcrChars(versionName);
    %>
    <legend class="Legend" style="max-width: 600px;"><div title="$verName" style="overflow: hidden;"><%=_ctx.appRes("UIPublicationPanel.status.version")%>: $verName </div></legend>
    <div class="StatusAction">
      <div class="StatusTitle"><%=_ctx.appRes("UIPublicationPanel.status.title")%>: </div>
      <div class="StatusLink">
        <%
          if (isCurrentRevision("enrolled", currentRevision)) {
            %>
              <a class="CurrentStatus"><%=_ctx.appRes("UIPublicationPanel.status.enrolled")%></a>
              <span class="StatusArrow"></span>
              <a class="ActiveStatus" href="<%= uicomponent.event("Draft")%>"><%=_ctx.appRes("UIPublicationPanel.status.draft")%></a>
              <span class="StatusArrow"></span>
              <a class="DisableStatus"><%=_ctx.appRes("UIPublicationPanel.status.live")%></a>
              <span class="StatusArrow"></span>
              <a class="DisableStatus"><%=_ctx.appRes("UIPublicationPanel.status.obsolete")%></a>
              <div style="clear: left;"><span></span></div>
            <%
          } else if (isCurrentRevision("draft",currentRevision)) {
            %>
              <a class="DisableStatus"><%=_ctx.appRes("UIPublicationPanel.status.enrolled")%></a>
              <span class="StatusArrow"></span>
              <a class="CurrentStatus"><%=_ctx.appRes("UIPublicationPanel.status.draft")%></a>
              <span class="StatusArrow"></span>
              <a class="ActiveStatus" href="<%= uicomponent.event("Live")%>"><%=_ctx.appRes("UIPublicationPanel.status.live")%></a>
              <span class="StatusArrow"></span>
              <a class="DisableStatus"><%=_ctx.appRes("UIPublicationPanel.status.obsolete")%></a>
              <div style="clear: left;"><span></span></div>              
            <%
          } else if (isCurrentRevision("published",currentRevision)) {
            %>
              <a class="DisableStatus"><%=_ctx.appRes("UIPublicationPanel.status.enrolled")%></a>
              <span class="StatusArrow"></span>
              <a class="DisableStatus"><%=_ctx.appRes("UIPublicationPanel.status.draft")%></a>
              <span class="StatusArrow"></span>
              <a class="CurrentStatus"><%=_ctx.appRes("UIPublicationPanel.status.live")%></a>
              <span class="StatusArrow"></span>
              <a class="ActiveStatus" href="<%= uicomponent.event("obsolete")%>"><%=_ctx.appRes("UIPublicationPanel.status.obsolete")%></a>
              <div style="clear: left;"><span></span></div>
            <%
          } else if (isCurrentRevision("obsolete", currentRevision)) {
            %>
              <a class="DisableStatus"><%=_ctx.appRes("UIPublicationPanel.status.enrolled")%></a>
              <span class="StatusArrow"></span>
              <a class="DisableStatus"><%=_ctx.appRes("UIPublicationPanel.status.draft")%></a>
              <span class="StatusArrow"></span>
              <a class="DisableStatus"><%=_ctx.appRes("UIPublicationPanel.status.live")%></a>
              <span class="StatusArrow"></span>
              <a class="CurrentStatus"><%=_ctx.appRes("UIPublicationPanel.status.obsolete")%></a>
              <div style="clear: left;"><span></span></div>
            <%
          }
        %>
      </div>
    </div>
  </fieldset>
  <table class="RevisionsTable" border="1">
    <tr>
      <td class="Version"><%=_ctx.appRes("UIPublicationPanel.revisions.version")%></td>
      <td class="Date"><%=_ctx.appRes("UIPublicationPanel.revisions.date")%></td>
      <td class="Author"><%=_ctx.appRes("UIPublicationPanel.revisions.author")%></td>
      <td class="Status"><%=_ctx.appRes("UIPublicationPanel.revisions.status")%></td>
      <td class="Action"><%=_ctx.appRes("UIPublicationPanel.revisions.action")%></td>
    </tr>
    <%
      List<Node> revisions = uicomponent.getRevisions();
      if (revisions.isEmpty()) {
        %>
          <tr>
            <td colspan="5" class="None"><%=_ctx.appRes("UIPublicationPanel.revisions.none")%></td>
          </tr>
        <%
      }
         
      for (Node revision : revisions) {
      	def state = uicomponent.getRevisionState(revision);
      	%>
          <tr>
            <td class="Name">
              <%
                if (versionName.equals(revision.getName())) {
                  %>
                  <div title="<%=Text.unescapeIllegalJcrChars(revision.getName())%>" style="width:200px; overflow:hidden; margin-right: 3px;"><%=_ctx.appRes("UIPublicationPanel.revisions.label")%>:<%= Text.unescapeIllegalJcrChars(revision.getName()) %></div>
                  <%
                } else {
                  %>
                  <a class="ActiveLink" href="<%= uicomponent.event("ChangeVersion", revision.getUUID()) %>"><div title="<%=revision.getName()%>" style="width:200px; overflow:hidden; margin-right: 3px;"><%=_ctx.appRes("UIPublicationPanel.revisions.label")%>:<%= Text.unescapeIllegalJcrChars(revision.getName()) %></div></a>
                  <%
                }
              %>
            </td>
            <td><%= uicomponent.getRevisionCreatedDate(revision)%></td>
            <td><%= uicomponent.getRevisionAuthor(revision) %></td>
            <td>
              <%
                if (revisions.indexOf(revision) == 0) {
                  out.print(_ctx.appRes("UIPublicationPanel.status." + uicomponent.getRevisionState(revision)));
                  out.print("[" + _ctx.appRes("UIPublicationPanel.revisions.currentVersion") + "]");
                } else {
                  out.print(_ctx.appRes("UIPublicationPanel.status." + uicomponent.getRevisionState(revision)));
                }
              %>
            </td>
            <td>
              <div title="<%=_ctx.appRes("UIPublicationPanel.title.preview")%>" class="Preview" onclick="<%= uicomponent.event("PreviewVersion", revision.getUUID()) %>"><span></span></div>
              <%
                if (revisions.indexOf(revision) != 0) {
                  if (PublicationDefaultStates.DRAFT.equals(uicomponent.getRevisionState(revisions.get(0)))) {
                    %><div title="<%=_ctx.appRes("UIPublicationPanel.title.restore")%>" class="Restore" onclick="if (confirm('<%=_ctx.appRes("UIPublicationPanel.revisions.confirm")%>')) <%= uicomponent.event("RestoreVersion", revision.getUUID()) %>"><span></span></div><%
                  } else {
                    %><div title="<%=_ctx.appRes("UIPublicationPanel.title.restore")%>" class="Restore" onclick="<%= uicomponent.event("RestoreVersion", revision.getUUID()) %>"><span></span></div><%
                  }
                }
              %>
              <div style="clear: left"><span></span></div>
            </td>
          </tr>
        <%
      }
    %>  
  </table>
  <%
    List<Node> allRevisions = uicomponent.getAllRevisions(cNode);
    if (allRevisions.size() > 3) {
      %>
        <tr>
          <div class="SeeAllVersion" onclick="<%= uicomponent.event("SeeAllVersion") %>"><%=_ctx.appRes("UIPublicationPanel.revisions.seeall")%></div>
        </tr>
      <%
    }
  %>
  <div class="UIAction">
	    <a href="<%= uicomponent.event("Close") %>" class="ActionButton LightBlueStyle">              
	        <%=_ctx.appRes("UIPublicationPanel.button.Close")%>                 
	    </a>
	</div>
</div>
<% uicomponent.end(); %>