<%
/*
<!--

    Copyright (C) 2003-2007 eXo Platform SAS.

    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.

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

    You should have received a copy of the GNU General Public License
    along with this program; if not, see<http://www.gnu.org/licenses/>.
link
-->
*/
%>
<%
  import org.exoplatform.webui.form.UIFormStringInput;
  import org.exoplatform.wcm.ext.component.activity.UILinkUtil;
  import org.apache.commons.lang.StringEscapeUtils;
  import org.exoplatform.social.service.rest.Util;
  
  def uicomponentId = uicomponent.id;
  def labelAttach = _ctx.appRes("UILinkComposerPlugin.label.Attach");

  def linkInfoDisplayed = uicomponent.isLinkInfoDisplayed();
//  uicomponent.setLinkInfoDisplayed(false);
  def requestContext = _ctx.getRequestContext();
  def jsManager = requestContext.getJavascriptManager();

  def noThumbnail = _ctx.appRes("UILinkComposerPlugin.msg.No_Thumbnail"),
      previousImage = _ctx.appRes("UILinkComposerPlugin.msg.Previous_Image"),
      nextImage = _ctx.appRes("UILinkComposerPlugin.msg.Next_Image");

  def linkShare = uicomponent.getLinkShare();
  def link, images, title, description, linkData = "{}";
  
  if (linkShare) {
    link = linkShare.getLink();
    images = linkShare.getImages();
    def image;
    if (images != null && images.size() > 0) {
      image = images.get(0);
    }
    title = linkShare.getTitle();
    description = linkShare.getDescription();
    linkData = "{"+
              "link: '" + UILinkUtil.encodeURI(link) + "',"+
              "title: '" + UILinkUtil.encodeURI(title) + "',"+
              "description: '" + UILinkUtil.encodeURI(description) + "'";

    if(image != null){
      linkData += ",image: '$image'";
    }
    linkData += "}";
  } else if(linkInfoDisplayed){
    link = uicomponent.templateParams.get("link");
    title = uicomponent.templateParams.get("title");
    description = uicomponent.templateParams.get("description");
    
    def image;
    if(uicomponent.templateParams.get("image") == null || uicomponent.templateParams.get("image").isEmpty()) {
      images = [];
    } else {
      images = [uicomponent.templateParams.get("image")];
      image = images.get(0);
    }
    linkData = "{"+
      "link: '" + UILinkUtil.encodeURI(link) + "',"+
      "title: '" + UILinkUtil.encodeURI(title) + "',"+
      "description: '" + UILinkUtil.encodeURI(description) + "'";

    if(image != null){
      linkData += ",image: '$image'";
    }
    linkData += "}";
  } else {
    linkInfoDisplayed = false;
  }
  //
  link = UILinkUtil.simpleEscapeHtml(Util.getDecodeQueryURL(link));
  title = UILinkUtil.simpleEscapeHtml(Util.getDecodeQueryURL(title));

  description = UILinkUtil.simpleEscapeHtml(description);

  def params = "{"+
                  "linkInfoDisplayed: $linkInfoDisplayed,"+
                  "inputLinkId: 'InputLink',"+
                  "attachButtonId: 'AttachButton',"+
                  "attachUrl: '" + UILinkUtil.encodeURI(uicomponent.url("Attach")) + "',"+
                  "changeLinkContentUrl: '" + UILinkUtil.encodeURI(uicomponent.url("ChangeLinkContent")) + "',"+
                  "linkData: " + linkData +
                "}";

  jsManager.require("SHARED/jquery", "jq")
           .require("SHARED/bts_tooltip").addScripts("jq('*[rel=\"tooltip\"]').tooltip();")
           .require("SHARED/social-ui-link-activity-composer", "linkComposer").addScripts("linkComposer.onLoad($params);");
%>

<div id="$uicomponentId">
<% if (linkInfoDisplayed) {
     uicomponent.clearLinkShare();
     def embedHtml = uicomponent.getTemplateParams().get(uicomponent.HTML_PARAM);
     def formDisplay = uicomponent.isDisplayed() ? "block" : "none"; %>

  <div class="uiLinkShareDisplay blastShare" style="display:$formDisplay">
	<a class="uiIconClose uiIconLightGray" href="javascript:void(0)" onclick="<%= uicomponent.event("RemoveLink")%>;" ></a>
    <%
      if(UILinkUtil.isImageLink(link)){
        %>
          <div class="ImageLink">
            <a href="$link" target="_blank" rel="tooltip" data-placement="bottom" title="$title"><img src="$link"  height="120"/></a> 
          </div>
        <%
      }
      else {
        if (embedHtml == null) {
        %>
		<div id="ThumbnailContainer" class="thumbnailContainer clearfix">
          <% if (images != null && images.size() > 0) { %>
			<div id="UIThumbnailLeftBox" class="uiBox thumbnailLeft">
				<div id="UIThumbnailDisplay" class="uiContentBox">
				  <div id="Thumbnails" class="thumbnailInner" rel="tooltip" data-placement="bottom" title="$title">
					<%
					  def thumbnailClass = "thumbnailShown";
					  images.each({ %>
					   <img src="$it" class="$thumbnailClass" />
					  <%
						thumbnailClass = "thumbnailHidden";
					  })%>
				  </div>
				  <div class="thumbnailAction clearfix">
					<div class="actionIconSmall pull-left" id="BackThumbnail">
					  <i class="uiIconArrowLeft uiIconLightGray"></i>
					</div>
					<div class="actionIconSmall pull-right" id="NextThumbnail">
					  <i class="uiIconArrowRight uiIconLightGray"></i>
					</div>				   
					<div id="Stats" class="stats">

					</div>
				  </div>
				</div>
			 </div>			 
          
            <% } %>
          <% } %>
          <div id="UIRightBox" class="contentRight">
            <h5 id="LinkTitle" class="title editable">
				$title
            </h5>
            <% if (embedHtml == null) { 
                 if (description.length() > 273) {
                   description = description.substring(0, 270) + "...";
                 }
            %>
                 
                 <div id="LinkDescription" class="content editable">$description</div>
            <% } %>
            <div id="LinkUrl" class="source">
             <a href="$link" target="_blank">$link</a>
            </div>
            <% if (embedHtml != null) { %>
                 <div id="embedHtml">$embedHtml</div>
            <% } %>			
            <% if (images != null && images.size() > 0) { %>
			      <span class="uiCheckbox"><input id="ThumbnailCheckbox" type="checkbox" class="checkbox" /><span>$noThumbnail</span></span>
			      <% } %>
		  </div>
      <% if (embedHtml == null) { %>
        </div>
      <% } %>
    <% } %>
  </div>
<% } else { 
      def formDisplay = uicomponent.isDisplayed() ? "block" : "none"; %>
      <div id="LinkExtensionContainer" class="uiComposerLink uiGrayLightBox clearfix" style="display:$formDisplay">
        <div class="inputLink"><% uicomponent.renderChild(UIFormStringInput.class); %></div>
        <div class="buttonLink"><button id="AttachButton" class="btn" >$labelAttach</button></div>
      </div>
<% }%>
</div>
