/*
 *   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 de.steamwar.sql.BauweltMember;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SteamwarUser;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

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

@UtilityClass
public class TypeUtils {

    static void init() {
        SWCommandUtils.addMapper(Player.class, SWCommandUtils.createMapper(Bukkit::getPlayer, s -> Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList())));
        SWCommandUtils.addMapper(GameMode.class, SWCommandUtils.createMapper(s -> {
            s = s.toLowerCase();
            if (s.equals("s") || s.equals("survival") || s.equals("0")) return GameMode.SURVIVAL;
            if (s.equals("c") || s.equals("creative") || s.equals("1")) return GameMode.CREATIVE;
            if (s.equals("sp") || s.equals("spectator") || s.equals("3")) return GameMode.SPECTATOR;
            if (s.equals("a") || s.equals("adventure") || s.equals("2")) return GameMode.ADVENTURE;
            return null;
        }, s -> Arrays.asList("s", "survival", "0", "c", "creative", "1", "sp", "spectator", "3", "a", "adventure", "2")));
        SWCommandUtils.addMapper(SteamwarUser.class, SWCommandUtils.createMapper(SteamwarUser::get, s -> Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList())));
        SWCommandUtils.addMapper(SchematicNode.class, new TypeMapper<SchematicNode>() {
            @Override
            public SchematicNode map(CommandSender commandSender, String[] previousArguments, String s) {
                return SchematicNode.getNodeFromPath(SteamwarUser.get(((Player) commandSender).getUniqueId()), s);
            }

            @Override
            public Collection<String> tabCompletes(CommandSender sender, String[] previousArguments, String s) {
                return SchematicNode.getNodeTabcomplete(SteamwarUser.get(((Player) sender).getUniqueId()), s);
            }
        });
        SWCommandUtils.addMapper(BauweltMember.class, new TypeMapper<BauweltMember>() {
            @Override
            public BauweltMember map(CommandSender commandSender, String[] previousArguments, String s) {
                if (!(commandSender instanceof Player)) {
                    return null;
                }
                Player player = (Player) commandSender;
                return BauweltMember.getMembers(player.getUniqueId())
                        .stream()
                        .filter(member -> SteamwarUser.byId(member.getMemberID()).getUserName().equalsIgnoreCase(s))
                        .findAny()
                        .orElse(null);
            }

            @Override
            public Collection<String> tabCompletes(CommandSender sender, String[] previousArguments, String s) {
                if (!(sender instanceof Player)) {
                    return new ArrayList<>();
                }
                Player player = (Player) sender;
                return BauweltMember.getMembers(player.getUniqueId())
                        .stream()
                        .map(m -> SteamwarUser.byId(m.getMemberID()).getUserName())
                        .collect(Collectors.toList());
            }
        });
    }
}
