package co.epitre.aelf_lectures.bible;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

public class BibleChapterPagerAdapter extends FragmentPagerAdapter {
    private BibleBookEntry mBibleBookEntry;
    private int mHighlightChapterId;
    private String mHighlightQuery;

    public BibleChapterPagerAdapter(@NonNull FragmentManager fm, @NonNull BibleBookEntry bibleBookEntry, int highlightChapterId, String highlightQuery) {
        super(fm);
        mBibleBookEntry = bibleBookEntry;
        mHighlightChapterId = highlightChapterId;
        mHighlightQuery = highlightQuery;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        String chapterText = mBibleBookEntry.getChapter(position).getContent();
        Fragment fragment = new BibleChapterFragment();

        Bundle args = new Bundle();
        args.putString(BibleChapterFragment.ARG_TEXT_HTML, chapterText);
        if (position == mHighlightChapterId) {
            args.putString(BibleChapterFragment.ARG_HIGHLIGHT, mHighlightQuery);
        }
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mBibleBookEntry.getChapters().get(position).getChapterName();
    }

    public String getRoute(int position) {
        return mBibleBookEntry.getChapters().get(position).getRoute();
    }

    @Override
    public int getCount() {
        return mBibleBookEntry.getChapters().size();
    }
}

@@ -233,16 +233,40 @@
 apter;%0A%0A
+import java.util.List;%0A%0A
 public c
@@ -1274,32 +1274,524 @@
 ragment;%0A    %7D%0A%0A
+    private BibleBookChapter getChapter(int position) %7B%0A        List%3CBibleBookChapter%3E chapters = mBibleBookEntry.getChapters();%0A%0A        // Handle empty state%0A        if (chapters.isEmpty()) %7B%0A            return null;%0A        %7D%0A%0A        // Clamp position%0A        if (position %3C 0) %7B%0A            position = 0;%0A        %7D%0A        if (position %3E= chapters.size()) %7B%0A            position = chapters.size() - 1;%0A        %7D%0A%0A        // Grab the chapter%0A        return chapters.get(position);%0A    %7D%0A%0A
     @Override%0A  
@@ -1845,32 +1845,24 @@
 ) %7B%0A        
-return m
 BibleBookEnt
@@ -1850,38 +1850,50 @@
        BibleBook
-Entry.
+Chapter chapter = 
 getChapters().ge
@@ -1878,39 +1878,32 @@
 ter = getChapter
-s().get
 (position).getCh
@@ -1892,24 +1892,112 @@
 er(position)
+;%0A        if (chapter == null) %7B%0A            return %22%22;%0A        %7D%0A        return chapter
 .getChapterN
@@ -2057,32 +2057,24 @@
 ) %7B%0A        
-return m
 BibleBookEnt
@@ -2062,38 +2062,50 @@
        BibleBook
-Entry.
+Chapter chapter = 
 getChapters().ge
@@ -2102,15 +2102,8 @@
 pter
-s().get
 (pos
@@ -2104,24 +2104,112 @@
 er(position)
+;%0A        if (chapter == null) %7B%0A            return %22%22;%0A        %7D%0A        return chapter
 .getRoute();

fix IndexOutOfBoundsException crash in getRoute