/*
 * Copyright 2015 Henrik Olsson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.meisolsson.githubsdk.core;

import android.net.Uri;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class GitHubPaginationInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        if(response.isSuccessful() && response.peekBody(1).string().equals("[")) {
            String json = "{";

            String link = response.header("link");
            if(link != null) {
                String[] links = link.split(",");
                for (String link1 : links) {
                    String[] pageLink = link1.split(";");
                    String page = Uri.parse(pageLink[0].replaceAll("[<>]", "")).getQueryParameter("page");
                    String rel = pageLink[1].replaceAll("\"", "").replace("rel=", "");

                    if (page != null)
                        json += String.format("\"%s\":\"%s\",", rel.trim(), page);
                }
            }

            json += String.format("\"items\":%s}", response.body().string());
            return response.newBuilder().body(ResponseBody.create(response.body().contentType(), json)).build();
        }
        return response;
    }
}

@@ -658,35 +658,94 @@
 ri;%0A
-%0Aimport java.io.IOException
+import android.text.TextUtils;%0A%0Aimport java.io.IOException;%0Aimport java.util.ArrayList
 ;%0A%0Ai
@@ -1041,16 +1041,17 @@
       if
+ 
 (respons
@@ -1070,11 +1070,104 @@
 ul()
- &&
+) %7B%0A            String linkHeader = response.header(%22link%22);%0A            final boolean isArray =
  res
@@ -1204,19 +1204,17 @@
 als(%22%5B%22)
-) %7B
+;
 %0A       
@@ -1222,109 +1222,126 @@
     
-String json = %22%7B%22;%0A%0A            String link = response.header(%22link%22);%0A            if(link != null) %7B
+final ArrayList%3CString%3E pageJson;%0A%0A            if (linkHeader != null) %7B%0A                pageJson = new ArrayList%3C%3E();
 %0A   
@@ -1374,16 +1374,22 @@
 s = link
+Header
 .split(%22
@@ -1425,17 +1425,16 @@
 ing link
-1
  : links
@@ -1481,17 +1481,16 @@
 k = link
-1
 .split(%22
@@ -1747,32 +1747,37 @@
             
-json += 
+pageJson.add(
 String.forma
@@ -1792,17 +1792,16 @@
 %22:%5C%22%25s%5C%22
-,
 %22, rel.t
@@ -1812,16 +1812,17 @@
 ), page)
+)
 ;%0A      
@@ -1850,28 +1850,278 @@
    %7D
-%0A%0A            json +
+ else %7B%0A                pageJson = null;%0A            %7D%0A%0A            final boolean hasPageInfo = pageJson != null && !pageJson.isEmpty();%0A            if (isArray %7C%7C hasPageInfo) %7B%0A                final String json;%0A                if (isArray) %7B%0A                    json 
 = St
@@ -2133,16 +2133,21 @@
 format(%22
+%7B%25s%25s
 %5C%22items%5C
@@ -2157,36 +2157,422 @@
 s%7D%22,
- response.body().string());%0A
+%0A                            hasPageInfo ? TextUtils.join(%22,%22, pageJson) : %22%22,%0A                            hasPageInfo ? %22, %22 : %22%22,%0A                            response.body().string());%0A                %7D else %7B%0A                    json = String.format(%22%7B%25s, %25s%22,%0A                            TextUtils.join(%22, %22, pageJson),%0A                            response.body().string().substring(1));%0A                %7D%0A    
     
@@ -2676,24 +2676,38 @@
 )).build();%0A
+            %7D%0A
         %7D%0A  
@@ -2736,9 +2736,8 @@
 %0A    %7D%0A%7D
-%0A

Fix pagination interception for search pages.