package org.jboss.resteasy.utils;

import org.jboss.resteasy.utils.maven.MavenUtil;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Util class for RESTEasy rxjava (1) related testing.
 */
public class TestUtilRxJava {

   private static String defaultRxJavaVersion = "1.3.2";
   private static String defaultRxJavaReactiveStreamsVersion = "1.2.1";
   private static String defaultSmallRyeContextPropagationVersion = "1.0.1";
   private static String defaultMicroProfileContextPropagationVersion = "1.0-RC1";


   private static String readSystemProperty(String name, String defaultValue) {
      String value = System.getProperty(name);
      return (value == null) ? defaultValue : value;
   }

   private static String getRxJavaVersion() {
      return readSystemProperty("version.rxjava", defaultRxJavaVersion);
   }

   private static String getRxJavaReactiveStreamsVersion() {
      return readSystemProperty("version.rxjava-reactive-streams", defaultRxJavaReactiveStreamsVersion);
   }

   private static String getSmallRyeContextPropagationVersion() {
      return readSystemProperty("version.smallrye-context-propagation", defaultSmallRyeContextPropagationVersion);
   }

   private static String getMicroProfileContextPropagationVersion() {
      return readSystemProperty("version.microprofile-context-propagation", defaultMicroProfileContextPropagationVersion);
   }

   private static File[] resolveRxJavaDependencies() {
      MavenUtil mavenUtil;
      mavenUtil = MavenUtil.create(true);
      List<File> runtimeDependencies = new ArrayList<>();

      try {
         runtimeDependencies.add(mavenUtil.createMavenGavFile("org.jboss.resteasy:resteasy-context-propagation:" + System.getProperty("version.resteasy.testsuite")));
         runtimeDependencies.add(mavenUtil.createMavenGavFile("org.jboss.resteasy:resteasy-rxjava:" + System.getProperty("project.version")));
         runtimeDependencies.add(mavenUtil.createMavenGavFile("org.eclipse.microprofile.context-propagation:microprofile-context-propagation-api:" + getMicroProfileContextPropagationVersion()));
         runtimeDependencies.add(mavenUtil.createMavenGavFile("io.smallrye:smallrye-context-propagation:" + getSmallRyeContextPropagationVersion()));
         runtimeDependencies.add(mavenUtil.createMavenGavFile("io.smallrye:smallrye-context-propagation-propagators-rxjava1:" + getSmallRyeContextPropagationVersion()));
         runtimeDependencies.add(mavenUtil.createMavenGavFile("io.reactivex:rxjava:" + getRxJavaVersion()));
         runtimeDependencies.add(mavenUtil.createMavenGavFile("io.reactivex:rxjava-reactive-streams:" + getRxJavaReactiveStreamsVersion()));
      } catch (Exception e) {
         throw new RuntimeException("Unable to get artifacts from maven via Aether library", e);
      }

      File[] dependencies = runtimeDependencies.toArray(new File[]{});
      return dependencies;
   }

   public static void addRxJavaLibraries(WebArchive archive) {
      archive.addAsLibraries(resolveRxJavaDependencies());
   }

   public static void setManifestWithReactiveStreamsDependency(WebArchive archive) {
      archive.setManifest(new StringAsset("Manifest-Version: 1.0\n" + "Dependencies: org.reactivestreams"));
   }

   public static void setupRxJava(WebArchive archive) {
      addRxJavaLibraries(archive);
      setManifestWithReactiveStreamsDependency(archive);
   }

}
