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

import de.steamwar.bausystem.features.simulator.execute.SimulatorAction;
import de.steamwar.inventory.InvCallback;
import de.steamwar.inventory.SWItem;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Material;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiConsumer;

@Getter
@Setter
public final class SimulatorGroup {
    private Material material = Material.CHEST;
    private boolean disabled = false;
    private final List<SimulatorElement<?>> elements = new ArrayList<>();

    public SimulatorGroup add(SimulatorElement<?> element) {
        if (!elements.contains(element)) {
            elements.add(element);
        }
        return this;
    }

    public void sort() {
        elements.sort(Comparator.comparingInt(SimulatorElement::getBaseTick));
    }

    public int getBaseTick() {
        return elements.stream()
                .mapToInt(SimulatorElement::getBaseTick)
                .min()
                .orElse(0);
    }

    public void changeBaseTicks(int tick) {
        elements.forEach(simulatorElement -> {
            simulatorElement.changeBaseTicks(tick);
        });
    }

    public void move(double x, double y, double z) {
        elements.forEach(simulatorElement -> {
            simulatorElement.move(x, y, z);
        });
    }

    public SWItem toItem(Player player, InvCallback groupCallback, InvCallback itemCallback) {
        if (elements.size() == 1) {
            return elements.get(0).toItem(player, itemCallback);
        } else {
            List<String> lore = new ArrayList<>();
            lore.add("§7Element count§8:§e " + elements.size());
            lore.add("§7Tick§8:§e " + getBaseTick());
            if (disabled) {
                lore.add("");
                lore.add("§cDisabled");
            }
            return new SWItem(material != null ? material : Material.ENDER_CHEST, "§eGroup", lore, disabled, groupCallback);
        }
    }

    public void toSimulatorActions(BiConsumer<Integer, SimulatorAction> tickStart, BiConsumer<Integer, SimulatorAction> tickEnd) {
        if (disabled) return;
        elements.forEach(simulatorElement -> {
            simulatorElement.toSimulatorActions(tickStart, tickEnd);
        });
    }
}
