package com.commercetools.sdk.contrib;

import io.sphere.sdk.client.*;
import org.junit.rules.ExternalResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;

public final class SphereClientRule extends ExternalResource implements BlockingSphereClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(SphereClientRule.class);
    private BlockingSphereClient client;

    @Override
    public void close() {
        LOGGER.warn("it is not recommended to close the client directly in " + getClass().getName());
        client.close();
    }

    @Override
    public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) {
        return client.execute(sphereRequest);
    }

    @Override
    public <T> T executeBlocking(final SphereRequest<T> sphereRequest) {
        return client.executeBlocking(sphereRequest);
    }

    @Override
    public <T> T executeBlocking(final SphereRequest<T> sphereRequest, final long l, final TimeUnit timeUnit) {
        return client.executeBlocking(sphereRequest, l, timeUnit);
    }

    @Override
    public <T> T executeBlocking(final SphereRequest<T> sphereRequest, final Duration duration) {
        return client.executeBlocking(sphereRequest, duration);
    }

    @Override
    protected void after() {
        client.close();
    }

    @Override
    protected void before() throws Throwable {
        final String debugOutput = "current dir "
                + new File(".").getAbsolutePath()
//              +  " " + new File(".").getParentFile().getParentFile().getAbsolutePath()
                ;
        throw new RuntimeException(debugOutput);

//        File file = new File("integrationtest.properties");
//        if (!file.exists()
//                && new File(".").getParentFile().exists()
//                && new File(".").getParentFile().getParentFile().exists()
//                && new File(".").getParentFile().getParentFile().getParentFile().exists()
//                && new File(new File(".").getParentFile().getParentFile().getParentFile(), "integrationtest.properties").exists()) {
//            file = new File(new File(".").getParentFile().getParentFile().getParentFile(), "integrationtest.properties");
//        }
//        try (final FileInputStream fileInputStream = new FileInputStream(file)) {
//            final Properties properties = new Properties();
//            properties.load(fileInputStream);
//            final SphereClientConfig config =  SphereClientConfig.ofProperties(properties, "");
//            final SphereClient underlying = SphereClientFactory.of().createClient(config);
//            client = BlockingSphereClient.of(underlying, 20, TimeUnit.SECONDS);
//        }
    }
}

@@ -1583,321 +1583,123 @@
     
-final String debugOutput = %22current dir %22%0A                + new File(%22.%22).getAbsolutePath()%0A//              +  %22 %22 + new File(%22.%22).getParentFile().getParentFile().getAbsolutePath()%0A                ;%0A        throw new RuntimeException(debugOutput);%0A%0A//        File file = new File(%22integrationtest.properties%22
+File file = new File(%22integrationtest.properties%22);%0A        final File parentFile = new File(%22.%22).getParentFile(
 );%0A
-//
     
@@ -1713,34 +1713,32 @@
 (!file.exists()%0A
-//
                 
@@ -1740,34 +1740,38 @@
      && 
-new File(%22.%22).getP
+parentFile !=null && p
 arentFil
@@ -1763,34 +1763,32 @@
 ll && parentFile
-()
 .exists()%0A//    
@@ -1773,34 +1773,32 @@
 ntFile.exists()%0A
-//
                 
@@ -1796,37 +1796,34 @@
          && 
-new File(%22.%22)
+parentFile
 .getParentFi
@@ -1818,32 +1818,54 @@
 .getParentFile()
+ != null && parentFile
 .getParentFile()
@@ -1866,34 +1866,32 @@
 File().exists()%0A
-//
                 
@@ -1893,28 +1893,41 @@
      && 
-new 
+parentFile.getParent
 File(
-%22.%22
 ).getPar
@@ -1927,32 +1927,54 @@
 .getParentFile()
+ != null && parentFile
 .getParentFile()
@@ -1999,18 +1999,16 @@
 xists()%0A
-//
         
@@ -2027,34 +2027,17 @@
 ew File(
-new File(%22.%22).getP
+p
 arentFil
@@ -2029,34 +2029,32 @@
  File(parentFile
-()
 .getParentFile()
@@ -2109,26 +2109,24 @@
 exists()) %7B%0A
-//
             
@@ -2141,34 +2141,17 @@
 ew File(
-new File(%22.%22).getP
+p
 arentFil
@@ -2143,34 +2143,32 @@
  File(parentFile
-()
 .getParentFile()
@@ -2212,26 +2212,24 @@
 operties%22);%0A
-//
         %7D%0A//
@@ -2226,18 +2226,16 @@
       %7D%0A
-//
         
@@ -2308,18 +2308,16 @@
 ile)) %7B%0A
-//
         
@@ -2368,18 +2368,16 @@
 ties();%0A
-//
         
@@ -2414,18 +2414,16 @@
 tream);%0A
-//
         
@@ -2510,18 +2510,16 @@
 s, %22%22);%0A
-//
         
@@ -2601,18 +2601,16 @@
 onfig);%0A
-//
         
@@ -2685,10 +2685,8 @@
 S);%0A
-//
     

improve credentials logic for release