local _, ns = ...
local MythicTools = ns.MythicTools

function MythicTools:QueueGroupScan()
    if not self.runtime or self.runtime.groupScanQueued then
        return
    end

    self.runtime.groupScanQueued = true
    self:Schedule(0.15, function()
        if not self.runtime then
            return
        end

        self.runtime.groupScanQueued = false
        self:ScanKnownPlayersInGroup()
    end)
end

function MythicTools:PrintHistoryAlert(message)
    print(tostring(message or ""))
end

function MythicTools:ScanKnownPlayersInGroup()
    if not (self.db and self.db.settings) then
        return
    end

    local settings = self.db.settings
    if settings.historyAlertChat == false and not settings.historyAlertFloating and not settings.historyAlertSound then
        return
    end

    local roster = self:GetCurrentGroupRoster()
    local previousGroupPlayers = self.runtime.currentGroupPlayers or {}
    local currentGroupPlayers = {}

    if #roster <= 1 then
        self.runtime.currentGroupPlayers = currentGroupPlayers
        return
    end

    for _, entry in ipairs(roster) do
        currentGroupPlayers[entry.name] = true

        if entry.name ~= self.playerName and not self:IsPlaceholderName(entry.name) then
            local playerEntry = self.db.playersIndex[entry.name]
            if playerEntry and not previousGroupPlayers[entry.name] then
                if settings.historyAlertChat ~= false then
                    self:PrintHistoryAlert(self:BuildKnownPlayerMessage(entry.name, playerEntry, "chat"))
                end
                if self.ShowHistoryFloatingAlert then
                    self:ShowHistoryFloatingAlert(self:BuildKnownPlayerMessage(entry.name, playerEntry, "floating"))
                end
                if self.PlayHistoryAlertSound then
                    self:PlayHistoryAlertSound()
                end
            end
        end
    end

    self.runtime.currentGroupPlayers = currentGroupPlayers
end
