/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2023  SteamWar.de-Serverteam
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.bausystem.features.loader.elements.impl;

import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement;
import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum;
import de.steamwar.inventory.SWItem;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Lectern;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;

import java.util.function.Consumer;

public class LoaderLectern extends LoaderInteractionElement<LoaderLectern.LecternSettingsEnum> {


    public LoaderLectern(Location location) {
        super(location, LecternSettingsEnum.NEXT_PAGE, LecternSettingsEnum.NOOP, LecternSettingsEnum.values());
    }

    public enum LecternSettingsEnum implements LoaderSettingsEnum<org.bukkit.block.data.type.Lectern, LoaderLectern, LecternSettingsEnum> {
        NOOP {
            @Override
            public int getPos() {
                return 2;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                return new SWItem(Material.STRUCTURE_VOID, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_NOOP", player));
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
            }
        },

        PREVIOUS_PAGE {
            @Override
            public int getPos() {
                return 5;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                return new SWItem(Material.STICK, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE_PREV", player));
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                if (lectern.getPage() > 0) {
                    lectern.setPage(lectern.getPage() - 1);
                    parent.update(blockData);
                }
            }
        },

        NEXT_PAGE {
            @Override
            public int getPos() {
                return 6;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                return new SWItem(Material.STICK, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE_NEXT", player));
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (lectern.getPage() < pages) {
                    lectern.setPage(lectern.getPage() + 1);
                    parent.update(blockData);
                }
            }
        },

        PAGE_1 {
            @Override
            public int getPos() {
                return 9;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 1));
                swItem.getItemStack().setAmount(1);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (1 <= pages) {
                    lectern.setPage(1);
                    parent.update(blockData);
                }
            }
        },

        PAGE_2 {
            @Override
            public int getPos() {
                return 10;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 2));
                swItem.getItemStack().setAmount(2);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (2 <= pages) {
                    lectern.setPage(2);
                    parent.update(blockData);
                }
            }
        },

        PAGE_3 {
            @Override
            public int getPos() {
                return 11;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 3));
                swItem.getItemStack().setAmount(3);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (3 <= pages) {
                    lectern.setPage(3);
                    parent.update(blockData);
                }
            }
        },

        PAGE_4 {
            @Override
            public int getPos() {
                return 12;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 4));
                swItem.getItemStack().setAmount(4);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (4 <= pages) {
                    lectern.setPage(4);
                    parent.update(blockData);
                }
            }
        },

        PAGE_5 {
            @Override
            public int getPos() {
                return 13;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 5));
                swItem.getItemStack().setAmount(5);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (5 <= pages) {
                    lectern.setPage(5);
                    parent.update(blockData);
                }
            }
        },

        PAGE_6 {
            @Override
            public int getPos() {
                return 14;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 6));
                swItem.getItemStack().setAmount(6);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (6 <= pages) {
                    lectern.setPage(6);
                    parent.update(blockData);
                }
            }
        },

        PAGE_7 {
            @Override
            public int getPos() {
                return 15;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 7));
                swItem.getItemStack().setAmount(7);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (7 <= pages) {
                    lectern.setPage(7);
                    parent.update(blockData);
                }
            }
        },

        PAGE_8 {
            @Override
            public int getPos() {
                return 16;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 8));
                swItem.getItemStack().setAmount(8);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (8 <= pages) {
                    lectern.setPage(8);
                    parent.update(blockData);
                }
            }
        },

        PAGE_9 {
            @Override
            public int getPos() {
                return 17;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 9));
                swItem.getItemStack().setAmount(9);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (9 <= pages) {
                    lectern.setPage(9);
                    parent.update(blockData);
                }
            }
        },

        PAGE_10 {
            @Override
            public int getPos() {
                return 19;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 10));
                swItem.getItemStack().setAmount(10);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (10 <= pages) {
                    lectern.setPage(10);
                    parent.update(blockData);
                }
            }
        },

        PAGE_11 {
            @Override
            public int getPos() {
                return 20;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 11));
                swItem.getItemStack().setAmount(11);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (11 <= pages) {
                    lectern.setPage(11);
                    parent.update(blockData);
                }
            }
        },

        PAGE_12 {
            @Override
            public int getPos() {
                return 21;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 12));
                swItem.getItemStack().setAmount(12);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (12 <= pages) {
                    lectern.setPage(12);
                    parent.update(blockData);
                }
            }
        },

        PAGE_13 {
            @Override
            public int getPos() {
                return 23;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 13));
                swItem.getItemStack().setAmount(13);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (13 <= pages) {
                    lectern.setPage(13);
                    parent.update(blockData);
                }
            }
        },

        PAGE_14 {
            @Override
            public int getPos() {
                return 24;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 14));
                swItem.getItemStack().setAmount(14);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (14 <= pages) {
                    lectern.setPage(14);
                    parent.update(blockData);
                }
            }
        },

        PAGE_15 {
            @Override
            public int getPos() {
                return 25;
            }

            @Override
            public SWItem menu(Player player, LoaderLectern parent, int power, long ticks) {
                SWItem swItem = new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", "LOADER_INTERACTION_PAGE", player, 15));
                swItem.getItemStack().setAmount(15);
                return swItem;
            }

            @Override
            public void execute(Location location, org.bukkit.block.data.type.Lectern blockData, LoaderLectern parent, int power, long ticks, Consumer<Long> delay) {
                if (!blockData.hasBook()) return;
                Lectern lectern = (Lectern) location.getBlock().getState();
                ItemStack itemStack = lectern.getInventory().getItem(0);
                if (itemStack == null) return;
                int pages = ((BookMeta) itemStack.getItemMeta()).getPages().size();
                if (15 <= pages) {
                    lectern.setPage(15);
                    parent.update(blockData);
                }
            }
        }
    }

    @Override
    public SWItem menu(Player player) {
        return new SWItem(Material.LECTERN, translateItemName("LOADER_BUTTON_LECTERN", player));
    }

    @Override
    public boolean checkBlockInWorld() {
        return location.getBlock().getType() == Material.LECTERN;
    }
}
