package org.rabix.bindings.cwl.bean.resource.requirement;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.rabix.bindings.cwl.bean.resource.CWLResource;
import org.rabix.bindings.cwl.bean.resource.CWLResourceType;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * Requirement that defines environment variables needed for execution 
 */
public class CWLEnvVarRequirement extends CWLResource {

  public final static String KEY_ENV_DEF = "envDef";

  @JsonIgnore
  public List<EnvironmentDef> getEnvironmentDefinitions() {
    List<EnvironmentDef> definitions = new ArrayList<>();

    List<Map<String, Object>> envDefObjs = getValue(KEY_ENV_DEF);
    if (envDefObjs != null) {
      for (Map<String, Object> envDefObj : envDefObjs) {
        String name = (String) envDefObj.get(EnvironmentDef.KEY_NAME);
        Object value = envDefObj.get(EnvironmentDef.KEY_VALUE);
        definitions.add(new EnvironmentDef(name, value));
      }
    }
    return definitions;
  }

  @Override
  public CWLResourceType getType() {
    return CWLResourceType.ENV_VAR_REQUIREMENT;
  }

  /**
   * Environment definition
   */
  public static class EnvironmentDef {

    public final static String KEY_NAME = "envName";
    public final static String KEY_VALUE = "envValue";

    private String name;
    private Object value;

    public EnvironmentDef(String name, Object value) {
      this.name = name;
      this.value = value;
    }

    public String getName() {
      return name;
    }

    public Object getValue() {
      return value;
    }

  }
}

@@ -124,16 +124,44 @@
 til.Map;
+%0Aimport java.util.Map.Entry;
 %0A%0Aimport
@@ -531,16 +531,49 @@
 nIgnore%0A
+  @SuppressWarnings(%22unchecked%22)%0A
   public
@@ -683,16 +683,117 @@
 t%3C%3E();%0A%0A
+    Object envDef = getValue(KEY_ENV_DEF);%0A    if (envDef instanceof List%3C?%3E) %7B    // handle lists%0A  
     List
@@ -831,35 +831,50 @@
 s = 
-getValue(KEY_ENV_DEF)
+(List%3CMap%3CString, Object%3E%3E) envDef
 ;%0A    
+ 
+ 
 if (
@@ -891,24 +891,26 @@
  != null) %7B%0A
+  
       for (M
@@ -950,24 +950,26 @@
 vDefObjs) %7B%0A
+  
         Stri
@@ -1031,24 +1031,26 @@
 E);%0A        
+  
 Object value
@@ -1089,24 +1089,26 @@
 KEY_VALUE);%0A
+  
         defi
@@ -1163,16 +1163,22 @@
     
+  
 %7D%0A    
+ 
+ 
 %7D%0A
+  
     
@@ -1197,16 +1197,406 @@
 itions;%0A
+    %7D%0A    if (envDef instanceof Map%3C?,?%3E) %7B   // handle maps%0A      Map%3CString, Object%3E envDefObjs = (Map%3CString, Object%3E) envDef;%0A      if (envDefObjs != null) %7B%0A        for (Entry%3CString, Object%3E envDefObj : envDefObjs.entrySet()) %7B%0A          definitions.add(new EnvironmentDef(envDefObj.getKey(), envDefObj.getValue()));%0A        %7D%0A      %7D%0A      return definitions;%0A    %7D%0A    return null;%0A
   %7D%0A%0A  @

handle maps for env var requirement