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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked;
import de.steamwar.techhider.TechHider;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Collection;
import java.util.stream.Collectors;

@Linked
public class ForceSpectatorCommand extends SWCommand {

    public ForceSpectatorCommand() {
        super("forcespectator");
    }

    @Register
    public void forceSpectator(@Validator("supervisor") Player player, @Mapper("builder") Player other) {
        Permission.forceOnlySpectator(other);
    }

    @Mapper("builder")
    public TypeMapper<Player> spectatorMapper() {
        return new TypeMapper<>() {
            @Override
            public Player map(CommandSender commandSender, String[] previousArguments, String s) {
                Player player = Bukkit.getPlayer(s);
                if (player == null) {
                    return null;
                }
                if (Permission.BUILD.hasPermission(player) && !Permission.SUPERVISOR.hasPermission(player)) {
                    return player;
                }
                return null;
            }

            @Override
            public Collection<String> tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) {
                return Bukkit.getOnlinePlayers().stream()
                        .filter(Permission.BUILD::hasPermission)
                        .filter(player -> !Permission.SUPERVISOR.hasPermission(player))
                        .map(Player::getName)
                        .collect(Collectors.toList());
            }
        };
    }
}
