<%
	import java.util.List;
  import java.util.ArrayList;
	import javax.jcr.Node;
	import javax.jcr.version.Version;
	import org.exoplatform.services.wcm.extensions.publication.lifecycle.authoring.ui.UIPublicationPanel;
	import org.exoplatform.services.wcm.publication.PublicationDefaultStates;
	import org.exoplatform.container.ExoContainer;
	import org.exoplatform.container.ExoContainerContext;
	import org.exoplatform.services.security.Identity;
	import org.exoplatform.services.security.IdentityRegistry;
	import org.exoplatform.services.wcm.extensions.publication.impl.PublicationManagerImpl;
	import org.exoplatform.services.wcm.extensions.publication.lifecycle.impl.LifecyclesConfig.Lifecycle;
	import org.exoplatform.services.wcm.extensions.publication.lifecycle.impl.LifecyclesConfig.State;
	import org.exoplatform.ecm.webui.utils.Utils;
	import org.exoplatform.wcm.webui.reader.ContentReader;

	Node currentRevision = uicomponent.getCurrentRevision();
	def context = _ctx.getRequestContext() ;
	String remoteUser=context.getRemoteUser();
	Node cNode = uicomponent.getCurrentNode();
	boolean lastState=false;
	boolean isBaseVersion= false;
	boolean isBeforeCurrentState = true;
	boolean enableState = false;
    List<State> states = uicomponent.getStates(cNode);
	if(currentRevision.getUUID().equals(cNode.getUUID())){
		isBaseVersion=true;
	}
	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">

  <% if (states.size() == 0) { %>
  <%=_ctx.appRes("UIPublicationPanel.msg.no-states")%> 
  <% } %>

  <fieldset class="StatusTable">
    <% String versionName = currentRevision.getName();
       String verName = ContentReader.getXSSCompatibilityContent(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 ClearFix">
        <%
		
 for (State state : states) {
 
	if(states.indexOf(state)==states.size()-1) lastState=true;
		 String membership = state.getMembership();
		 String status = state.getState();
		 boolean isAutomatic = false;
		 if(!"automatic".equals(membership)){
           enableState = uicomponent.canReachState(state, remoteUser, cNode) ;
         }
		 else{
		    isAutomatic=true;
		 }
		 if (isCurrentRevision(status,currentRevision)){		
  			isBeforeCurrentState=false;
  			
  			if(isBaseVersion&&cNode.hasProperty("publication:currentState")&&!cNode.getProperty("publication:currentState").getString().equals(status)){
  			%>
  			<a class="ActiveStatus" href="<%= uicomponent.event("ChangeState", status)%>">
  			<%}
  			else{%>
  			<a class="CurrentStatus">
  			<%}
  			
  			}else if((!isBaseVersion&&isBeforeCurrentState)||isAutomatic||!enableState) {
  			
  			%>
  			<a class="DisableStatus">
  			<%}
  			
  			else  if(!isAutomatic&&enableState){
  			%>	
  			<a class="ActiveStatus" href="<%= uicomponent.event("ChangeState", status)%>">
  			
  			<%}%>
  			<%=_ctx.appRes("UIPublicationPanel.status."+status)%></a>
  			<%if(!lastState){%>
  			
  			<span class="StatusArrow"></span>
  			<%}else{%>
  			 
  			<%}
			
	   }%>
      </div>
    </div>
  </fieldset>
  <fieldset class="StatusTable">
  	<legend class="Legend"><%=_ctx.appRes("UIPublicationPanel.scheduled.title")%></legend>
  	<table class="">
  		<tr>
  			<td><%=_ctx.appRes("UIPublicationPanel.scheduled.from")%>&nbsp;<% uicomponent.renderChild(uicomponent.START_PUBLICATION); %></td>
  			<td><%=_ctx.appRes("UIPublicationPanel.scheduled.to")%>&nbsp;<% uicomponent.renderChild(uicomponent.END_PUBLICATION); %></td>
  		</tr>
  	</table>
  </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);
      	def nodeName = Utils.getTitle(revision);
      	%>
          <tr>
            <td class="Name">
              <%
                if (versionName.equals(revision.getName())) {
                  %>
                    <div title="<%=nodeName%>" style="width:200px; overflow:hidden; margin-right: 3px;"><%=_ctx.appRes("UIPublicationPanel.revisions.label")%>:<%= nodeName %></div>
                  <%
                } else {
                  %>
                    <a class="ActiveLink" href="<%= uicomponent.event("ChangeVersion", revision.getUUID()) %>"><div title="<%=nodeName%>" style="width:200px; overflow:hidden; margin-right: 3px;"><%=_ctx.appRes("UIPublicationPanel.revisions.label")%>:<%= nodeName %></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><%
                  }
                }
              %>
              
            </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"> 
      <% for(action in uicomponent.getActions()) { 
       	 String actionLabel = _ctx.appRes(uicomponent.getName() + ".action." + action) ;
          String link = uicomponent.event(action) ;
 		%>
             <a href="javascript:void(0);" onclick="$link" class="ActionButton LightBlueStyle">$actionLabel</a>
 		<%}%>        
     </div>
</div>
<% uicomponent.end(); %>
