---@class aura_env
---@field config table
---@field whoQueued boolean
---@field GetPlayer fun(name: string): Player

local channelname = aura_env.config.channelname or "lAKDJjZfNgobMblAangc"
aura_env.whoQueued = false

if not WeakAurasSaved.Cyka then WeakAurasSaved.Cyka = {} end
if not WeakAurasSaved.Cyka.RaceCache then WeakAurasSaved.Cyka.RaceCache = {} end

aura_env.GetPlayer = function(name)
	local player = WeakAurasSaved.Cyka.RaceCache[name] or Player.new(name)
	WeakAurasSaved.Cyka.RaceCache[name] = player
	return player
end

---@class Player
---@field name string
---@field guild string|nil
---@field race string|nil
---@field health number
---@field whoQueued boolean
Player = {
	---@param name string
	---@return Player
	new = function(name)
		local self = setmetatable({}, {
			__index = Player,
		})
		self.name = name
		self.whoQueued = false
		self.health = 0
		return self
	end,
	QueryIfNecessary = function(self)
		if self.whoQueued then return end
		if self.race ~= nil then return end
		self:QueryWho()
	end,
	QueryWho = function(self)
		if aura_env.whoQueued then
			print(string.format("There is already a who query queued, waiting for player %s", self.name))
			C_Timer.After(0.5, function() self:QueryWho() end)
			return
		end
		if self.whoQueued then
			print(string.format("Player %s is already queued", self.name))
			return
		end
		local query = string.format('n-"%s"', self.name)
		print(string.format("Queueing who query for player %s: %s", self.name, query))
		self.whoQueued = true
		aura_env.whoQueued = true
		SendWho(query)
	end,
}

--[09:01 PM] Dump: value=GetChannelList()
--[09:01 PM] [1]=1,
--[09:01 PM] [2]="world_en",
--[09:01 PM] [3]=2,
--[09:01 PM] [4]="world_ru",
--[09:01 PM] [5]=3,
--[09:01 PM] [6]="world_hc",
--[09:01 PM] [7]=6,
--[09:01 PM] [8]="lAKDJjZfNgobMblAangc"
--
-- zoneChannel, channelName = JoinPermanentChannel("name" [, "password" [, chatFrameIndex [, enableVoice]]])
--
--[09:03 PM] Dump: value=GetChannelDisplayInfo(6)
--[09:03 PM] [1]="world_hc",
--[09:03 PM] [2]=false,
--[09:03 PM] [4]=3,
--[09:03 PM] [5]=1258,
--[09:03 PM] [6]=true,
--[09:03 PM] [7]="CHANNEL_CATEGORY_CUSTOM",
--[09:03 PM] [8]=false
--
-- channelCount = GetNumDisplayChannels()
--
--
-- SendChatMessage("text" [, "chatType" [, languageIndex [, "channel"]]])
--Arguments:
--    text - Message to be sent (up to 255 characters) (string)
--    chatType - Channel on which to send the message (defaults to SAY if omitted) (string)
--        CHANNEL - Message to a server or custom chat channel (sent with /1, /2, etc in the default UI); requires channel number for channel argument
--        DND - Enables Away-From-Keyboard status for the player, with text as the custom message seen by others attempting to whisper the player
--        EMOTE - Custom text emotes visible to nearby players (sent with /e in the default UI)
--        GUILD - Messages to guild members (sent with /g in the default UI)
--        INSTANCE_CHAT - Messages to a LFG/LFR instance group (sent with /i in the default UI)
--        OFFICER - Messages to guild officers (sent with /o in the default UI)
--        PARTY - Messages to party members (sent with /p in the default UI)
--        RAID - Messages to raid members (sent with /ra in the default UI)
--        RAID_WARNING - Warning to raid members (sent with /rw in the default UI)
--        SAY - Speech to nearby players (sent with /s in the default UI)
--        WHISPER - Message to a specific character (sent with /w in the default UI); requires name of the character for channel argument
--        YELL - Yell to not-so-nearby players (sent with /y in the default UI)
--    languageIndex - Language in which to send the message; defaults to Common (for Alliance players) or Orcish (for Horde players) if omitted. Language indices can be retrieved from GetLanguageByIndex(). (number)
--    channel - If chatType is WHISPER, name of the target character; if chatType is CHANNEL, number identifying the target channel; ignored otherwise (string)
--
-- language = GetDefaultLanguage()
