package me.km.snuviscript.commands;

import com.mojang.authlib.GameProfile;
import java.util.Date;
import java.util.GregorianCalendar;
import me.hammerle.snuviscript.code.ScriptManager;
import static me.km.snuviscript.commands.CommandUtils.getUUID;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.ProfileBanEntry;
import net.minecraft.util.text.StringTextComponent;

public class BanCommands {
    public static void registerFunctions(ScriptManager sm, MinecraftServer server) {
        sm.registerConsumer("ban.kick", (sc, in) -> {
            ((ServerPlayerEntity) in[0].get(sc)).connection.disconnect(new StringTextComponent(in[1].getString(sc)));
        });
        sm.registerConsumer("ban.add", (sc, in) -> {
            GameProfile gp = server.getPlayerProfileCache().getProfileByUUID(getUUID(in[0].get(sc)));
            String reason = in[1].getString(sc);
            String banner = in[2].getString(sc);
            ProfileBanEntry entry;
            if(in.length >= 4) {
                GregorianCalendar calender = (GregorianCalendar) in[3].get(sc);
                Date d = new Date(calender.getTimeInMillis());
                entry = new ProfileBanEntry(gp, null, banner, d, reason);
            } else {
                entry = new ProfileBanEntry(gp, null, banner, null, reason);
            }
            server.getPlayerList().getBannedPlayers().addEntry(entry);
        });
        sm.registerConsumer("ban.remove", (sc, in) -> {
            GameProfile gp = server.getPlayerProfileCache().getProfileByUUID(getUUID(in[0].get(sc)));
            server.getPlayerList().getBannedPlayers().removeEntry(gp);
        });
    }
}
