import { Collection, Guild, GuildMember, Role } from "discord.js"
import { ChangedRoles, Maybe, PermissionLevel } from "../interfaces"
import { logger } from "../logger"
import { config } from "../configuration"

export function filterRolesFromMemberUpdate(oldMember: GuildMember, newMember: GuildMember): ChangedRoles {

	const oldRoles = oldMember.roles.cache
	const newRoles = newMember.roles.cache

	const removedRoles = oldRoles.filter(x => newRoles.find(y => y.id === x.id) == undefined)
	const addedRoles = newRoles.filter(x => oldRoles.find(y => y.id === x.id) == undefined)
	logger.info(`Member ${oldMember.id} RemovedRoles: ${removedRoles.map(x => x.name)}`, { guildId: oldMember.guild.id })
	logger.info(`Member ${oldMember.id} AddedRoles: ${addedRoles.map(x => x.name)}`, { guildId: oldMember.guild.id })

	return { addedRoles, removedRoles }
}

export async function getMembersWithRoleFromGuild(roleId: string, guild: Guild): Promise<Collection<string, GuildMember>> {
	const emptyResponse = new Collection<string, GuildMember>
	const guildRole: Maybe<Role> = guild.roles.resolve(roleId)
	if (!guildRole) return emptyResponse
	return guildRole.members
}

export function getGuildSpecificTriggerRoleId(): Collection<string, PermissionLevel> {
	const outVal = new Collection<string, PermissionLevel>()
	outVal.set(config.bot.watcher_role, "VIEWER")
	outVal.set(config.bot.jf_admin_role, "ADMIN")
	return outVal
}
