package org.rabix.bindings.cwl.processor.callback;

import java.io.File;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.rabix.bindings.cwl.helper.CWLFileValueHelper;
import org.rabix.bindings.cwl.helper.CWLSchemaHelper;
import org.rabix.bindings.cwl.processor.CWLPortProcessorCallback;
import org.rabix.bindings.cwl.processor.CWLPortProcessorException;
import org.rabix.bindings.cwl.processor.CWLPortProcessorResult;
import org.rabix.bindings.model.ApplicationPort;

public class CWLFileLiteralProcessorCallback implements CWLPortProcessorCallback {

  private File workingDir;
  
  public CWLFileLiteralProcessorCallback(File workingDir) {
    this.workingDir = workingDir;
  }
  
  @Override
  public CWLPortProcessorResult process(Object value, ApplicationPort port) throws Exception {
    if (CWLSchemaHelper.isFileFromValue(value)) {
      String path = CWLFileValueHelper.getPath(value);
      if (path == null) {
        String contents = CWLFileValueHelper.getContents(value);
        if (StringUtils.isEmpty(contents)) {
          throw new CWLPortProcessorException("Cannot process file literal for port " + port.getId());
        }
        String name = "file_literal_" + UUID.randomUUID().toString();
        File file = new File(workingDir, name);
        FileUtils.writeStringToFile(file, contents);
        
        CWLFileValueHelper.setName(name, value);
        CWLFileValueHelper.setDirname(file.getParentFile().getName(), value);
        CWLFileValueHelper.setPath(file.getAbsolutePath(), value);
        return new CWLPortProcessorResult(value, true);
      }
    }
    return new CWLPortProcessorResult(value, false);
  }

}

@@ -1230,16 +1230,109 @@
 g name =
+ CWLFileValueHelper.getName(value);%0A        if (StringUtils.isEmpty(name)) %7B%0A          name =
  %22file_l
@@ -1371,16 +1371,36 @@
 tring();
+          %0A        %7D
 %0A       

create file literal with a given name if there is one