package org.gcube.dataanalysis.dataminer.poolmanager.ansiblebridge.template;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Vector;

***REMOVED***
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.Role;
***REMOVED***
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency;

public class AlgorithmPackage ***REMOVED***

  private Algorithm algorithm;
  private boolean includeAlgorithmDependencies;
  public AlgorithmPackage(Algorithm a,boolean includeAlgorithmDependencies) ***REMOVED***
    this.algorithm = a;
    this.includeAlgorithmDependencies = includeAlgorithmDependencies;
    
  ***REMOVED***
  
  
  protected Map<String, String> getDictionary(Algorithm a) ***REMOVED***
    Map<String, String> out = new HashMap<String, String>();
    out.put("name", a.getName());
    out.put("category", a.getCategory());
    out.put("class", a.getClazz());
    out.put("atype", a.getAlgorithmType());
    out.put("skipjava", a.getSkipJava());
    out.put("vre", ScopeProvider.instance.get());
    ***REMOVED***out.put("vre", "FAKE_VRE");
    out.put("packageurl", a.getPackageURL());
    out.put("description", a.getDescription());
    String deps = "";
    
    if(includeAlgorithmDependencies)***REMOVED***
    for(Dependency d:a.getDependencies()) ***REMOVED***
      deps+=String.format("- ***REMOVED*** role: %s ***REMOVED***\n", d.getType()+"-"+d.getName().replaceAll("/", "-"));
    ***REMOVED******REMOVED***
    deps = deps.trim();
    out.put("dependencies", deps);
    return out;
  ***REMOVED***
  
  protected Algorithm getAlgorithm() ***REMOVED***
    return this.algorithm;
  ***REMOVED***
  
  public Collection<Role> getRoles(TemplateManager tm) ***REMOVED***
    Collection<Role> out = new Vector<>();
    for(String mode:new String[]***REMOVED***"add"***REMOVED***) ***REMOVED***  ***REMOVED*** "remove", "update"
      String roleName = "gcube-algorithm-"+this.getAlgorithm().getName()+("add".equals(mode) ? "" : "-"+mode);
      try ***REMOVED***
        ***REMOVED*** find template
        Role template = tm.getRoleTemplate("gcube-algorithm-" + mode);
        ***REMOVED*** 
        if(template!=null) ***REMOVED***
          Map<String, String> dictionary = this.getDictionary(this.getAlgorithm());
          Role r = tm.fillRoleTemplate(template, dictionary);
          r.setName(roleName);
          out.add(r);
        ***REMOVED*** else ***REMOVED***
          System.out.println("WARNING: template is null");
        ***REMOVED***
      ***REMOVED*** catch (NoSuchElementException e) ***REMOVED***
***REMOVED***        e.printStackTrace();
        System.out.println("WARNING: no template found for " + roleName);
      ***REMOVED***
    ***REMOVED***
    return out;
  ***REMOVED***

***REMOVED***
