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

import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.api.proxy.Player;
import de.steamwar.velocitycore.ServerVersion;
import de.steamwar.velocitycore.VelocityCore;
import de.steamwar.velocitycore.commands.TypeMappers;
import de.steamwar.messages.Chatter;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.sql.Team;
import lombok.experimental.UtilityClass;

import java.util.Collection;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@UtilityClass
public class TypeUtils {

    static void init() {
        SWCommandUtils.addMapper(Player.class, SWCommandUtils.createMapper(s -> VelocityCore.getProxy().getPlayer(s).orElse(null), s -> VelocityCore.getProxy().getAllPlayers().stream().map(Player::getUsername).collect(Collectors.toList())));
        SWCommandUtils.addMapper(SteamwarUser.class, SWCommandUtils.createMapper(SteamwarUser::get, s -> VelocityCore.getProxy().getAllPlayers().stream().map(Player::getUsername).collect(Collectors.toList())));
        SWCommandUtils.addMapper(ServerVersion.class, new TypeMapper<>() {
            @Override
            public ServerVersion map(Chatter sender, PreviousArguments previousArguments, String s) {
                Player player = sender.getPlayer();
                if (player != null && s.isEmpty()) {
                    ProtocolVersion version = player.getProtocolVersion();
                    if (version.greaterThan(ProtocolVersion.MINECRAFT_1_20_5)) {
                        return ServerVersion.PAPER_21;
                    } else if (version.greaterThan(ProtocolVersion.MINECRAFT_1_19_4)) {
                        return ServerVersion.PAPER_20;
                    } else if (version.greaterThan(ProtocolVersion.MINECRAFT_1_15_2)) {
                        return ServerVersion.PAPER_19;
                    } else if (version.greaterThan(ProtocolVersion.MINECRAFT_1_12_2)) {
                        return ServerVersion.SPIGOT_15;
                    } else {
                        return ServerVersion.SPIGOT_12;
                    }
                }

                return ServerVersion.get(s);
            }

            @Override
            public Collection<String> tabCompletes(Chatter sender, PreviousArguments previousArguments, String s) {
                return ServerVersion.chatVersions();
            }
        });

        AbstractTypeMapper<Object, Team> teamMapper = SWCommandUtils.createMapper(Team::get, s -> Team.getAll().stream().flatMap(team -> Stream.of(team.getTeamKuerzel(), team.getTeamName())).collect(Collectors.toList()));
        TabCompletionCache.add(teamMapper, true, 10, TimeUnit.SECONDS);
        SWCommandUtils.addMapper(Team.class, teamMapper);

        TypeMappers.init();
    }
}
