package org.folio.rest.tools;

import java.io.File;
import java.io.FileReader;
import java.util.List;
import java.util.Properties;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ResourceInfo;

import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
 * @author shale
 *
 */
public enum PomReader {

  INSTANCE;

  private String moduleName = null;
  private String version = null;
  private Properties props = null;
  private List<Dependency> dependencies = null;
  private String rmbVersion = null;

  private final Logger log = LoggerFactory.getLogger(PomReader.class);

  @SuppressWarnings("checkstyle:methodlength")
  private PomReader() {
    try {
      System.out.print("Attempting to read in the module name from....");
      MavenXpp3Reader mavenreader = new MavenXpp3Reader();
      Model model = null;
      String currentRunningJar =
          PomReader.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
      System.out.println(currentRunningJar);
      if(currentRunningJar != null && (currentRunningJar.contains("domain-models-runtime")
          || currentRunningJar.contains("domain-models-interface-extensions"))){
        //the runtime is the jar run when deploying during unit tests
        //the interface-extensions is the jar run when running build time tools,
        //like MDGenerator, ClientGenerator, etc..
        //this is build time - not runtime, so just use the pom
        File pomFile = new File("pom.xml");
        model = mavenreader.read(new FileReader(pomFile));
      }
      else{
        //this is runtime, the jar called via java -jar is the module's jar
        ClassPath classPath = ClassPath.from(Thread.currentThread().getContextClassLoader());
        ImmutableSet<ResourceInfo> resources = classPath.getResources();
        for (ResourceInfo info : resources) {
          if(info.getResourceName().endsWith("pom.xml")){
            //maven sets the classpath order according to the pom, so the poms project will be the first entry
            model = mavenreader.read(PomReader.class.getResourceAsStream("/"+info.getResourceName()));
            break;
          }
        }
      }
      if(model.getParent() != null){
        moduleName = model.getParent().getArtifactId();
        version = model.getParent().getVersion();
      }
      else{
        moduleName = model.getArtifactId();
      }

      if(version == null){
        version = model.getVersion();
        if(version == null){
          version = "1.0.0";
        }
      }
      version = version.replaceAll("-.*", "");
      moduleName = moduleName.replaceAll("-", "_");
      props = model.getProperties();
      dependencies = model.getDependencies();

      for (int i = 0; i < dependencies.size(); i++) {
        if("domain-models-runtime".equals(dependencies.get(i).getArtifactId())){
          rmbVersion = dependencies.get(i).getVersion();
        }
      }
      if(rmbVersion == null){
        //if we are in the rmb jar - build time
        rmbVersion = version;
      }

      //props.list(System.out);
      log.info("module name: " + moduleName + ", version: " + version);
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
  }

  public String getVersion() {
    return version;
  }

  public String getModuleName() {
    return moduleName;
  }

  public Properties getProps() {
    return props;
  }

  public List<Dependency> getDependencies() {
    return dependencies;
  }

  public String getRmbVersion() {
    return rmbVersion;
  }

}

@@ -2804,24 +2804,25 @@
 %22-.*%22, %22%22);%0A
+%0A
       module
@@ -2941,24 +2941,148 @@
 dencies();%0A%0A
+      //the version is a placeholder to a value in the props section%0A      version = replacePlaceHolderWithValue(version);%0A%0A
       for (i
@@ -3253,32 +3253,153 @@
 ).getVersion();%0A
+          rmbVersion = replacePlaceHolderWithValue(rmbVersion);%0A          rmbVersion = rmbVersion.replaceAll(%22-.*%22, %22%22);%0A
         %7D%0A      
@@ -3392,32 +3392,33 @@
       %7D%0A      %7D%0A
+%0A
       if(rmbVers
@@ -3686,16 +3686,16 @@
 (), e);%0A
-
     %7D%0A  
@@ -3693,24 +3693,355 @@
 %0A    %7D%0A  %7D%0A%0A
+  private String replacePlaceHolderWithValue(String placeholder)%7B%0A    String ret%5B%5D = new String%5B%5D%7Bplaceholder%7D;%0A    if(placeholder != null && placeholder.startsWith(%22$%7B%22))%7B%0A      props.forEach( (k,v) -%3E %7B%0A        if((%22$%7B%22+k+%22%7D%22).equals(placeholder))%7B%0A          ret%5B0%5D = (String)v;%0A        %7D%0A      %7D);%0A    %7D%0A    return ret%5B0%5D;%0A  %7D%0A%0A
   public Str

handle version even if its a placeholder into props