package com.github.kubatatami.judonetworking.transports;

import com.github.kubatatami.judonetworking.AsyncResult;

import java.io.IOException;

import okhttp3.Authenticator;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

/**
 * Created by Kuba on 16/10/15.
 */
public abstract class OkHttpOAuth2 {

    protected String tokenType;

    protected String accessToken;

    protected AsyncResult tokenAsyncResult;

    protected boolean lastTokenValid = true;

    protected Interceptor oAuthInterceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (accessToken != null && tokenType != null) {
                try {
                    if (tokenAsyncResult != null) {
                        tokenAsyncResult.await();
                    }
                } catch (InterruptedException e) {
                    throw new IOException(e);
                }
                request = request.newBuilder()
                        .header("Authorization", tokenType + " " + accessToken).build();
            }
            Response response = chain.proceed(request);
            if (response.isSuccessful()) {
                lastTokenValid = true;
            }
            return response;
        }
    };

    protected Authenticator oAuthAuthenticator = new Authenticator() {
        @Override
        public Request authenticate(Route route, Response response) throws IOException {
            if (lastTokenValid && canDoTokenRequest()) {
                boolean createToken = false;
                if (tokenAsyncResult == null || tokenAsyncResult.isDone()) {
                    tokenAsyncResult = doTokenRequest();
                    createToken = true;
                }
                try {
                    tokenAsyncResult.await();
                } catch (InterruptedException e) {
                    throw new IOException(e);
                }
                if (accessToken != null) {
                    if (createToken) {
                        lastTokenValid = false;
                    }
                    return response.request();
                }
            }
            return null;
        }
    };

    public void prepareOkHttpToOAuth(OkHttpClient.Builder okHttpClient) {
        okHttpClient.networkInterceptors().add(oAuthInterceptor);
        okHttpClient.authenticator(oAuthAuthenticator);
    }

    public void setOAuthToken(String tokenType, String accessToken) {
        this.tokenType = tokenType;
        this.accessToken = accessToken;
    }

    protected abstract AsyncResult doTokenRequest();

    protected abstract boolean canDoTokenRequest();

}

@@ -724,32 +724,53 @@
 hain.request();%0A
+            await();%0A
             if (
@@ -817,269 +817,8 @@
 ) %7B%0A
-                try %7B%0A                    if (tokenAsyncResult != null) %7B%0A                        tokenAsyncResult.await();%0A                    %7D%0A                %7D catch (InterruptedException e) %7B%0A                    throw new IOException(e);%0A                %7D%0A
     
@@ -1158,24 +1158,282 @@
   %7D%0A    %7D;%0A%0A
+    private void await() throws IOException %7B%0A        try %7B%0A            if (tokenAsyncResult != null) %7B%0A                tokenAsyncResult.await();%0A            %7D%0A        %7D catch (InterruptedException e) %7B%0A            throw new IOException(e);%0A        %7D%0A    %7D%0A%0A
     protecte
@@ -1912,174 +1912,16 @@
     
-try %7B%0A                    tokenAsyncResult.await();%0A                %7D catch (InterruptedException e) %7B%0A                    throw new IOException(e);%0A                %7D
+await();
 %0A   

fix oauth bug