/*
 *   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.SimulatorWatcher;
import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

public abstract class SimulatorBaseGui {

    protected final Player player;
    protected final Simulator simulator;
    private Inventory inv;
    protected SWInventory inventory;

    protected final int size;

    protected SimulatorBaseGui(Player player, Simulator simulator, int size) {
        this.player = player;
        this.simulator = simulator;
        this.size = size;
    }

    public final void open() {
        if (!shouldOpen()) return;

        if (inv != null) {
            // TODO: Flickering is better but not gone!
            for (int i = 9; i < size - 9; i++) {
                inv.setItem(i, null);
            }
            setup();
            if (player.getOpenInventory().getTopInventory() != inv) {
                inventory.open();
                SimulatorWatcher.watch(player, simulator, this::open);
            }
            player.getOpenInventory().setTitle(title());
            if (simulator != null && simulator.getStabGenerator() != null) {
                populateStabGenerator();
            } else {
                populate();
            }
            if (player.getOpenInventory().getTopInventory() == inv) {
                inventory.open();
                SimulatorWatcher.watch(player, simulator, this::open);
            }
            return;
        }

        player.closeInventory();

        inventory = new SWInventory(player, () -> {
            inv = Bukkit.createInventory(null, size, title());
            return inv;
        });
        setup();
        inventory.addCloseCallback(clickType -> {
            SimulatorWatcher.watch(player, null, null);
        });

        SimulatorWatcher.watch(player, simulator, this::open);
        if (simulator != null && simulator.getStabGenerator() != null) {
            populateStabGenerator();
        } else {
            populate();
        }
        inventory.open();
    }

    private void populateStabGenerator() {
        inventory.setItem(22, new SWItem(Material.BARRIER, "§cCancel Stab Generator", click -> {
            simulator.getStabGenerator().cancel();
            SimulatorWatcher.update(simulator);
        }));
    }

    public boolean shouldOpen() {
        return true;
    }

    private void setup() {
        for (int i = 0; i < 9; i++) {
            inventory.setItem(i, new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§8", clickType -> {
            }));
            inventory.setItem(size - 9 + i, new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§8", clickType -> {
            }));
        }
    }

    public abstract String title();

    public abstract void populate();
}
