/*
 *   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.data.SimulatorElement;
import de.steamwar.bausystem.features.simulator.data.SimulatorGroup;
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.List;
import java.util.stream.Collectors;

public class SimulatorGroupGui extends SimulatorPageGui<SimulatorElement<?>> {

    private SimulatorGroup simulatorGroup;
    private final SimulatorBaseGui back;

    public SimulatorGroupGui(Player player, Simulator simulator, SimulatorGroup simulatorGroup, SimulatorBaseGui back) {
        super(player, simulator, 6 * 9, simulatorGroup.getElements());
        this.simulatorGroup = simulatorGroup;
        this.back = back;
    }

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

    @Override
    public void headerAndFooter() {
        if (simulatorGroup.getMaterial() == null) {
            List<SimulatorGroup> parents = data.stream().map(e -> e.getGroup(simulator)).distinct().collect(Collectors.toList());
            if (parents.size() == 1) {
                simulatorGroup = parents.get(0);
                data = simulatorGroup.getElements();
            }
        }

        if (simulatorGroup.getElements().removeIf(element -> element.getPhases().isEmpty())) {
            SimulatorWatcher.update(simulator);
        }
        if (simulatorGroup.getElements().size() < 2) {
            back.open();
            return;
        }

        simulatorGroup.sort();

        inventory.setItem(0, new SWItem(Material.ARROW, "§eBack", clickType -> {
            back.open();
        }).setCustomModelData(CMDs.BACK));

        inventory.setItem(8, new SWItem(Material.BARRIER, "§eDelete", clickType -> {
            simulatorGroup.getElements().clear();
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.DELETE));

        inventory.setItem(4, simulatorGroup.toItem(player, clickType -> {
            if (simulatorGroup.getMaterial() == null) return;
            new SimulatorMaterialGui(player, simulator, simulatorGroup::getMaterial, simulatorGroup::setMaterial, this).open();
        }, clickType -> {
        }));

        inventory.setItem(48, new SWItem(Material.REPEATER, "§eSettings", clickType -> {
            new SimulatorGroupSettingsGui(player, simulator, simulatorGroup, this).open();
        }).setCustomModelData(CMDs.Simulator.SETTINGS));
        boolean disabled = simulatorGroup.getMaterial() == null ? simulatorGroup.getElements().stream().allMatch(SimulatorElement::isDisabled) : simulatorGroup.isDisabled();
        inventory.setItem(50, new SWItem(disabled ? Material.ENDER_PEARL : Material.ENDER_EYE, simulatorGroup.isDisabled() ? "§cDisabled" : "§aEnabled", clickType -> {
            if (simulatorGroup.getMaterial() == null) {
                simulatorGroup.getElements().forEach(simulatorElement -> {
                    simulatorElement.setDisabled(!disabled);
                });
            } else {
                simulatorGroup.setDisabled(!disabled);
            }
            SimulatorWatcher.update(simulator);
        }).setCustomModelData(CMDs.Simulator.ENABLED_OR_DISABLED));
    }

    @Override
    public SWItem convert(SimulatorElement<?> element) {
        return element.toItem(player, clickType -> {
            element.open(player, simulator, simulatorGroup, this);
        });
    }
}
