/*
 * Copyright 2013 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.docbrowser;

import annis.gui.SearchUI;
import annis.libgui.Helper;
import annis.libgui.PluginSystem;
import annis.libgui.visualizers.VisualizerInput;
import annis.libgui.visualizers.VisualizerPlugin;
import com.sun.jersey.api.client.WebResource;
import com.vaadin.server.ClientConnector;
import com.vaadin.ui.Component;
import com.vaadin.ui.TabSheet;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.SaltProject;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sCorpusStructure.SDocument;
import java.io.Serializable;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Represents a global controller for the doc browser feature.
 *
 * @author Benjamin Weißenfels <b.pixeldrama@gmail.com>
 */
public class DocBrowserController implements Serializable
{

  private Logger log = LoggerFactory.getLogger(DocBrowserController.class);

  // holds the complete state of the gui
  private transient final SearchUI ui;

  // track the already initiated doc browsers
  private transient final Map<String, Component> initedDocBrowsers;

  // cache for already initiated visualizations, the key is the doc name
  private transient Map<String, Component> initiatedVis;

  public DocBrowserController(SearchUI ui)
  {
    this.ui = ui;
    this.initedDocBrowsers = new HashMap<String, Component>();
    this.initiatedVis = new HashMap<String, Component>();
  }

  public void openDocVis(String corpus, String doc)
  {
    String canonicalTitle = "doc view: " + corpus + " > " + doc;
    if (initiatedVis.containsKey(canonicalTitle))
    {
      TabSheet.Tab addTab = ui.getTabSheet().addTab(initiatedVis.get(
        canonicalTitle), canonicalTitle);
      ui.getTabSheet().setSelectedTab(addTab);
      addTab.setClosable(true);
    }
    else
    {
      VisualizerPlugin visualizer = ((PluginSystem) ui).getVisualizer(
        "grid_tree");
      Component vis = visualizer.createComponent(createInput(corpus, doc), null);
      TabSheet.Tab visTab = ui.getTabSheet().addTab(vis, canonicalTitle);
      visTab.setClosable(true);
      ui.getTabSheet().setSelectedTab(vis);
      initiatedVis.put(canonicalTitle, vis);
    }
  }

  public void openDocBrowser(String corpus)
  {
    String caption = "doc browser " + corpus;

    // if not already init, do it now
    if (!initedDocBrowsers.containsKey(corpus))
    {
      DocBrowserPanel browseTbl = DocBrowserPanel.
        initDocBrowserPanel(ui, corpus);
      initedDocBrowsers.put(corpus, browseTbl);
    }

    // init tab and put to front
    ui.getTabSheet().addTab(initedDocBrowsers.get(corpus), caption);
    TabSheet.Tab tab = ui.getTabSheet().addTab(initedDocBrowsers.get(corpus),
      caption);
    tab.setClosable(true);
    ui.getTabSheet().setSelectedTab(tab);
  }

  private VisualizerInput createInput(String corpus, String docName)
  {
    VisualizerInput input = new VisualizerInput();

    // get the whole document wrapped in a salt project
    SaltProject txt = null;
    try
    {
      String topLevelCorpusName = URLEncoder.encode(corpus, "UTF-8");
      docName = URLEncoder.encode(docName, "UTF-8");
      WebResource annisResource = Helper.getAnnisWebResource();
      txt = annisResource.path("query").path("graphs").path(topLevelCorpusName).
        path(docName).get(SaltProject.class);
    }
    catch (RuntimeException e)
    {
      log.error("General remote service exception", e);
    }
    catch (Exception e)
    {
      log.error("General remote service exception", e);
    }

    if (txt != null)
    {
      SDocument sDoc = txt.getSCorpusGraphs().get(0).getSDocuments().get(0);
      input.setResult(sDoc);
    }

    // set empty mapping for avoiding errors with pure written visualizers
    input.setMappings(new Properties());
    return input;
  }
}

@@ -2278,276 +2278,120 @@
 oc;%0A
+%0A
     
-if (initiatedVis.containsKey(canonicalTitle))%0A    %7B%0A      TabSheet.Tab addTab = ui.getTabSheet().addTab(initiatedVis.get(%0A        canonicalTitle), canonicalTitle);%0A      ui.getTabSheet().setSelectedTab(addTab);%0A      addTab.setClosable(true);%0A    %7D%0A    else
+// check if a visualization is already initiated%0A    if (!initiatedVis.containsKey(canonicalTitle))
 %0A    %7B%0A
+%0A
     
@@ -2559,24 +2559,128 @@
 ), null);%0A  
+    initiatedVis.put(canonicalTitle, vis);%0A    %7D%0A%0A    Component vis = initiatedVis.get(canonicalTitle);%0A
     TabSheet
@@ -2743,18 +2743,16 @@
 e);%0A    
- 
- 
 visTab.s
@@ -2769,18 +2769,16 @@
 (true);%0A
-  
     ui.g
@@ -2815,59 +2815,8 @@
 s);%0A
-      initiatedVis.put(canonicalTitle, vis);%0A    %7D%0A
   %7D%0A

Refactor visualizer caching for doc browser.