/*
 *   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.SimulatorWatcher;
import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui;
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;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class SimulatorMaterialGui extends SimulatorPageGui<Material> {

    private static final List<Material> MATERIALS = Arrays.stream(Material.values())
            .filter(material -> !material.isAir())
            .filter(material -> !material.isLegacy())
            .filter(Material::isItem)
            .collect(Collectors.toList());

    private final Supplier<Material> currentMaterial;
    private Material material;
    private final Consumer<Material> change;
    private final SimulatorBaseGui back;

    public SimulatorMaterialGui(Player player, Simulator simulator, Supplier<Material> currentMaterial, Consumer<Material> change, SimulatorBaseGui back) {
        super(player, simulator, 6 * 9, MATERIALS);
        this.currentMaterial = currentMaterial;
        this.change = change;
        this.back = back;
    }

    @Override
    public boolean shouldOpen() {
        if (player.getItemOnCursor().getType().isAir()) {
            return true;
        }
        change.accept(player.getItemOnCursor().getType());
        SimulatorWatcher.update(simulator);
        return false;
    }

    @Override
    public String baseTitle() {
        return "Material";
    }

    @Override
    public void headerAndFooter() {
        material = currentMaterial.get();
        inventory.setItem(4, new SWItem(material, "§eMaterial", clickType -> {
        }));
        inventory.setItem(0, new SWItem(Material.ARROW, "§eBack", clickType -> {
            back.open();
        }).setCustomModelData(CMDs.BACK));
    }

    @Override
    public SWItem convert(Material material) {
        return new SWItem(material, "§eNew Material", Arrays.asList(material == this.material ? "§eSelected" : "§eClick to select"), material == this.material, clickType -> {
            change.accept(material);
            SimulatorWatcher.update(simulator);
        });
    }
}
