/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2024  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.velocitycore.commands;

import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.messages.Chatter;
import de.steamwar.persistent.Subserver;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.sql.UserPerm;
import de.steamwar.velocitycore.VelocityCore;

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

public class SendCommand extends SWCommand {

    // /send to <server> only [<users>]
    // <server> can also be current

    public SendCommand() {
        super("send", UserPerm.TEAM);
    }

    @Register("to")
    public void sendCommand(Chatter sender, RegisteredServer toServer, @ArrayLength(min = 1) Player... players) {
        for (Player player : players) {
            player.createConnectionRequest(toServer).fireAndForget();
        }
    }

    @ClassMapper(value = RegisteredServer.class, local = true)
    public TypeMapper<RegisteredServer> subserverTypeMapper() {
        return new TypeMapper<>() {
            private boolean check(SteamwarUser user, RegisteredServer registeredServer) {
                if (registeredServer == null) return false;
                ServerInfo serverInfo = registeredServer.getServerInfo();
                String name = serverInfo.getName();
                if (name.contains(" ")) return false;

                Subserver subserver = Subserver.getSubserver(serverInfo);
                return subserver == null || Subserver.isArena(subserver) || (Subserver.isBuilder(subserver) && user.hasPerm(UserPerm.BUILD));
            }

            @Override
            public RegisteredServer map(Chatter sender, PreviousArguments previousArguments, String s) {
                SteamwarUser user = sender.user();
                for (RegisteredServer registeredServer : VelocityCore.getProxy().getAllServers()) {
                    if (registeredServer.getServerInfo().getName().equalsIgnoreCase(s) && check(user, registeredServer)) {
                        return registeredServer;
                    }
                }

                RegisteredServer currentServer = sender.getPlayer().getCurrentServer().map(ServerConnection::getServer).orElse(null);
                if (s.equals("current") && check(user, currentServer)) {
                    return currentServer;
                }
                return null;
            }

            @Override
            public Collection<String> tabCompletes(Chatter sender, PreviousArguments previousArguments, String s) {
                SteamwarUser user = sender.user();
                List<RegisteredServer> previous = previousArguments.getAll(RegisteredServer.class);

                List<String> tabCompletes = new ArrayList<>();
                for (RegisteredServer registeredServer : VelocityCore.getProxy().getAllServers()) {
                    if (previous.contains(registeredServer)) continue;
                    if (check(user, registeredServer)) {
                        tabCompletes.add(registeredServer.getServerInfo().getName());
                    }
                }

                Player player = sender.getPlayer();
                if (player != null) {
                    RegisteredServer currentServer = player.getCurrentServer().map(ServerConnection::getServer).orElse(null);
                    if (currentServer != null && !previous.contains(currentServer) && check(user, currentServer)) {
                        tabCompletes.add("current");
                    }
                }

                return tabCompletes;
            }
        };
    }

    @ClassMapper(value = Player.class, local = true)
    public TypeMapper<Player> playerTypeMapper() {
        return new TypeMapper<>() {
            @Override
            public Player map(Chatter sender, PreviousArguments previousArguments, String s) {
                return VelocityCore.getProxy().getPlayer(s).orElse(null);
            }

            @Override
            public Collection<String> tabCompletes(Chatter sender, PreviousArguments previousArguments, String s) {
                List<RegisteredServer> previous = previousArguments.getAll(RegisteredServer.class);
                if (previous.isEmpty()) return null;
                Collection<Player> players = previous.get(0).getPlayersConnected();
                return VelocityCore.getProxy().getAllPlayers().stream().filter(player -> !players.contains(player)).map(Player::getUsername).collect(Collectors.toList());
            }
        };
    }
}
