package com.namelessmc.plugin.common;

import com.namelessmc.plugin.common.audiences.NamelessPlayer;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
import net.luckperms.api.query.QueryOptions;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Set;
import java.util.stream.Collectors;

public class LuckPermsPermissions extends AbstractPermissions {

	private @Nullable LuckPerms api;

	@Override
	public void unload() {
		this.api = null;
	}

	@Override
	public void load() {
		try {
			this.api = LuckPermsProvider.get();
		} catch (IllegalStateException | NoClassDefFoundError e) {}
	}

	@Override
	public boolean isUsable() {
		return this.api != null;
	}

	@Override
	public Set<String> getGroups() {
		if (this.api == null) {
			throw new ProviderNotUsableException();
		}
		return this.api.getGroupManager().getLoadedGroups().stream()
				.map(Group::getName)
				.collect(Collectors.toUnmodifiableSet());
	}

	@Override
	public @Nullable Set<String> getPlayerGroups(NamelessPlayer player) {
		if (this.api == null) {
			throw new ProviderNotUsableException();
		}
		final User user = this.api.getUserManager().getUser(player.uuid());
		if (user == null) {
			return null;
		}
		return user.getInheritedGroups(QueryOptions.defaultContextualOptions()).stream()
				.map(Group::getName)
				.collect(Collectors.toUnmodifiableSet());
	}
}

@@ -264,53 +264,8 @@
 er;%0A
-import net.luckperms.api.query.QueryOptions;%0A
 impo
@@ -321,16 +321,46 @@
 lable;%0A%0A
+import java.util.Collections;%0A
 import j
@@ -1304,24 +1304,82 @@
 n null;%0A%09%09%7D%0A
+%09%09return Collections.singleton(user.getPrimaryGroup());%0A//
 %09%09return use
@@ -1441,32 +1441,34 @@
 ons()).stream()%0A
+//
 %09%09%09%09.map(Group::
@@ -1468,32 +1468,34 @@
 Group::getName)%0A
+//
 %09%09%09%09.collect(Col

Send primary group only