package com.github.Fupery.InvMenu.API.Templates;

import com.github.Fupery.InvMenu.API.Button.Button;
import com.github.Fupery.InvMenu.API.Button.CloseButton;
import com.github.Fupery.InvMenu.API.Event.MenuCloseReason;
import com.github.Fupery.InvMenu.API.Handler.CacheableMenu;
import com.github.Fupery.InvMenu.API.Handler.MenuHandler;
import com.github.Fupery.InvMenu.Utils.SoundCompat;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryType;

public abstract class ListMenu extends CacheableMenu {

    private final String heading;
    protected int page;

    public ListMenu(MenuHandler handler, String heading, int page) {
        super(handler, heading, InventoryType.CHEST);
        this.heading = heading;
        this.page = page;
    }

    @Override
    public void onMenuOpenEvent(Player viewer) {
    }

    @Override
    public void onMenuRefreshEvent(Player viewer) {
    }

    @Override
    public void onMenuClickEvent(Player viewer, int slot, ClickType click) {
    }

    @Override
    public void onMenuCloseEvent(Player viewer, MenuCloseReason reason) {
    }

    @Override
    public Button[] getButtons() {
        Button[] listItems = getListItems();
        int maxButtons = 25;
        Button[] buttons = new Button[maxButtons + 2];

        if (page < 1) {
            buttons[0] = new CloseButton(this);

        } else {
            buttons[0] = new PageButton(false);

            if (page > 0) {
                buttons[0].setAmount(page - 1);
            }
        }
        if (listItems == null || listItems.length < 1) return buttons;

        int start = page * maxButtons;
        int pageLength = listItems.length - start;

        if (pageLength > 0) {
            int end = (pageLength >= maxButtons) ? maxButtons : pageLength;

            System.arraycopy(listItems, start, buttons, 1, end);

            if (listItems.length > (maxButtons + start)) {
                buttons[maxButtons + 1] = new PageButton(true);

                if (page < 64) {
                    buttons[maxButtons + 1].setAmount(page + 1);
                }

            } else {
                buttons[maxButtons + 1] = null;
            }
        }
        return buttons;
    }

    protected void changePage(Player player, boolean forward) {
        if (forward) page++;
        else page--;
        refresh(player);
    }

    protected abstract Button[] getListItems();

    private class PageButton extends Button {

        boolean forward;

        private PageButton(boolean forward) {
            super(forward ? Material.EMERALD : Material.BARRIER, forward ? "§a§l➡" : "§c§l⬅");
            this.forward = forward;
        }

        @Override
        public void onClick(Player player, ClickType clickType) {
            if (forward) SoundCompat.UI_BUTTON_CLICK.play(player);
            else SoundCompat.UI_BUTTON_CLICK.play(player);
            changePage(player, forward);
        }
    }
}

@@ -604,34 +604,24 @@
   pr
-ivate final String heading
+otected int page
 ;%0A  
@@ -636,20 +636,25 @@
 ted int 
-page
+listStart
 ;%0A%0A    p
@@ -713,24 +713,161 @@
 int page) %7B%0A
+        this(handler, heading, 0, page);%0A    %7D%0A%0A    public ListMenu(MenuHandler handler, String heading, int listStartIndex, int page) %7B%0A
         supe
@@ -940,16 +940,57 @@
 eading;%0A
+        this.listStart = listStartIndex;%0A
         
@@ -1471,16 +1471,28 @@
 ons = 25
+ - listStart
 ;%0A      
@@ -1527,22 +1527,29 @@
 ton%5B
-maxButtons + 2
+type.getDefaultSize()
 %5D;%0A%0A
@@ -1584,33 +1584,41 @@
         buttons%5B
-0
+listStart
 %5D = new CloseBut
@@ -1666,17 +1666,25 @@
 buttons%5B
-0
+listStart
 %5D = new 
@@ -1726,17 +1726,17 @@
 (page %3E 
-0
+1
 ) %7B%0A    
@@ -1755,17 +1755,25 @@
 buttons%5B
-0
+listStart
 %5D.setAmo
@@ -1910,16 +1910,17 @@
  page * 
+(
 maxButto
@@ -1917,24 +1917,25 @@
  (maxButtons
+)
 ;%0A        in
@@ -2138,16 +2138,28 @@
 buttons,
+ listStart +
  1, end)
@@ -2252,24 +2252,36 @@
 maxButtons +
+ listStart +
  1%5D = new Pa
@@ -2370,16 +2370,28 @@
 uttons +
+ listStart +
  1%5D.setA
@@ -2483,16 +2483,28 @@
 uttons +
+ listStart +
  1%5D = nu
@@ -2763,21 +2763,23 @@
 %0A%0A    pr
-ivate
+otected
  class P
@@ -2845,14 +2845,13 @@
    p
-rivate
+ublic
  Pag

Allow adding buttons before a list via the new listStartIndex param