/*
    This file is a part of the SteamWar software.

    Copyright (C) 2020  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.server.RegisteredServer;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.messages.Chatter;
import de.steamwar.messages.PlayerChatter;
import de.steamwar.persistent.Subserver;
import de.steamwar.velocitycore.VelocityCore;

import java.util.Collection;
import java.util.List;

public class ArenaCommand extends SWCommand {

    public ArenaCommand() {
        super("arena");
    }

    @Register
    public void arenaJoin(PlayerChatter sender, Subserver server) {
        TpCommand.teleport(sender, server.getRegisteredServer());
    }

    @ClassMapper(value = Subserver.class, local = true)
    public TypeMapper<Subserver> serverInfoTypeMapper() {
        return new TypeMapper<>() {
            @Override
            public Subserver map(Chatter sender, PreviousArguments previousArguments, String s) {
                return Subserver.getSubserver(VelocityCore.getProxy().getServer(s).map(RegisteredServer::getServerInfo).orElse(null));
            }

            @Override
            public boolean validate(Chatter sender, Subserver value, MessageSender messageSender) {
                if (!Subserver.isArena(value)) {
                    sender.system("ARENA_NOT_FOUND");
                    return false;
                }
                return true;
            }

            @Override
            public Collection<String> tabCompletes(Chatter sender, PreviousArguments previousArguments, String s) {
                List<Subserver> subserverList = Subserver.getServerList();
                synchronized (subserverList) {
                    return subserverList.stream().filter(Subserver::isArena).map(subserver -> subserver.getServer().getName()).toList();
                }
            }
        };
    }
}
