package org.wikipedia.page;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;

import org.wikipedia.WikipediaApp;
import org.wikipedia.dataclient.page.PageClient;
import org.wikipedia.dataclient.page.PageLead;
import org.wikipedia.dataclient.page.PageRemaining;
import org.wikipedia.util.log.L;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Response;

import static org.wikipedia.util.DimenUtil.calculateLeadImageWidth;

final class PageCacher {

    @SuppressLint("CheckResult")
    static void loadIntoCache(@NonNull PageTitle title) {
        L.d("Loading page into cache: " + title.getPrefixedText());
        PageClient client = new PageClient();
        Observable.zip(leadReq(client, title), remainingReq(client, title), (leadRsp, sectionsRsp) -> leadRsp)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(leadRsp -> {
                    for (int i = WikipediaApp.getInstance().getTabCount() - 1; i >= 0; i--) {
                        PageTitle pageTitle = WikipediaApp.getInstance().getTabList().get(i).getBackStackPositionTitle();
                        if (pageTitle.equals(title)) {
                            pageTitle.setThumbUrl(leadRsp.body().getThumbUrl());
                            break;
                        }
                    }
                }, L::e);
    }

    @NonNull
    private static Observable<Response<PageLead>> leadReq(@NonNull PageClient client, @NonNull PageTitle title) {
        return client.lead(title.getWikiSite(), null, null, null, title.getPrefixedText(),
                calculateLeadImageWidth());
    }

    @NonNull
    private static Observable<Response<PageRemaining>> remainingReq(@NonNull PageClient client,
                                                    @NonNull PageTitle title) {
        return client.sections(title.getWikiSite(), null, null, title.getPrefixedText());
    }

    private PageCacher() { }
}

@@ -171,23 +171,67 @@
 ent.
-page.PageClient
+RestService;%0Aimport org.wikipedia.dataclient.ServiceFactory
 ;%0Aim
@@ -264,21 +264,38 @@
 ent.
-page.PageLead
+okhttp.OkHttpConnectionFactory
 ;%0Aim
@@ -333,25 +333,58 @@
 age.Page
-Remaining
+Summary;%0Aimport org.wikipedia.util.UriUtil
 ;%0Aimport
@@ -540,24 +540,48 @@
 Schedulers;%0A
+import okhttp3.Request;%0A
 import retro
@@ -600,77 +600,8 @@
 e;%0A%0A
-import static org.wikipedia.util.DimenUtil.calculateLeadImageWidth;%0A%0A
 fina
@@ -785,156 +785,104 @@
 ));%0A
-        PageClient client = new PageClient();%0A        Observable.zip(leadReq(client, title), remainingReq(client, title), (leadRsp, sections
+%0A        Observable.zip(summaryReq(title), mobileHtmlReq(title), (summaryRsp, mobileHtml
 Rsp) -%3E 
 lead
@@ -877,20 +877,23 @@
 Rsp) -%3E 
-lead
+summary
 Rsp)%0A   
@@ -1021,20 +1021,23 @@
 bscribe(
-lead
+summary
 Rsp -%3E %7B
@@ -1358,20 +1358,23 @@
 humbUrl(
-lead
+summary
 Rsp.body
@@ -1380,24 +1380,28 @@
 y().getThumb
+nail
 Url());%0A    
@@ -1508,37 +1508,24 @@
 :e);%0A    %7D%0A%0A
-    @NonNull%0A
     private 
@@ -1546,159 +1546,300 @@
 ble%3C
-Response%3CPageLead%3E%3E leadReq(@NonNull PageClient client, @NonNull PageTitle title) %7B%0A        return client.lead(title.getWikiSite(), null, null, null, t
+okhttp3.Response%3E mobileHtmlReq(@NonNull PageTitle pageTitle) %7B%0A        Request request = new Request.Builder().url(UriUtil.resolveProtocolRelativeUrl(pageTitle.getWikiSite(),%0A                pageTitle.getWikiSite().url() + RestService.REST_API_PREFIX + RestService.PAGE_HTML_ENDPOINT + pageT
 itle
@@ -1856,17 +1856,18 @@
 edText()
-,
+))
 %0A       
@@ -1879,184 +1879,366 @@
     
-calculateLeadIma
+.addHeader(%22Accept-Language%22, WikipediaApp.getInstance().getAcceptLanguage(pageTitle.
 ge
+t
 Wi
-dth(
+kiSite()
 ))
-;
 %0A    
-%7D%0A%0A    @NonNull%0A    private static Observable%3CResponse%3CPageRemaining%3E%3E remainingReq(@NonNull Pa
+            .build();%0A%0A        return Observable.create(emitter -%3E %7B%0A            okhttp3.Response response = OkHttpConnectionFactory.
 ge
+t
 Client
- client,%0A                        
+().newCall(request).execute();%0A            emitter.onNext(response);%0A            emitter.onComplete();%0A
         
     
@@ -2237,106 +2237,223 @@
     
+%7D);%0A
     
+%7D%0A%0A
     
-            @NonNull PageTitle title) %7B%0A        return client.sections(title.getWikiSite()
+@NonNull%0A    private static Observable%3CResponse%3CPageSummary%3E%3E summaryReq(@NonNull PageTitle title) %7B%0A        return ServiceFactory.getRest(title.getWikiSite())%0A                .getSummaryResponse(null
 , nu

Update PageCacher to load page summary and mobile-html content