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

import java.io.File;
***REMOVED***
import java.util.Map;
import java.util.NoSuchElementException;

import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.Role;
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.RoleFile;
import org.gcube.dataanalysis.dataminer.poolmanager.ansiblebridge.AnsibleBridge;
import org.gcube.dataanalysis.dataminer.poolmanager.ansiblebridge.AnsibleSerializeHelper;
import org.stringtemplate.v4.ST;

public class TemplateManager ***REMOVED***

  public TemplateManager() ***REMOVED***
    
  ***REMOVED***

  public String getTemplateRoot() ***REMOVED***
	    String input = AnsibleBridge.class.getClassLoader().getResource("templates").getPath();
    return input;
  ***REMOVED***

 
***REMOVED***  private String readTemplate(String templateName) throws IOException ***REMOVED***
***REMOVED***    File templateFile = new File(this.getTemplateRoot(), templateName + ".yaml");
***REMOVED***    System.out.println("looking for file " + templateFile.getName());
***REMOVED***    String out = IOUtils.toString(new FileInputStream(templateFile), "UTF-8");
***REMOVED***    return out;
***REMOVED***  ***REMOVED***

 
***REMOVED***  public String getTemplate(String templateName) throws NoSuchElementException ***REMOVED***
***REMOVED***    String template = null;
***REMOVED***    try ***REMOVED***
***REMOVED***      template = this.readTemplate(templateName);
***REMOVED***    ***REMOVED*** catch (IOException e) ***REMOVED***
***REMOVED***      throw new NoSuchElementException();
***REMOVED***    ***REMOVED***
***REMOVED***    return template;
***REMOVED***  ***REMOVED***
  
  public Role fillRoleTemplate(Role template, Map<String, String> dictionary) ***REMOVED***
    Role out = new Role();
    out.setName(template.getName());
    for(RoleFile tf:template.getTaskFiles()) ***REMOVED***
      out.addTaskFile(this.fillTaskTemplate(tf, dictionary));
    ***REMOVED***
    for(RoleFile tf:template.getMeta()) ***REMOVED***
      out.addMeta(this.fillTaskTemplate(tf, dictionary));
    ***REMOVED***
    return out;
  ***REMOVED***

  private RoleFile fillTaskTemplate(RoleFile template, Map<String, String> dictionary) ***REMOVED***
    RoleFile out = new RoleFile();
    out.setName(template.getName());
    out.setContent(this.fillTemplate(template.getContent(), dictionary));
    return out;
  ***REMOVED***

  private String fillTemplate(String template, Map<String, String> dictionary) ***REMOVED***
    if (template != null) ***REMOVED***
      ST t = new ST(template);
      for (String key : dictionary.keySet()) ***REMOVED***
        t.add(key, dictionary.get(key));
      ***REMOVED***
      String output = t.render();
      return output;
    ***REMOVED***
    return template;
  ***REMOVED***

  public Role getRoleTemplate(String roleName) throws NoSuchElementException ***REMOVED***
    File f = new File(this.getTemplateRoot(), roleName);
    try ***REMOVED***
      return AnsibleSerializeHelper.deserializeRoleFromFilesystem(f);
    ***REMOVED*** catch (IOException e) ***REMOVED***
***REMOVED***      e.printStackTrace();
      throw new NoSuchElementException("unable to find " + roleName);
    ***REMOVED***
  ***REMOVED***

***REMOVED***
