---@type string[]
local channelNames = {}
---@type string[]
local channelPasswords = {}
---@type string|nil
local err
---@type string
local chatFrameName = aura_env.config.chatFrameName or "Auto"
---@type string[]
local subscribedMessageGroups = {}
---@type string[]
local subscribedChannels = {}

---@param input string
---@param deliminer string
---@return string[], string|nil
local function StrSplit(input, deliminer)
	if not deliminer then return {}, "deliminer is nil" end
	if not input then return {}, "input is nil" end
	local parts = {}
	for part in string.gmatch(input, "([^" .. deliminer .. "]+)") do
		table.insert(parts, strtrim(part))
	end
	return parts, nil
end

channelNames, err = StrSplit(aura_env.config.channels, ",")
if err then
	print(string.format("Error splitting channels: %s", err))
	return
end

channelPasswords, err = StrSplit(aura_env.config.channelPasswords, ",")
if err then
	print(string.format("Error splitting channel passwords: %s", err))
	return
end

subscribedMessageGroups, err = StrSplit(aura_env.config.subscribedMessageGroups, ",")
if err then
	print(string.format("Error splitting subscribed message groups: %s", err))
	return
end

subscribedChannels, err = StrSplit(aura_env.config.subscribedChannels, ",")
if err then
	print(string.format("Error splitting subscribed channels: %s", err))
	return
end

aura_env.JoinChannels = function()
	for i, channelName in ipairs(channelNames) do
		channelName = strtrim(channelName)
		local password = nil
		if i <= #channelPasswords then password = strtrim(channelPasswords[i]) end
		JoinPermanentChannel(channelName, password)
	end
end

aura_env.FindOrCreateChatWindow = function()
	for i = 1, NUM_CHAT_WINDOWS do
		local name = GetChatWindowInfo(i)
		if name == chatFrameName then
			print(string.format("Found chat window %s", name))
			local frame = _G["ChatFrame" .. i]
			if FCF_IsValidChatFrame(frame) then
				print(string.format("Frame %s is valid", name))
				return frame
			else
				print(string.format("Frame %s is invalid", name))
			end
		end
	end

	print("Opening new chat window")
	FCF_OpenNewWindow(chatFrameName)
	for i = 1, NUM_CHAT_WINDOWS do
		local name = GetChatWindowInfo(i)
		if name == chatFrameName then
			print(string.format("Found chat window %s", name))
			local frame = _G["ChatFrame" .. i]
			if FCF_IsValidChatFrame(frame) then
				print(string.format("Frame %s is valid", name))
				return frame
			else
				print(string.format("Frame %s is invalid", name))
			end
		end
	end

	return nil
end

aura_env.ConfigureChatWindow = function()
	local general = _G["ChatFrame1"]
	local chatFrame = aura_env.FindOrCreateChatWindow()
	if not chatFrame then
		print(string.format("Chat window %s not found", chatFrameName))
		return
	end

	--SetChatWindowName(chatFrame, chatFrameName)
	print("Removing all message groups")
	ChatFrame_RemoveAllMessageGroups(chatFrame)
	print("Removing all channels")
	ChatFrame_RemoveAllChannels(chatFrame)
	print("Adding message groups")
	for _, messageGroup in ipairs(subscribedMessageGroups) do
		print(string.format("Adding message group: %s", messageGroup))
		ChatFrame_AddMessageGroup(chatFrame, strtrim(messageGroup))
		ChatFrame_RemoveMessageGroup(general, strtrim(messageGroup))
	end
	print("Adding channels")
	for _, channel in ipairs(subscribedChannels) do
		print(string.format("Adding channel: %s", channel))
		ChatFrame_AddChannel(chatFrame, strtrim(channel))
		ChatFrame_RemoveChannel(general, strtrim(channel))
	end
end

aura_env.ConfigureChatWindow()
