/*
 * 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.velocitycore.ArenaMode;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.TypeMapper;
import de.steamwar.command.TypeValidator;
import de.steamwar.messages.Chatter;
import de.steamwar.messages.PlayerChatter;
import de.steamwar.sql.Punishment;
import lombok.experimental.UtilityClass;

import java.util.Collection;

@UtilityClass
public class TypeMappers {

    public static void init() {
        SWCommandUtils.addValidator("arenaPlayer", arenaPlayer());
        SWCommandUtils.addMapper("nonHistoricArenaMode", arenaModeTypeMapper(false));
        SWCommandUtils.addMapper("historicArenaMode", arenaModeTypeMapper(true));
        SWCommandUtils.addMapper("arenaMap", arenaMapTypeMapper());
    }

    private static TypeValidator<PlayerChatter> arenaPlayer() {
        return (sender, player, messageSender) -> !PunishmentCommand.isPunishedWithMessage(player, Punishment.PunishmentType.NoFightServer);
    }

    private static TypeMapper<ArenaMode> arenaModeTypeMapper(boolean historic) {
        return new TypeMapper<>() {
            @Override
            public ArenaMode map(Chatter sender, PreviousArguments previousArguments, String s) {
                return ArenaMode.getByChat(s);
            }

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

    private static TypeMapper<String> arenaMapTypeMapper() {
        return new TypeMapper<>() {
            @Override
            public String map(Chatter sender, PreviousArguments previousArguments, String s) {
                if (previousArguments.userArgs.length == 0) return null;
                return ArenaMode.getByChat(previousArguments.userArgs[previousArguments.userArgs.length - 1]).convertToRealMapName(s);
            }

            @Override
            public Collection<String> tabCompletes(Chatter sender, PreviousArguments previousArguments, String s) {
                if (previousArguments.userArgs.length == 0) return null;
                ArenaMode arenaMode = ArenaMode.getByChat(previousArguments.userArgs[previousArguments.userArgs.length - 1]);
                if (arenaMode == null) return null;
                return arenaMode.getMaps();
            }
        };
    }
}
