/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2023  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.inventory.InvCallback;
import de.steamwar.inventory.SWItem;
import org.bukkit.Material;
import org.bukkit.entity.Player;

import java.util.stream.Collectors;

public class SimulatorGroupChooserGui extends SimulatorPageGui<SimulatorGroup> {

    private final SimulatorElement<?> subject;
    private final SimulatorGroup parent;
    private final SimulatorBaseGui back;

    public SimulatorGroupChooserGui(Player player, Simulator simulator, SimulatorElement<?> subject, SimulatorGroup parent, SimulatorBaseGui back) {
        super(player, simulator, 6 * 9, simulator.getGroups().stream().filter(e -> e != parent).filter(e -> subject.canBeInGroup(e)).collect(Collectors.toList()));
        this.subject = subject;
        this.parent = parent;
        this.back = back;
    }

    @Override
    public void headerAndFooter() {
        inventory.setItem(4, new SWItem(simulator.getMaterial(), "§e" + simulator.getName(), clickType -> {
        }));
        if (parent.getElements().size() != 1) {
            inventory.setItem(49, new SWItem(Material.BARRIER, "§cRemove from Group", clickType -> {
                SimulatorGroup newParent = new SimulatorGroup();
                newParent.add(subject);
                simulator.getGroups().add(newParent);
                parent.getElements().remove(subject);
                back.open();
                SimulatorWatcher.update(simulator);
                if (parent.getElements().size() == 1) {
                    parent.setDisabled(false);
                    parent.setMaterial(Material.BARREL);
                }
            }));
        }
        inventory.addCloseCallback(clickType -> {
            back.open();
        });
    }

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

    @Override
    public SWItem convert(SimulatorGroup simulatorGroup) {
        InvCallback invCallback = clickType -> {
            simulatorGroup.add(subject);
            parent.getElements().remove(subject);
            if (parent.getElements().size() == 1) {
                parent.setDisabled(false);
                parent.setMaterial(Material.BARREL);
            }
            back.open();
            SimulatorWatcher.update(simulator);
        };
        return simulatorGroup.toItem(player, invCallback, invCallback);
    }
}
