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

import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.command.TypeValidator;
import de.steamwar.misslewars.Config;
import de.steamwar.misslewars.MWTeam;
import de.steamwar.misslewars.MissileWars;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

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

public class InviteCommand extends SWCommand {

    public InviteCommand() {
        super("invite", "inv");
    }

    @Register
    public void onCommand(@Validator Player player, @AllowNull @OptionalValue("") Player invitedPlayer) {
        MWTeam team = MissileWars.getTeam(player);

        if (!player.getUniqueId().equals(Config.RedLeader) && !player.getUniqueId().equals(Config.BlueLeader) || team == null) {
            player.sendMessage("§cDu kannst keine Spieler einladen.");
        } else if (invitedPlayer == null) {
            player.sendMessage("§cDieser Spieler ist nicht online.");
        } else if (MissileWars.getTeam(invitedPlayer) != null) {
            player.sendMessage("§cDieser Spieler ist bereits in einem Team.");
        } else if (MissileWars.getInvitation(invitedPlayer) != null) {
            player.sendMessage("§cDieser Spieler wurde bereits eingeladen.");
        } else {
            team.invitePlayer(invitedPlayer);
            player.sendMessage("§7Du hast §e" + invitedPlayer.getName() + " §7eingeladen.");
            invitedPlayer.sendMessage("§7Du wurdest von §e" + player.getName() + "§7 in das Team §e" + MissileWars.getTeam(player).getColoredName() + "§7 eingeladen.");
            invitedPlayer.sendMessage("§8/§eaccept §8- §7Zum akzeptieren.");
            invitedPlayer.sendMessage("§8/§edecline §8- §7Zum ablehnen.");
        }
    }

    @ClassMapper(value = Player.class, local = true)
    public TypeMapper<Player> typeMapper() {
        return new TypeMapper<>() {
            @Override
            public Player map(CommandSender commandSender, String[] previousArguments, String s) {
                return Bukkit.getPlayer(s);
            }

            @Override
            public Collection<String> tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) {
                return Bukkit.getOnlinePlayers()
                        .stream()
                        .filter(p -> MissileWars.getTeam(p) != null)
                        .filter(p -> MissileWars.getInvitation(p) != null)
                        .map(Player::getName)
                        .collect(Collectors.toList());
            }
        };
    }

    @ClassValidator(value = Player.class, local = true)
    public TypeValidator<Player> validator() {
        return (commandSender, player, messageSender) -> {
            if (!Config.isChallenge()) {
                messageSender.send("§cDieser Command ist deaktiviert.");
                return false;
            }
            return true;
        };
    }

    @Override
    protected void sendMessage(CommandSender sender, String message, Object[] args) {
        sender.sendMessage(message);
    }
}
