/*
 *   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;

import de.steamwar.bausystem.features.simulator.SimulatorCursor;
import de.steamwar.bausystem.features.simulator.SimulatorWatcher;
import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.bausystem.features.simulator.data.SimulatorElement;
import de.steamwar.bausystem.features.simulator.data.SimulatorGroup;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorPageGui;
import de.steamwar.data.CMDs;
import de.steamwar.inventory.SWItem;
import org.bukkit.Material;
import org.bukkit.entity.Player;

public class SimulatorGui extends SimulatorPageGui<SimulatorGroup> {

    public SimulatorGui(Player player, Simulator simulator) {
        super(player, simulator, 6 * 9, simulator.getGroups());
    }

    @Override
    public String baseTitle() {
        return "Simulator - " + simulator.getName();
    }

    @Override
    public void headerAndFooter() {
        if (simulator.getGroups().removeIf(element -> element.getElements().isEmpty() || element.getElements().stream().allMatch(simulatorElement -> simulatorElement.getPhases().isEmpty()))) {
            SimulatorWatcher.update(simulator);
        }

        inventory.setItem(4, simulator.toItem(player, clickType -> {
            new SimulatorMaterialGui(player, simulator, simulator::getMaterial, simulator::setMaterial, this).open();
        }));
        SimulatorCursor.CursorType cursorType = SimulatorCursor.getCursorType(player);
        inventory.setItem(48, new SWItem(cursorType.material, "§7Placing §8-§e " + cursorType.name, clickType -> {
            new SimulatorCursorSwitcherGui(player, simulator, this).open();
        }));
        inventory.setItem(50, new SWItem(Material.REPEATER, "§eSettings", clickType -> {
            new SimulatorSettingsGui(player, simulator, this).open();
        }).setCustomModelData(CMDs.Simulator.SETTINGS));
    }

    @Override
    public SWItem convert(SimulatorGroup simulatorGroup) {
        return simulatorGroup.toItem(player, clickType -> {
            new SimulatorGroupGui(player, simulator, simulatorGroup, this).open();
        }, clickType -> {
            SimulatorElement<?> element = simulatorGroup.getElements().get(0);
            element.open(player, simulator, simulatorGroup, this);
        });
    }
}
