package me.thekey.android.core;

import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import me.thekey.android.TheKey;
import me.thekey.android.exception.TheKeyApiError;
import me.thekey.android.exception.TheKeySocketException;

public abstract class CodeGrantAsyncTask extends AsyncTask<Void, Void, String> {
    @NonNull
    protected final TheKey mTheKey;

    @NonNull
    private final Uri mRedirectUri;
    @NonNull
    private final String mCode;
    @Nullable
    private final String mState;

    protected CodeGrantAsyncTask(@NonNull final TheKey thekey, @NonNull final String code,
                                 @Nullable final String state) {
        this(thekey, null, code, state);
    }

    protected CodeGrantAsyncTask(@NonNull final TheKey thekey, @Nullable final Uri redirectUri,
                                 @NonNull final String code, @Nullable final String state) {
        mTheKey = thekey;
        mRedirectUri = redirectUri != null ? redirectUri : mTheKey.getDefaultRedirectUri();
        mCode = code;
        mState = state;
    }

    public final AsyncTask<Void, Void, String> execute() {
        return executeOnExecutor(THREAD_POOL_EXECUTOR);
    }

    @Override
    protected final String doInBackground(final Void... params) {
        try {
            return mTheKey.processCodeGrant(mCode, mRedirectUri, mState);
        } catch (final TheKeyApiError | TheKeySocketException e) {
            return null;
        }
    }
}

@@ -313,24 +313,145 @@
 on;%0A
-%0Apublic abstract
+import timber.log.Timber;%0A%0Aimport static me.thekey.android.TheKey.PARAM_CODE;%0Aimport static me.thekey.android.TheKey.PARAM_STATE;%0A%0Apublic
  cla
@@ -527,31 +527,29 @@
 nNull%0A    pr
-otec
+iva
 te
-d
  final TheKe
@@ -611,30 +611,31 @@
 tUri;%0A    @N
-onN
 ull
+able
 %0A    private
@@ -704,32 +704,29 @@
 tate;%0A%0A    p
-rotected
+ublic
  CodeGrantAs
@@ -782,53 +782,320 @@
 nal 
-String code,%0A                    
+Uri dataUri) %7B%0A        mTheKey = thekey;%0A        mRedirectUri = DataUriUtils.removeCodeAndState(dataUri);%0A        mCode = dataUri.getQueryParameter(PARAM_CODE);%0A        mState = dataUri.getQueryParameter(PARAM_STATE);%0A
     
+%7D%0A%0A
     
-    
+public CodeGrantAsyncTask(@NonNull final TheKey thekey, @NonNull final String code,
  @Nu
@@ -1176,24 +1176,21 @@
 %7D%0A%0A    p
-rotected
+ublic
  CodeGra
@@ -1263,21 +1263,45 @@
 rectUri,
-%0A    
+ @NonNull final String code,%0A
         
@@ -1325,35 +1325,8 @@
     
-@NonNull final String code,
  @Nu
@@ -1723,24 +1723,57 @@
 . params) %7B%0A
+        if (mCode != null) %7B%0A    
         try 
@@ -1782,24 +1782,28 @@
             
+    
 return mTheK
@@ -1860,16 +1860,20 @@
         
+    
 %7D catch 
@@ -1931,24 +1931,159 @@
             
+   
+ Timber.tag(%22CodeGrantAsyncTask%22)%0A                        .d(e, %22error processing the code grant%22);%0A            %7D%0A        %7D%0A        
 return null;
@@ -2082,26 +2082,16 @@
 rn null;
-%0A        %7D
 %0A    %7D%0A%7D

support using a raw data URI for processing a code grant