---------------------------------------------------------------------------------
--
-- Prat - A framework for World of Warcraft chat mods
--
-- Copyright (C) 2006-2007  Prat Development Team
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to:
--
-- Free Software Foundation, Inc., 
-- 51 Franklin Street, Fifth Floor, 
-- Boston, MA  02110-1301, USA.
--
--
-------------------------------------------------------------------------------



--[[
Name: Prat_FixBlizzardChannelUnhiding
Revision: $Revision:  r24999$
Author(s): SlackerJer (slackerjer@gmail.com)
Website: http://www.wowace.com/files/index.php?path=Prat/
Documentation: http://www.wowace.com/wiki/Prat
Subversion: http://svn.wowace.com/wowace/trunk/Prat/
Discussions: http://groups.google.com/group/wow-prat
Issues and feature requests: http://code.google.com/p/prat/issues/list
Description:  Fix for Blizzard unhiding channels in chat frame on subsequent logins (reference: http://forums.worldofwarcraft.com/thread.html?topicId=71027742 et al)
Dependencies: Prat
]]

-- set prat module name
local PRAT_MODULE = Prat:RequestModuleName("FixBlizzardChannelUnhiding")

-- define localized strings
local L = AceLibrary("AceLocale-2.2"):new(PRAT_MODULE)

L:RegisterTranslations("enUS", function() return {
    ["FixBlizzardChannelUnhiding"] = true,
    ["Fix for Blizzard unhiding channels in chat frame on subsequent logins"] = true,
} end)

L:RegisterTranslations("deDE", function() return {
    ["FixBlizzardChannelUnhiding"] = "FixBlizzardKanalUnverbergen",
    ["Fix for Blizzard unhiding channels in chat frame on subsequent logins"] = "Fix f\195\188r Blizzard's nicht verbergen von Kanälen im Chat bei nachfolgen Logins.",
} end)

L:RegisterTranslations("frFR", function() return {
    ["FixBlizzardChannelUnhiding"] = "FixBlizzardChannelUnhiding",
    ["Fix for Blizzard unhiding channels in chat frame on subsequent logins"] = "Fix for Blizzard unhiding channels in chat frame on subsequent logins",
} end)

L:RegisterTranslations("esES", function() return {
    ["FixBlizzardChannelUnhiding"] = "FixBlizzardChannelUnhiding",
    ["Fix for Blizzard unhiding channels in chat frame on subsequent logins"] = "Fix for Blizzard unhiding channels in chat frame on subsequent logins",
} end)

L:RegisterTranslations("koKR", function() return {
    ["FixBlizzardChannelUnhiding"] = "브리자드채널버그수정",
    ["Fix for Blizzard unhiding channels in chat frame on subsequent logins"] = "접속 직후 대화창에 브리자드 채널 버그를 수정합니다.",
} end)

-- get prat module categories
local cat = Prat.Categories

-- create prat module

Prat_FixBlizzardChannelUnhiding = Prat:NewModule(PRAT_MODULE)
Prat_FixBlizzardChannelUnhiding.revision = tonumber(string.sub("$Revision:  24999 $", 12, -3))

-- define key module values
Prat_FixBlizzardChannelUnhiding.moduleName = L["FixBlizzardChannelUnhiding"]
Prat_FixBlizzardChannelUnhiding.moduleDesc = L["Fix for Blizzard unhiding channels in chat frame on subsequent logins"]

Prat_FixBlizzardChannelUnhiding.consoleName = string.lower(Prat_FixBlizzardChannelUnhiding.moduleName)
Prat_FixBlizzardChannelUnhiding.guiName = Prat_FixBlizzardChannelUnhiding.moduleName

Prat_FixBlizzardChannelUnhiding.Categories = { cat.FRAME, cat.BEHAVIOR }


Prat_FixBlizzardChannelUnhiding.defaultDB = {
    on = false,
}

Prat_FixBlizzardChannelUnhiding.toggleOptions = { }

--[[------------------------------------------------
    Module Event Functions
------------------------------------------------]]--

function Prat_FixBlizzardChannelUnhiding:OnModuleEnable()
	self:ScheduleEvent("ReconcileChannelLists", self.ReconcileChannelLists, 10, self); -- schedule a check after channels have been dealt with, doesn't seem to happen immediately
end

function Prat_FixBlizzardChannelUnhiding:OnModuleDisable()
	self:UnregisterAllEvents();
end

--[[------------------------------------------------
    Core Functions
------------------------------------------------]]--

function Prat_FixBlizzardChannelUnhiding:ReconcileChannelLists()
	local i,j;
	for i = 1, 10 do -- iterate chat frames
		local chatframe = getglobal("ChatFrame"..i);
		if (chatframe) then -- chat frame exists
			self:Debug("Checking ChatFrame" .. i);
			local displayChannels = {};
			for _, chan in ipairs(chatframe.channelList) do -- channelList should still be our good list of active channels for this frame at this point
				displayChannels[string.lower(chan)] = 1;
			end
			local windowChans = { GetChatWindowChannels(chatframe:GetID()) }
			for j=1, #windowChans, 2 do
				if (displayChannels[string.lower(windowChans[j])] ~= 1) then -- wasn't found in our list of channels that should be displayed
					self:Debug("Rehiding " .. windowChans[j] .. " in this chat frame");
					-- re-hide it!
					RemoveChatWindowChannel(chatframe:GetID(), windowChans[j]);
				end
			end
		end
	end
end