/*
 * Copyright 2011 Corpuslinguistic working group Humboldt University Berlin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.gui;

import annis.gui.beans.CitationProvider;
import annis.gui.objects.ContextualizedQuery;
import annis.gui.objects.Query;
import annis.gui.objects.QueryGenerator;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.themes.BaseTheme;
import com.vaadin.ui.themes.ValoTheme;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author thomas
 */
public class CitationLinkGenerator implements Table.ColumnGenerator,
  Button.ClickListener
{
  @Override
  public Object generateCell(Table source, Object itemId, Object columnId)
  {
    Button btLink = new Button();
    btLink.addStyleName(ValoTheme.BUTTON_BORDERLESS);
    btLink.setIcon(FontAwesome.SHARE_ALT);
    btLink.setDescription("Share query reference link");
    btLink.addClickListener(this);

    if(itemId instanceof CitationProvider)
    {
      final CitationProvider citationProvider = (CitationProvider) itemId;
      btLink.addClickListener(new LinkClickListener(citationProvider));
    }
    else if(itemId instanceof Query)
    {
      final CitationProvider citationProvider = new CitationProviderForQuery((Query) itemId);
      btLink.addClickListener(new LinkClickListener(citationProvider));
    }

    return btLink;
  }

  @Override
  public void buttonClick(ClickEvent event)
  {
  }
  
  private static class CitationProviderForQuery implements CitationProvider
  {
    private final Query query;

    public CitationProviderForQuery(Query query)
    {
      this.query = query;
    }

    @Override
    public String getQuery()
    {
      if(query == null)
      {
        return null;
      }
      return query.getQuery();
    }

    @Override
    public Set<String> getCorpora()
    {
      if(query == null)
      {
        return new HashSet<>();
      }
      return query.getCorpora();
    }

    @Override
    public int getLeftContext()
    {
      if(query instanceof ContextualizedQuery)
      {
        return ((ContextualizedQuery) query).getLeftContext();
      }
      return 5;
    }

    @Override
    public int getRightContext()
    {
      if(query instanceof ContextualizedQuery)
      {
        return ((ContextualizedQuery) query).getRightContext();
      }
      return 5;
    }
    
    
  }

  private static class LinkClickListener implements Button.ClickListener, Serializable
  {

    private final CitationProvider citationProvider;

    public LinkClickListener(CitationProvider citationProvider)
    {
      this.citationProvider = citationProvider;
    }

    @Override
    public void buttonClick(ClickEvent event)
    {

      if(citationProvider != null)
      {
        ShareQueryReferenceWindow c
          = new ShareQueryReferenceWindow(
            QueryGenerator.displayed()
            .query(citationProvider.getQuery())
            .corpora(citationProvider.getCorpora())
            .left(citationProvider.getLeftContext())
            .right(citationProvider.getRightContext())
            .offset(0)
            .limit(10)
            .build());
        UI.getCurrent().addWindow(c);
        c.center();
      }
      else
      {
        Notification.show("Internal error",
          "No valid citation link was found",
          Notification.Type.WARNING_MESSAGE);
      }
      
    }
  }
}

@@ -739,24 +739,71 @@
 lizedQuery;%0A
+import annis.gui.objects.DisplayedResultQuery;%0A
 import annis
@@ -1652,16 +1652,21 @@
 this);%0A%0A
+    %0A
     if(i
@@ -1682,32 +1682,170 @@
 tanceof 
-CitationProvider
+DisplayedResultQuery)%0A    %7B%0A      btLink.addClickListener(new LinkClickListener((DisplayedResultQuery) itemId));%0A    %7D%0A    else if(itemId instanceof Query
 )%0A    %7B%0A
@@ -1892,17 +1892,20 @@
 vider = 
-(
+new 
 Citation
@@ -1912,24 +1912,39 @@
 Provider
+ForQuery((Query
 ) itemId
 ;%0A      
@@ -1935,16 +1935,17 @@
 ) itemId
+)
 ;%0A      
@@ -2038,37 +2038,48 @@
 emId instanceof 
-Qu
+CitationProvid
 er
-y
 )%0A    %7B%0A      fi
@@ -2110,36 +2110,33 @@
 ationProvider = 
-new 
+(
 CitationProvider
@@ -2135,39 +2135,24 @@
 Provider
-ForQuery((Query
 ) itemId
 );%0A     
@@ -2135,33 +2135,32 @@
 Provider) itemId
-)
 ;%0A      btLink.a
@@ -3393,16 +3393,62 @@
 rovider;
+%0A    private final DisplayedResultQuery query;
 %0A%0A    pu
@@ -3567,83 +3567,433 @@
     
-%7D%0A%0A    @Override%0A    public void buttonClick(ClickEvent event)%0A    %7B%0A%0A     
+  this.query = null;%0A    %7D%0A    %0A    public LinkClickListener(DisplayedResultQuery query)%0A    %7B%0A      this.citationProvider = null;%0A      this.query = query;%0A    %7D%0A%0A    @Override%0A    public void buttonClick(ClickEvent event)%0A    %7B%0A%0A      if(query != null)%0A      %7B%0A        ShareQueryReferenceWindow c%0A          = new ShareQueryReferenceWindow(query);%0A        UI.getCurrent().addWindow(c);%0A        c.center();%0A      %7D%0A      else
  if(

handle complete queries (DisplayResultQuery) in the history