package org.mnode.whistlepost.model;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ChildResource;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;

import static org.apache.sling.query.SlingQuery.$;

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class ArticleList extends Page {

    @ChildResource
    private Resource list;

    private int currentPage;

    @ValueMapValue @Default(intValues = 6)
    private int pageSize;

    private int limit = -1;

    @PostConstruct
    protected void init() {
        try {
            String[] directives = request.getRequestPathInfo().getSuffix().substring(1).split("-");
            if (directives.length > 1) {
                currentPage = Integer.parseInt(directives[0]);
                limit = Integer.parseInt(directives[1]);
            } else {
                currentPage = Integer.parseInt(directives[0]);
            }
        } catch (RuntimeException e) {
            currentPage = 1;
        }
//        pageSize = 6; //resource.getChild("paging").getValueMap().get("pageSize", 6);
    }

    public String getTitle() {
        if (getCurrentPage() > 1) {
            return String.format("%s | %s", super.getTitle(), getCurrentPage());
        } else {
            return super.getTitle();
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getPageCount() {
        return (int) Math.ceil((float) $(list).children().asList().size() / pageSize);
    }

    public boolean hasPrevious() {
        return currentPage > 1;
    }

    public boolean hasNext() {
        return currentPage < getPageCount() - 1;
    }

    public Iterable<Article> getArticles() {
        int offset = Math.min(currentPage * pageSize, $(list).children().asList().size() - 1);

        List<Resource> filtered;
        if (limit > 0) {
            filtered = $(list).children().asList().subList(offset, Math.min(offset + limit, $(list).children().asList().size()));
        } else {
            filtered = $(list).children().asList().subList(offset, Math.min(offset + pageSize, $(list).children().asList().size()));
        }
        return filtered.stream().map(r -> r.adaptTo(Article.class)).collect(Collectors.toList());
    }
}

@@ -423,16 +423,44 @@
 struct;%0A
+import java.util.ArrayList;%0A
 import j
@@ -1848,32 +1848,540 @@
 geSize);%0A    %7D%0A%0A
+    public Iterable%3CInteger%3E getPageNumbers() %7B%0A        List%3CInteger%3E pageNumbers = new ArrayList%3C%3E();%0A        for (int i = 1; i %3C= getPageCount(); i++) %7B%0A            if (i == 1 %7C%7C i == getPageCount() %7C%7C (i %3E getCurrentPage() - 3 && i %3C getCurrentPage() + 3)) %7B%0A                pageNumbers.add(i);%0A            %7D else if (pageNumbers.lastIndexOf(-1) %3C pageNumbers.size() - 1) %7B%0A                // add separator..%0A                pageNumbers.add(-1);%0A            %7D%0A        %7D%0A        return pageNumbers;%0A    %7D%0A%0A
     public boole
@@ -2395,24 +2395,24 @@
 revious() %7B%0A
-
         retu
@@ -2512,20 +2512,16 @@
 eCount()
- - 1
 ;%0A    %7D%0A
@@ -2596,16 +2596,17 @@
 ath.min(
+(
 currentP
@@ -2608,16 +2608,21 @@
 rentPage
+ - 1)
  * pageS

Support collapsed paging navigation