/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 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.utils;

import de.steamwar.bausystem.BauSystem;
import lombok.experimental.UtilityClass;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

@UtilityClass
public class ListChatView {

    public static <T> void chatView(Player player, List<T> elements, int page, Function<T, TextComponent> elementConverter, BiConsumer<TextComponent, Integer> beforePageConsumer, BiConsumer<TextComponent, Integer> afterPageConsumer) {
        int pageCount = (int) Math.ceil(elements.size() / 18d);
        for (int i = page * 18; i < elements.size() && i < (page + 1) * 18; i++) {
            T element = elements.get(i);
            player.spigot().sendMessage(elementConverter.apply(element));
        }

        TextComponent beforePage = new TextComponent("««");
        if (page > 0) {
            beforePage.setColor(ChatColor.YELLOW);
            beforePageConsumer.accept(beforePage, page - 1);
        } else {
            beforePage.setColor(ChatColor.RED);
        }

        TextComponent nextPage = new TextComponent(BauSystem.MESSAGE.parse("PAGE_LIST", player, page + 1, Math.max(pageCount, 1)));
        if (page < pageCount - 1) {
            nextPage.setColor(ChatColor.YELLOW);
            afterPageConsumer.accept(nextPage, page + 1);
        } else {
            nextPage.setColor(ChatColor.RED);
        }

        beforePage.addExtra(nextPage);
        player.spigot().sendMessage(beforePage);
    }
}
