/*
 *   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.features.simulator.gui.base;

import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.core.Core;
import de.steamwar.data.CMDs;
import de.steamwar.inventory.SWItem;
import org.bukkit.Material;
import org.bukkit.entity.Player;

import java.util.List;

public abstract class SimulatorPageGui<T> extends SimulatorBaseGui {

    protected int page = 0;
    protected List<T> data;

    protected SimulatorPageGui(Player player, Simulator simulator, int size, List<T> data) {
        super(player, simulator, size);
        this.data = data;
    }

    public final int maxPage() {
        return data.size() / (size - 18);
    }

    @Override
    public final String title() {
        return baseTitle() + " " + (page + 1) + "/" + (maxPage() + 1);
    }

    @Override
    public final void populate() {
        headerAndFooter();
        page = Math.min(page, maxPage());

        inventory.setItem(size - 9, new SWItem(page > 0 ? Material.LIME_DYE : Material.GRAY_DYE, page > 0 ? (byte) 10 : (byte) 8, Core.MESSAGE.parse(page > 0 ? "SWLISINV_PREVIOUS_PAGE_ACTIVE" : "SWLISINV_PREVIOUS_PAGE_INACTIVE", player), clickType -> {
            if (page > 0) {
                page--;
                open();
            }
        }).setCustomModelData(CMDs.PREVIOUS_PAGE));
        boolean hasNext = page < maxPage() - (data.size() % (size - 18) == 0 ? 1 : 0);
        inventory.setItem(size - 1, new SWItem(hasNext ? Material.LIME_DYE : Material.GRAY_DYE, hasNext ? (byte) 10 : (byte) 8, Core.MESSAGE.parse(hasNext ? "SWLISINV_NEXT_PAGE_ACTIVE" : "SWLISINV_NEXT_PAGE_INACTIVE", player), clickType -> {
            if (hasNext) {
                page++;
                open();
            }
        }).setCustomModelData(CMDs.NEXT_PAGE));

        int minElement = page * (size - 18);
        int maxElement = Math.min(data.size(), (page + 1) * (size - 18));
        int index = 9;

        for (int i = minElement; i < maxElement; i++) {
            T element = data.get(i);
            inventory.setItem(index++, convert(element));
        }
    }

    public abstract String baseTitle();

    public void headerAndFooter() {
    }

    public abstract SWItem convert(T t);
}
