
local L = AceLibrary("AceLocale-2.2"):new("PratEventNames")

L:RegisterTranslations("enUS", function() return {
    ["EventNames"] = true,
    ["EventNames options."] = true,
    ["Toggle"] = true,
    ["Toggle the module on and off."] = true,
    ["Show"] = true,
    ["Toggle showing event names for each window."] = true,
    ["Chat%dEventNamesShow"] = true,
    ["Toggles showing event names on and off for chat window %d."] = true
} end)

L:RegisterTranslations("zhCN", function() return {
    ["EventNames"] = "事件名称",
    ["EventNames options."] = "事件名称设置",
    ["Toggle"] = "切换",
    ["Toggle the module on and off."] = "切换此模块打开与关闭",
    ["Show"] = "显示",
    ["Toggle showing event names for each window."] = "切换是否在每个窗口显示事件名称",
    ["Chat%dEventNamesShow"] = "显示窗口%d事件名称",
    ["Toggles showing event names on and off for chat window %d."] = "切换是否在聊天窗口%d显示事件名称."
} end)

L:RegisterTranslations("koKR", function() return {
    ["EventNames"] = "이벤트 이름",
    ["EventNames options."] = "이벤트 이름 옵션",
    ["Toggle"] = "전환",
    ["Toggle the module on and off."] = "모듈 켜고 끄기를 전환합니다.",
    ["Show"] = "표시",
    ["Toggle showing event names for each window."] = "각 창에 대한 이벤트 이름 표시를 전환합니다.",
    ["Chat%dEventNamesShow"] = "대화%d 이벤트 이름 표시",
    ["Toggles showing event names on and off for chat window %d."] = "대화창%d에 대한 이벤트 이름 켜고 끄기를 전환합니다."
} end)

L:RegisterTranslations("zhTW", function() return {
    ["EventNames"] = "事件名稱",
    ["EventNames options."] = "事件名稱選項。",
    ["Toggle"] = "切換",
    ["Toggle the module on and off."] = "切換打開或關閉此模組。",
    ["Show"] = "顯示",
    ["Toggle showing event names for each window."] = "切換是否在每個視窗顯示事件名稱。",
    ["Chat%dEventNamesShow"] = "顯示視窗%d事件名稱",
    ["Toggles showing event names on and off for chat window %d."] = "切換是否在聊天視窗%d顯示事件名稱。"
} end)

Prat_EventNames = Prat:NewModule("eventnames")

function Prat_EventNames:OnInitialize()
    self.db = Prat:AcquireDBNamespace("EventNames")
    Prat:RegisterDefaults("EventNames", "profile", {
        on = true,
        show = {false, false, false, false, false, false, false},
    })
    Prat.Options.args.eventnames = {
        name = L["EventNames"],
        desc = L["EventNames options."],
        type = "group",
        args = {
            show = {
                name = L["Show"],
                desc = L["Toggle showing event names for each window."],
                type = "group",
                order = 110,
                args = {
                    chat1 = {
                        name = string.format(L["Chat%dEventNamesShow"], 1),
                        desc = string.format(L["Toggles showing event names on and off for chat window %d."], 1),
                        type = "toggle",
                        get = function() return self.db.profile.show[1] end,
                        set = function(v) self.db.profile.show[1] = v; self:SetFrameStatus(1) end
                    },
                    chat2 = {
                        name = string.format(L["Chat%dEventNamesShow"], 2),
                        desc = string.format(L["Toggles showing event names on and off for chat window %d."], 2),
                        type = "toggle",
                        get = function() return self.db.profile.show[2] end,
                        set = function(v) self.db.profile.show[2] = v; self:SetFrameStatus(2) end
                    },
                    chat3 = {
                        name = string.format(L["Chat%dEventNamesShow"], 3),
                        desc = string.format(L["Toggles showing event names on and off for chat window %d."], 3),
                        type = "toggle",
                        get = function() return self.db.profile.show[3] end,
                        set = function(v) self.db.profile.show[3] = v; self:SetFrameStatus(3) end
                    },
                    chat4 = {
                        name = string.format(L["Chat%dEventNamesShow"], 4),
                        desc = string.format(L["Toggles showing event names on and off for chat window %d."], 4),
                        type = "toggle",
                        get = function() return self.db.profile.show[4] end,
                        set = function(v) self.db.profile.show[4] = v; self:SetFrameStatus(4) end
                    },
                    chat5 = {
                        name = string.format(L["Chat%dEventNamesShow"], 5),
                        desc = string.format(L["Toggles showing event names on and off for chat window %d."], 5),
                        type = "toggle",
                        get = function() return self.db.profile.show[5] end,
                        set = function(v) self.db.profile.show[5] = v; self:SetFrameStatus(5) end
                    },
                    chat6 = {
                        name = string.format(L["Chat%dEventNamesShow"], 6),
                        desc = string.format(L["Toggles showing event names on and off for chat window %d."], 6),
                        type = "toggle",
                        get = function() return self.db.profile.show[6] end,
                        set = function(v) self.db.profile.show[6] = v; self:SetFrameStatus(6) end
                    },
                    chat7 = {
                        name = string.format(L["Chat%dEventNamesShow"], 7),
                        desc = string.format(L["Toggles showing event names on and off for chat window %d."], 7),
                        type = "toggle",
                        get = function() return self.db.profile.show[7] end,
                        set = function(v) self.db.profile.show[7] = v; self:SetFrameStatus(7) end
                    },
                },
            },
            toggle = {
                name = L["Toggle"],
                desc = L["Toggle the module on and off."],
                type = "toggle",
                order = 200,
                get = function() return self.db.profile.on end,
                set = function() self.db.profile.on = Prat:ToggleModuleActive("eventnames") end
            }
        }
    }
end

function Prat_EventNames:OnEnable()
    for i=1,NUM_CHAT_WINDOWS do
		Prat_EventNames:SetFrameStatus(i)
    end
end

function Prat_EventNames:SetFrameStatus(id)
    if self.db.profile.show[id] then
    	if not self:IsHooked(getglobal("ChatFrame"..id), "AddMessage") then self:Hook(getglobal("ChatFrame"..id), "AddMessage", "AddMessage"..id) end
    else
        if self:IsHooked(getglobal("ChatFrame"..id), "AddMessage") then self:Unhook(getglobal("ChatFrame"..id), "AddMessage") end
    end
end

function Prat_EventNames:AddMessage1(...)
	Prat_EventNames:AddMessage(1, ...)
end
function Prat_EventNames:AddMessage2(...)
	Prat_EventNames:AddMessage(2, ...)
end
function Prat_EventNames:AddMessage3(...)
	Prat_EventNames:AddMessage(3, ...)
end
function Prat_EventNames:AddMessage4(...)
	Prat_EventNames:AddMessage(4, ...)
end
function Prat_EventNames:AddMessage5(...)
	Prat_EventNames:AddMessage(5, ...)
end
function Prat_EventNames:AddMessage6(...)
	Prat_EventNames:AddMessage(6, ...)
end
function Prat_EventNames:AddMessage7(...)
	Prat_EventNames:AddMessage(7, ...)
end

function Prat_EventNames:AddMessage(id, frame, text, ...)
    if text then
		text = text.." ("..tostring(event)..")"
    end
    self.hooks[frame].AddMessage(frame, text, ...)
end

