---------------------------------------------------------------------------------
--
-- 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: PratChatTabs
Revision: $Revision: 25896 $
Author(s): Sylvanaar (sylvanaar@mindspring.com)
Website: http://www.wowace.com/files/index.php?path=Prat/
Documentation: http://www.wowace.com/wiki/Prat/Integrated_Modules#ChatTabs
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: Module for Prat that adds chat window tab options.
Dependencies: Prat
]]

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

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

L:RegisterTranslations("enUS", function() return {
    ["ChatTabs"] = true,
    ["Chat window tab options."] = true,
    ["SetMode"] = true,
    ["Toggle mode"] = true,
    ["SetAll"] = true,
    ["Set chat tabs for all windows."] = true,
    ["Individual"] = true,
    ["Toggle chat tabs for each window."] = true,
    ["Chat%dTab"] = true,
    ["Toggles chat tab options for chat window %d."] = true,
    ["Active Alpha"] = true,
    ["Sets alpha of chat tab for active chat frame."] = true,
    ["Not Active Alpha"] = true,
    ["Sets alpha of chat tab for not active chat frame."] = true,
    ["All"] = true,
    ["Individual"] = true,
    ["Always Visible"] = true,
    ["Hidden"] = true,
    ["Default"] = true,
    ["Flash Selected Tab"] = true,
    ["Toggle flashing the active chat tab on whispers."] = true,
} end)

L:RegisterTranslations("deDE", function() return {
    ["ChatTabs"] = "Chat Tasten",
    ["Chat window tab options."] = "Chat Tasten Optionen",
    ["SetMode"] = "Setze Modus",
    ["Toggle mode"] = "Schalte Modus",
    ["SetAll"] = "Setze Alle",
    ["Set chat tabs for all windows."] = "Setze Chat Tastens f\195\188r alle Fenster",
    ["Individual"] = "Individuell",
    ["Toggle chat tabs for each window."] = "Aktiviere Chat Tasten f\195\188r jedes Fenster",
    ["Chat%dTab"] = "Chat %d Taste",
    ["Toggles chat tab options for chat window %d."] = "Aktiviert Chat Tasten Optionen f\195\188r Chat Fenster %d.",
    ["Active Alpha"] = "Aktive Helligkeit",
    ["Sets alpha of chat tab for active chat frame."] = "Stellt die Helligkeit f\195\188r aktive Chattasten Fenster ein.",
    ["Not Active Alpha"] = "Keine Aktive Helligkeit",
    ["Sets alpha of chat tab for not active chat frame."] = "Stellt die Helligkeit f\195\188r nicht aktive Chattasten ein.",
    ["All"] = "Alle",
    ["Individual"] = "Individuell",
    ["Always Visible"] = "Immer sichtbar",
    ["Hidden"] = "Versteckt",
    ["Default"] = "Grundeinstellung",
    ["Flash Selected Tab"] = "Aufblitzen der benutzten Taste",
    ["Toggle flashing the active chat tab on whispers."] = "Aktiviert Aufblitzen der Chat Taste vom gegew\195\164rtig benutzen Chatfenster wenn eine gel\195\188sterte Nachricht ankommt.",
} end)

L:RegisterTranslations("esES", function() return {
    ["ChatTabs"] = "Pesta\195\177as de Chat",
    ["Chat window tab options."] = "Opciones de las pesta\195\177as de chat",
    ["SetMode"] = "Establecer Modo",
    ["Toggle mode"] = "Cambia el modo",
    ["SetAll"] = "Establecer Todos",
    ["Set chat tabs for all windows."] = "Establece las pesta\195\177as de chat para todas las ventanas",
    ["Individual"] = "Individual",
    ["Toggle chat tabs for each window."] = "Cambia las pesta\195\177as de chat para cada ventana",
    ["Chat%dTab"] = "Pesta\195\177a de Chat %d",
    ["Toggles chat tab options for chat window %d."] = "Determina las opciones de la pesta\195\177a de chat de la ventana de chat %d",
    ["Active Alpha"] = "Transparencia Activos",
    ["Sets alpha of chat tab for active chat frame."] = "Establece el \195\173ndice de transparencia de la pesta\195\177a de chat para los marcos de chat activos.",
    ["Not Active Alpha"] = "Transparencia No Activos",
    ["Sets alpha of chat tab for not active chat frame."] = "Establece el \195\173ndice de transparencia de la pesta\195\177a de chat para los marcos de chat no activos.",
    ["All"] = "Todos",
    ["Individual"] = "Individual",
    ["Always Visible"] = "Siempre Visible",
    ["Hidden"] = "Oculto",
    ["Default"] = "Por Defecto",
    ["Flash Selected Tab"] = "Destello en la Pesta\195\177a Seleccionada",
        ["Toggle flashing the active chat tab on whispers."] = "Determina si se muestra un destello en la pesta\195\177a del chat activo cuando te susurran",
} end)

L:RegisterTranslations("koKR", function() return {
    ["ChatTabs"] = "대화탭",
    ["Chat window tab options."] = "대화탭 설정",
    ["SetMode"] = "모드설정",
    ["Toggle mode"] = "모드 전환",
    ["SetAll"] = "모두설정",
    ["Set chat tabs for all windows."] = "모든 창에대한 대화탭을 설정합니다.",
    ["Individual"] = "개별",
    ["Toggle chat tabs for each window."] = "각 창에 대한 대화 탭을 전환합니다.",
    ["Chat%dTab"] = "대화%d탭",
    ["Toggles chat tab options for chat window %d."] = "%d 대화창에 대한 대화탭 설정을 전환합니다.",
    ["Active Alpha"] = "활성화 투명도",
    ["Sets alpha of chat tab for active chat frame."] = "활성화된 대화창에 대한 대화탭의 투명도를 설정합니다.",
    ["Not Active Alpha"] = "비활성화 투명도",
    ["Sets alpha of chat tab for not active chat frame."] = "비활성화 대화창에 대한 대화탭의 투명도를 설정합니다.",
    ["All"] = "모두",
    ["Individual"] = "개별",
    ["Always Visible"] = "항상 표시",
    ["Hidden"] = "숨김",
    ["Default"] = "기본값",
    ["Flash Selected Tab"] = "선택된 탭 강조",
    ["Toggle flashing the active chat tab on whispers."] = "귓속말시 활성화된 대화탭 강조를 전환합니다.",
} end)

L:RegisterTranslations("frFR", function() return {
    ["ChatTabs"] = "Onglets",
    ["Chat window tab options."] = "Options des onglets.",
    ["SetMode"] = "Mode de r\195\169glage",
    ["Toggle mode"] = "Change le mode de r\195\169glage.",
    ["SetAll"] = "Toutes les fen\195\170tres",
    ["Set chat tabs for all windows."] = "R\195\168gle les onglets pour toutes les fen\195\170tres à la fois.",
    ["Individual"] = "Chaque fenêtre",
    ["Toggle chat tabs for each window."] = "R\195\168gle les onglets fen\195\170tre par fen\195\170re.",
    ["Chat%dTab"] = "Onglets de la fen\195\170tre %d",
    ["Toggles chat tab options for chat window %d."] = "Change les options d'onglet de la fen\195\170tre %d.",
    ["Active Alpha"] = "Transparence lorsque actif",
    ["Sets alpha of chat tab for active chat frame."] = "R\195\168gle la transparence des onglets de la fen\195\170tre active.",
    ["Not Active Alpha"] = "Transparence lorsque inactif",
    ["Sets alpha of chat tab for not active chat frame."] = "R\195\168gle la transparence des onglets des fen\195\170tres inactives.",
    ["All"] = "Toutes les fen\195\170tres",
    ["Individual"] = "Fen\195\170tre par fen\195\170tre",
    ["Always Visible"] = "Toujours visible",
    ["Hidden"] = "Cach\195\169s",
    ["Default"] = "Par d\195\169faut",
    ["Flash Selected Tab"] = "Faire clignoter l'onglet actif",
    ["Toggle flashing the active chat tab on whispers."] = "Active ou désactive le clignotement de l'onglet actif lorsqu'un message est recu.",
} end)

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

-- create prat module
Prat_ChatTabs = Prat:NewModule(PRAT_MODULE)
Prat_ChatTabs.revision = tonumber(string.sub("$Revision: 25896 $", 12, -3))

-- define key module values
Prat_ChatTabs.moduleName = L["ChatTabs"]
Prat_ChatTabs.moduleDesc = L["Chat window tab options."]
Prat_ChatTabs.consoleName = "chattabs"
Prat_ChatTabs.guiName = L["ChatTabs"]
Prat_ChatTabs.Categories = { cat.FRAME }

-- define the default db values
Prat_ChatTabs.defaultDB = {
        on = false,
        notactivealpha = 0.5,
        activealpha = 1,
        mode = "ALL",
        cf = {"ALWAYS", "ALWAYS", "ALWAYS", "ALWAYS", "ALWAYS", "ALWAYS", "ALWAYS"},
        cfall = "ALWAYS",
        flashselected = false,
}

-- create a moduleOptions stub (for setting self.moduleOptions)
Prat_ChatTabs.moduleOptions = {}

-- add module options not covered by templates
function Prat_ChatTabs:GetModuleOptions()
     self.moduleOptions = {
        name = L["ChatTabs"],
        desc = L["Chat window tab options."],
        type = "group",
        args = {
            mode = {
                name = L["SetMode"],
                desc = L["Toggle mode"],
                type = "text",
                order = 100,
                get = function() return self.db.profile.mode end,
                set = function(v) self.db.profile.mode = v self:OnUpdate() end,
                validate = {["ALL"] = L["All"], ["INDIVIDUAL"] = L["Individual"]},
            },
            all = {
                name = L["SetAll"],
                desc = L["Set chat tabs for all windows."],
                type = "text",
                disabled = function() if self.db.profile.mode == "INDIVIDUAL" then return true else return false end end,
                order = 110,
                get = function() return self.db.profile.cfall end,
                set = function(v) self.db.profile.cfall = v self:OnUpdate() end,
                validate = {["ALWAYS"] = L["Always Visible"], ["HIDDEN"] = L["Hidden"]},
            },
            individual = {
                name = L["Individual"],
                desc = L["Toggle chat tabs for each window."],
                type = "group",
                disabled = function() if self.db.profile.mode == "ALL" then return true else return false end end,
                order = 120,
                args = {
                    chat1 = {
                        name = string.format(L["Chat%dTab"], 1),
                        desc = string.format(L["Toggles chat tab options for chat window %d."], 1),
                        type = "text",
                        get = function() return self.db.profile.cf[1] end,
                        set = function(v)
                            self.db.profile.cf[1] = v
                            self:OnUpdate()
                            if self.db.profile.cf[1] == "DEFAULT" then
                                getglobal("ChatFrame1Tab"):Hide()
                            end
                        end,
                        validate = {["ALWAYS"] = L["Always Visible"], ["HIDDEN"] = L["Hidden"], ["DEFAULT"] = L["Default"]},
                    },
                    chat2 = {
                        name = string.format(L["Chat%dTab"], 2),
                        desc = string.format(L["Toggles chat tab options for chat window %d."], 2),
                        type = "text",
                        get = function() return self.db.profile.cf[2] end,
                        set = function(v)
                            self.db.profile.cf[2] = v
                            self:OnUpdate()
                            if self.db.profile.cf[2] == "DEFAULT" then
                                getglobal("ChatFrame2Tab"):Hide()
                            end
                        end,
                        validate = {["ALWAYS"] = L["Always Visible"], ["HIDDEN"] = L["Hidden"], ["DEFAULT"] = L["Default"]},
                    },
                    chat3 = {
                        name = string.format(L["Chat%dTab"], 3),
                        desc = string.format(L["Toggles chat tab options for chat window %d."], 3),
                        type = "text",
                        get = function() return self.db.profile.cf[3] end,
                        set = function(v)
                            self.db.profile.cf[3] = v
                            self:OnUpdate()
                            if self.db.profile.cf[3] == "DEFAULT" then
                                getglobal("ChatFrame3Tab"):Hide()
                            end
                        end,
                        validate = {["ALWAYS"] = L["Always Visible"], ["HIDDEN"] = L["Hidden"], ["DEFAULT"] = L["Default"]},
                    },
                    chat4 = {
                        name = string.format(L["Chat%dTab"], 4),
                        desc = string.format(L["Toggles chat tab options for chat window %d."], 4),
                        type = "text",
                        get = function() return self.db.profile.cf[4] end,
                        set = function(v)
                            self.db.profile.cf[4] = v
                            self:OnUpdate()
                            if self.db.profile.cf[4] == "DEFAULT" then
                                getglobal("ChatFrame4Tab"):Hide()
                            end
                        end,
                        validate = {["ALWAYS"] = L["Always Visible"], ["HIDDEN"] = L["Hidden"], ["DEFAULT"] = L["Default"]},
                    },
                    chat5 = {
                        name = string.format(L["Chat%dTab"], 5),
                        desc = string.format(L["Toggles chat tab options for chat window %d."], 5),
                        type = "text",
                        get = function() return self.db.profile.cf[5] end,
                        set = function(v)
                            self.db.profile.cf[5] = v
                            self:OnUpdate()
                            if self.db.profile.cf[5] == "DEFAULT" then
                                getglobal("ChatFrame5Tab"):Hide()
                            end
                        end,
                        validate = {["ALWAYS"] = L["Always Visible"], ["HIDDEN"] = L["Hidden"], ["DEFAULT"] = L["Default"]},
                    },
                    chat6 = {
                        name = string.format(L["Chat%dTab"], 6),
                        desc = string.format(L["Toggles chat tab options for chat window %d."], 6),
                        type = "text",
                        get = function() return self.db.profile.cf[6] end,
                        set = function(v)
                            self.db.profile.cf[6] = v
                            self:OnUpdate()
                            if self.db.profile.cf[6] == "DEFAULT" then
                                getglobal("ChatFrame6Tab"):Hide()
                            end
                        end,
                        validate = {["ALWAYS"] = L["Always Visible"], ["HIDDEN"] = L["Hidden"], ["DEFAULT"] = L["Default"]},
                    },
                    chat7 = {
                        name = string.format(L["Chat%dTab"], 7),
                        desc = string.format(L["Toggles chat tab options for chat window %d."], 7),
                        type = "text",
                        get = function() return self.db.profile.cf[7] end,
                        set = function(v)
                            self.db.profile.cf[7] = v
                            self:OnUpdate()
                            if self.db.profile.cf[7] == "DEFAULT" then
                                getglobal("ChatFrame7Tab"):Hide()
                            end
                        end,
                        validate = {["ALWAYS"] = L["Always Visible"], ["HIDDEN"] = L["Hidden"], ["DEFAULT"] = L["Default"]},
                    },
                },
            },
            activealpha = {
                name = L["Active Alpha"],
                desc = L["Sets alpha of chat tab for active chat frame."],
                type = "range",
                get = function() return self.db.profile.activealpha end,
                set = function(v) self.db.profile.activealpha = v self:OnUpdate() end,
                min = 0.1,
                max = 1,
                step = 0.1,
                order = 200,
            },
            notactivealpha = {
                name = L["Not Active Alpha"],
                desc = L["Sets alpha of chat tab for not active chat frame."],
                type = "range",
                get = function() return self.db.profile.notactivealpha end,
                set = function(v) self.db.profile.notactivealpha = v self:OnUpdate() end,
                min = 0.1,
                max = 1,
                step = 0.1,
                order = 210,
            },
            flashselected = {
                name = L["Flash Selected Tab"],
                desc = L["Toggle flashing the active chat tab on whispers."],
                type = "toggle",
                get = function() return self.db.profile.flashselected end,
                set = function(v) self.db.profile.flashselected = v  Prat_ChatTabs:HookFlash() end,
                order = 211
            }
        }
    }
    return self.moduleOptions
end

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

-- things to do when the module is enabled
function Prat_ChatTabs:OnModuleEnable()
    self:SecureHook("FCF_OnUpdate", "OnUpdate")
    self:HookFlash()
    self:OnUpdate()
end

-- things to do when the module is enabled
function Prat_ChatTabs:OnModuleDisable()
    if self:IsHooked("FCF_OnUpdate") then
        self:Unhook("FCF_OnUpdate")
    end
end

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

function Prat_ChatTabs:HookFlash()
    if self.db.profile.flashselected then
        if not self:IsHooked("FCF_FlashTab") then
            self:SecureHook("FCF_FlashTab")
        end
    else
        if self:IsHooked("FCF_FlashTab") then
            self:Unhook("FCF_FlashTab")
        end
    end
end

function Prat_ChatTabs:OnUpdate(elapsed)
    if self.db.profile.on then
        for i=1,NUM_CHAT_WINDOWS do
            local cftab = getglobal("ChatFrame"..i.."Tab")
            if getglobal("ChatFrame"..i):IsVisible() or getglobal("ChatFrame"..i).isDocked then
                if self.db.profile.mode == "ALL" then
                    if SELECTED_CHAT_FRAME:GetID() == i then
                        cftab:SetAlpha(self.db.profile.activealpha)
                    else
                        cftab:SetAlpha(self.db.profile.notactivealpha)
                    end
                    if self.db.profile.cfall == "ALWAYS" then
                        cftab:Show()
                    elseif self.db.profile.cfall == "HIDDEN" then
                        cftab:Hide()
                    end
                else
                    if SELECTED_CHAT_FRAME:GetID() == i then
                        cftab:SetAlpha(self.db.profile.activealpha)
                    else
                        cftab:SetAlpha(self.db.profile.notactivealpha)
                    end
                    if self.db.profile.cf[i] == "ALWAYS" then
                        cftab:Show()
                    elseif self.db.profile.cf[i] == "HIDDEN" then
                        cftab:Hide()
                    end
                end
            end
        end
    end
end

function Prat_ChatTabs:FCF_FlashTab()
    local tabFlash = getglobal(this:GetName().."TabFlash");
    if ( not this.isDocked or UIFrameIsFlashing(tabFlash) ) then
        return;
    end
    tabFlash:Show();
    UIFrameFlash(tabFlash, 0.25, 0.25, 60, nil, 0.5, 0.5);
end