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 {
        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);
        }
    }
}

@@ -1571,16 +1571,225 @@
 wable %7B%0A
+        final String debugOutput = %22current dir %22 + new File(%22.%22).getAbsolutePath() + %22 %22 + new File(%22.%22).getParentFile().getParentFile().getAbsolutePath();%0A        throw new RuntimeException(debugOutput);%0A%0A//
         
@@ -1832,32 +1832,34 @@
 t.properties%22);%0A
+//
         if (!fil
@@ -1861,32 +1861,34 @@
 (!file.exists()%0A
+//
                 
@@ -1921,32 +1921,34 @@
 File().exists()%0A
+//
                 
@@ -1997,32 +1997,34 @@
 File().exists()%0A
+//
                 
@@ -2097,16 +2097,18 @@
 xists()%0A
+//
         
@@ -2228,24 +2228,26 @@
 exists()) %7B%0A
+//
             
@@ -2352,24 +2352,26 @@
 operties%22);%0A
+//
         %7D%0A  
@@ -2364,24 +2364,26 @@
 //        %7D%0A
+//
         try 
@@ -2448,24 +2448,26 @@
 am(file)) %7B%0A
+//
             
@@ -2510,24 +2510,26 @@
 operties();%0A
+//
             
@@ -2562,16 +2562,18 @@
 tream);%0A
+//
         
@@ -2660,16 +2660,18 @@
 s, %22%22);%0A
+//
         
@@ -2753,16 +2753,18 @@
 onfig);%0A
+//
         
@@ -2835,16 +2835,18 @@
 CONDS);%0A
+//
         

throw exception for debugging