package org.gcube.dataanalysis.dataminerpoolmanager;

***REMOVED***
import java.io.InputStream;
import java.net.ProxySelector;
***REMOVED***
***REMOVED***
import java.util.Vector;

***REMOVED***
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.AlgorithmSet;
import org.gcube.dataanalysis.dataminer.poolmanager.process.AddAlgorithmCommand;
import org.gcube.dataanalysis.dataminer.poolmanager.process.AlgorithmPackageParser;
import org.gcube.dataanalysis.dataminer.poolmanager.util.PropertiesBasedProxySelector;

public class AlgorithmPackageParserTest ***REMOVED***

  private static int BUFFER_SIZE = 2048;

  public void extractAllAlgorithms() throws IOException ***REMOVED***
    String url = "http:***REMOVED***svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/DataMinerConfiguration/algorithms/dev/algorithms";
    List<String> commands = this.extractAddAlgorithmCommands(url);
    AlgorithmSet algorithms = new AlgorithmSet();
    for (String cmd : commands) ***REMOVED***
      System.out.println("-----------------------------------------");
      System.out.println(cmd);
      AddAlgorithmCommand aac = new AddAlgorithmCommand(cmd);
      System.out.println(aac);

      ***REMOVED*** start creating the algo from the command
      Algorithm algo = new Algorithm();
      algo.setAlgorithmType(aac.getAlgorithmType());
      algo.setCategory(aac.getCategory());
      algo.setClazz(aac.getClazz());
      algo.setDescription(aac.getDescription());
      algo.setName(aac.getName());
      algo.setPackageURL(aac.getUrl());
      algo.setSkipJava(aac.getSkipjava());

      ***REMOVED*** then override with info from the package
      if (aac.getUrl().length() > 4) ***REMOVED***
        Algorithm packagedAlgo = this.extractAlgorithm(aac.getUrl());
        if (packagedAlgo != null) ***REMOVED***
          algo.setDependencies(packagedAlgo.getDependencies());
        ***REMOVED***
      ***REMOVED***
      algorithms.addAlgorithm(algo);
      break;
    ***REMOVED***
   ***REMOVED***to uncomment
   ***REMOVED*** new DataminerPoolManager().addAlgorithmsToVRE(algorithms,
     ***REMOVED***   "/gcube/devNext/NextNext");
  ***REMOVED***

  /**
   * Extract 'addAlgorithm' commands from a file containing wiki-table-style
   * entries for algorithm.
   * 
   * @return
   * @throws IOException
   */
  private List<String> extractAddAlgorithmCommands(String listUrl)
      throws IOException ***REMOVED***
    URL url = new URL(listUrl);
    InputStream is = url.openStream();

    StringBuilder s = new StringBuilder();
    byte[] buffer = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = is.read(buffer)) != -1) ***REMOVED***
      s.append(new String(buffer, 0, read));
    ***REMOVED***
    List<String> out = new Vector<>();
    String[] lines = s.toString().split("\n");
    for (String line : lines) ***REMOVED***
      System.out.println("--------------------");
      if (!line.isEmpty()) ***REMOVED***
        String[] parts = line.split("\\|");
        int c = 1;
        for (String part : parts) ***REMOVED***
          if (part == null || part.trim().isEmpty()) ***REMOVED***
            continue;
          ***REMOVED***
          System.out.println(c + ". " + part);
          c++;
          if (part.contains("addAlgorithm.sh")) ***REMOVED***
            String cmd = part.trim();
            cmd = cmd.replaceAll("<notextile>", "");
            cmd = cmd.replaceAll("</notextile>", "");
            System.out.println(cmd);
            ***REMOVED*** AddAlgorithmCommand aac = new AddAlgorithmCommand(cmd);
            ***REMOVED*** System.out.println(aac);
            out.add(cmd);
          ***REMOVED***
        ***REMOVED***
      ***REMOVED***
    ***REMOVED***
    return out;
  ***REMOVED***

  /**
   * Create an Algorithm starting from the algorithm jar.
   * 
   * @param url
   * @return
   * @throws IOException
   */
  private Algorithm extractAlgorithm(String url) throws IOException ***REMOVED***
    return new AlgorithmPackageParser().parsePackage(url);
  ***REMOVED***

  public static void main(String[] args) throws Exception ***REMOVED***
***REMOVED***    ProxySelector.setDefault(new PropertiesBasedProxySelector(
***REMOVED***        "/home/ngalante/.proxy-settings"));

    new AlgorithmPackageParserTest().extractAllAlgorithms();
***REMOVED***    AlgorithmPackageParserTest at = new AlgorithmPackageParserTest();
***REMOVED***    Algorithm a = at.extractAlgorithm("http:***REMOVED***data.d4science.org/YjJ3TmJab1dqYzVoTmppdjlsK0l0b1ZXWGtzWlQ1NHNHbWJQNStIS0N6Yz0");
***REMOVED***    System.out.println(a.getFullname());
  ***REMOVED***

***REMOVED***
