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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
***REMOVED***
import java.io.PrintWriter;

import org.apache.commons.io.IOUtils;
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.AnsibleHost;
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.HostGroup;
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.Inventory;
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.Playbook;
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.Role;
import org.gcube.dataanalysis.dataminer.poolmanager.ansible.model.RoleFile;

public class AnsibleSerializeHelper ***REMOVED***
  
  public static void serialize(Inventory inventory, File inventoryFile) throws IOException ***REMOVED***
    String out = "";
    for(HostGroup hg:inventory.getHostGroups()) ***REMOVED***
      out+=String.format("[%s]\n", hg.getName());
      for(AnsibleHost h:hg.getHosts()) ***REMOVED***
        out+=h.getName()+"\n";
      ***REMOVED***
      out+="\n";
    ***REMOVED***
    out = out.trim();
    serialize(out, inventoryFile);
  ***REMOVED***
  
  public static void serialize(Playbook playbook, File playbookFile) throws IOException ***REMOVED***
    String out = "- hosts: " + playbook.getHostGroupName() +  "\n";
    out += "  remote_user: "+playbook.getRemote_user()+"\n";
    out+="  roles:\n";
    for(String r:playbook.getRoles()) ***REMOVED***
      out+="    - " + r+"\n";
    ***REMOVED***
    out+="  vars:\n";
    out+="    os_package_state: present\n";
    out = out.trim();
    serialize(out, playbookFile);
  ***REMOVED***

  public static void serializeRole(Role r, File dir) throws IOException ***REMOVED***
    ***REMOVED*** create root
    File root = new File(dir, r.getName());
    root.mkdirs();
    
    ***REMOVED*** create tasks
    if(r.getTaskFiles().size()>0) ***REMOVED***
      File tasks = new File(root, "tasks");
      tasks.mkdirs();
      for(RoleFile tf: r.getTaskFiles()) ***REMOVED***
        serializeTask(tf, tasks);
      ***REMOVED***
    ***REMOVED***
    
    ***REMOVED*** create meta
    if(r.getMeta().size()>0) ***REMOVED***
      File meta = new File(root, "meta");
      meta.mkdirs();
      for(RoleFile tf: r.getMeta()) ***REMOVED***
        serializeTask(tf, meta);
      ***REMOVED***
    ***REMOVED***
  ***REMOVED***

  public static void serializeTask(RoleFile tf, File dir) throws IOException ***REMOVED***
    File f = new File(dir, tf.getName());
    serialize(tf.getContent().trim(), f);
  ***REMOVED***

  public static void serialize(String s, File f) throws IOException ***REMOVED***
    PrintWriter out = new PrintWriter(f);
    out.println(s);
    out.close();
  ***REMOVED***
  
  public static Role deserializeRoleFromFilesystem(File roleDir) throws IOException ***REMOVED***
    Role out = new Role();
    out.setName(roleDir.getName());
    
    if(!roleDir.exists()) ***REMOVED***
      throw new FileNotFoundException();
    ***REMOVED***
    
    try ***REMOVED***
      File tasksDir = new File(roleDir, "tasks");
      if(tasksDir.exists()) ***REMOVED***
        for(File main:tasksDir.listFiles()) ***REMOVED***
          String content = IOUtils.toString(new FileInputStream(main), "UTF-8");
          RoleFile tf = new RoleFile(main.getName(), content);
          tf.setPath(main.getAbsolutePath().substring(roleDir.getAbsolutePath().length()+1));
          out.addTaskFile(tf);
        ***REMOVED***
      ***REMOVED***
    ***REMOVED*** catch(FileNotFoundException e) ***REMOVED***
      e.printStackTrace();
    ***REMOVED***
    
    try ***REMOVED***
      File metaDir = new File(roleDir, "meta");
      if(metaDir.exists()) ***REMOVED***
        for(File main:metaDir.listFiles()) ***REMOVED***
          String content = IOUtils.toString(new FileInputStream(main), "UTF-8");
          RoleFile tf = new RoleFile(main.getName(), content);
          tf.setPath(main.getAbsolutePath().substring(roleDir.getAbsolutePath().length()+1));
          out.addMeta(tf);
        ***REMOVED***
      ***REMOVED***
    ***REMOVED*** catch(FileNotFoundException e) ***REMOVED***
      e.printStackTrace();
    ***REMOVED***
    
    return out;
  ***REMOVED***
  
***REMOVED***
