/*
 * 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 de.steamwar.command.SWCommand;
import de.steamwar.messages.Chatter;
import de.steamwar.messages.Message;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.sql.UserElo;
import de.steamwar.velocitycore.ArenaMode;

import java.util.Optional;

public class RankCommand extends SWCommand {

    public RankCommand() {
        super("rank");
    }

    @Register
    public void ownRank(Chatter sender) {
        rank(sender, sender.user());
    }

    @Register
    public void rank(Chatter sender, @ErrorMessage("RANK_PLAYER_NOT_FOUND") SteamwarUser user) {
        if (!sender.user().equals(user))
            sender.prefixless("RANK_PLAYER_FOUND", user);

        for(ArenaMode mode : ArenaMode.getAllModes()) {
            if (!mode.isRanked())
                continue;

            Optional<Integer> elo = UserElo.getElo(user.getId(), mode.getSchemTypeOrInternalName());
            Message eloMsg;
            if (elo.isPresent()) {
                int placement = UserElo.getPlacement(elo.get(), mode.getSchemTypeOrInternalName());
                eloMsg = new Message("RANK_PLACED", placement, elo.get());
            } else {
                eloMsg = new Message("RANK_UNPLACED");
            }

            sender.prefixless("RANK_HEADER", UserElo.getEmblemProgression(mode.getChatName(), user.getId()), mode.getChatName(), eloMsg);
        }
    }
}
