package org.wikipedia.feed.random;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import org.wikipedia.R;
import org.wikipedia.concurrency.CallbackTask;
import org.wikipedia.dataclient.restbase.page.RbPageSummary;
import org.wikipedia.feed.view.StaticCardView;
import org.wikipedia.history.HistoryEntry;
import org.wikipedia.page.PageTitle;
import org.wikipedia.random.RandomSummaryClient;
import org.wikipedia.readinglist.page.database.ReadingListPageDao;
import org.wikipedia.util.log.L;

import retrofit2.Call;

public class RandomCardView extends StaticCardView<RandomCard> {
    public interface Callback {
        void onGetRandomError(@NonNull Throwable t, @NonNull RandomCardView view);
    }

    public RandomCardView(@NonNull Context context) {
        super(context);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getRandomPage();
            }
        });
    }

    @Override public void setCard(@NonNull RandomCard card) {
        super.setCard(card);
        setTitle(getString(R.string.view_random_card_title));
        setSubtitle(getString(R.string.view_random_card_subtitle));
        setIcon(R.drawable.icon_feed_random);
    }

    public void getRandomPage() {
        if (getCallback() != null && getCard() != null) {
            setProgress(true);
            new RandomSummaryClient().request(getCard().wikiSite(),  new RandomSummaryClient.Callback() {
                @Override
                public void onSuccess(@NonNull Call<RbPageSummary> call, @NonNull PageTitle title) {
                    setProgress(false);
                    if (getCallback() != null && getCard() != null) {
                        getCallback().onSelectPage(getCard(),
                                new HistoryEntry(title, HistoryEntry.SOURCE_FEED_RANDOM));
                    }
                }

                @Override
                public void onError(@NonNull Call<RbPageSummary> call, @NonNull Throwable t) {
                    L.w("Failed to get random card from network. Falling back to reading lists.", t);
                    getRandomReadingListPage(t);
                    setProgress(false);
                }
            });
        }
    }

    private void getRandomReadingListPage(@NonNull final Throwable throwableIfEmpty) {
        ReadingListPageDao.instance().randomPage(new CallbackTask.DefaultCallback<PageTitle>() {
            @Override public void success(@Nullable PageTitle title) {
                if (getCallback() != null && getCard() != null) {
                    if (title != null) {
                        getCallback().onSelectPage(getCard(),
                                new HistoryEntry(title, HistoryEntry.SOURCE_FEED_RANDOM));
                    } else {
                        getCallback().onGetRandomError(throwableIfEmpty, RandomCardView.this);
                    }
                }
            }
        });
    }
}

@@ -393,24 +393,69 @@
 storyEntry;%0A
+import org.wikipedia.offline.OfflineManager;%0A
 import org.w
@@ -629,16 +629,45 @@
 log.L;%0A%0A
+import java.io.IOException;%0A%0A
 import r
@@ -1592,17 +1592,16 @@
 iSite(),
- 
  new Ran
@@ -2267,20 +2267,19 @@
  to 
-reading list
+compilation
 s.%22,
@@ -2312,31 +2312,35 @@
 etRandom
-ReadingListPage
+PageFromCompilation
 (t);%0A   
@@ -2453,31 +2453,693 @@
 etRandom
-ReadingListPage
+PageFromCompilation(@NonNull final Throwable throwableIfEmpty) %7B%0A        if (OfflineManager.hasCompilation() && getCallback() != null && getCard() != null) %7B%0A            try %7B%0A                getCallback().onSelectPage(getCard(), new HistoryEntry(%0A                        new PageTitle(OfflineManager.instance().getRandomTitle(), getCard().wikiSite()),%0A                        HistoryEntry.SOURCE_FEED_RANDOM));%0A            %7D catch (IOException e) %7B%0A                getCallback().onGetRandomError(e, RandomCardView.this);%0A            %7D%0A        %7D else %7B%0A            getRandomPageFromReadingLists(throwableIfEmpty);%0A        %7D%0A    %7D%0A%0A    private void getRandomPageFromReadingLists
 (@NonNul
@@ -3286,32 +3286,44 @@
        @Override
+%0A           
  public void suc

integrate with Random feed card.