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


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

import java.util.List;

public abstract class SimulatorScrollGui<T> extends SimulatorBaseGui {
    protected int scroll = 0;
    protected final List<T> data;

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

    private int maxScroll() {
        return Math.max(0, Math.min(scroll, data.size() - 9 + 1));
    }

    @Override
    public final String title() {
        return baseTitle() + " " + maxScroll() + "/" + Math.max((data.size() - 9 + 1), 0);
    }

    @Override
    public final void populate() {
        headerAndFooter();
        scroll = maxScroll();

        inventory.setItem(size - 9, SWItem.getDye(scroll > 0 ? 10 : 8), scroll > 0 ? (byte) 10 : (byte) 8, Core.MESSAGE.parse(scroll > 0 ? "SWLISINV_PREVIOUS_PAGE_ACTIVE" : "SWLISINV_PREVIOUS_PAGE_INACTIVE", player), clickType -> {
            if (scroll > 0) {
                scroll = Math.max(0, scroll - 9);
                open();
            }
        });
        boolean hasNext = (data.size() + 1) - scroll > 9;
        inventory.setItem(size - 1, SWItem.getDye(hasNext ? 10 : 8), hasNext ? (byte) 10 : (byte) 8, Core.MESSAGE.parse(hasNext ? "SWLISINV_NEXT_PAGE_ACTIVE" : "SWLISINV_NEXT_PAGE_INACTIVE", player), clickType -> {
            if (hasNext) {
                scroll = Math.min(scroll + 9, data.size() + 1 - 9);
                open();
            }
        });

        for (int i = 0; i < 9; i++) {
            if (scroll + i < data.size()) {
                T element = data.get(scroll + i);
                SWItem[] column = column(element, scroll + i);
                populateColumn(column, i);
            } else {
                SWItem[] column = lastColumn();
                populateColumn(column, i);
                break;
            }
        }
    }

    public abstract String baseTitle();

    public void headerAndFooter() {
    }

    public void populateColumn(SWItem[] column, int index) {
        for (int j = 0; j < column.length; j++) {
            inventory.setItem(index + j * 9 + 9, column[j]);
        }
    }

    public abstract SWItem[] column(T t, int index);

    public abstract SWItem[] lastColumn();
}
