---------------------------------------------------------------------------------
--
-- 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: PratServerNames
Revision: $Revision: 30586 $
Author(s): Curney (asml8ed@gmail.com)
           Krtek (krtek4@gmail.com)
           Sylvanaar (sylvanaar@mindspring.com)

Website: http://www.wowace.com/files/index.php?path=Prat/
Documentation: http://www.wowace.com/wiki/Prat/Integrated_Modules#ServerNames
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 options for replacing server names with abbreviations.
Dependencies: Prat
]]

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

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

L:RegisterTranslations("enUS", function() return {
    ["ServerNames"] = true,
    ["Server name abbreviation options."] = true,
    ["Replace"] = true,
    ["Toggle replacing this server."] = true,
    ["Blank"] = true,
    ["Don't display the server name"] = true,
    ["Set"] = true,
    ["Server %d"] = true,
    ["'%s - %s' display settings."] = true,
    ["Use a custom replacement for the server %s text."] = true,
    ["randomclr_name"] = "Random Colors",
    ["randomclr_desc"] = "Use a random color for each server.",
    ["Set color"] = true,
    ["Change the color for this server name"] = true,
    ["Use custom color"] = true,
    ["Toggle useing custom color this server."] = true,
    ["colon_name"] = "Show Colon",
    ["colon_desc"] = "Toggle adding colon after server replacement.",
} end)

L:RegisterTranslations("deDE", function() return {
    ["ServerNames"] = "Server Name",
    ["Server name abbreviation options."] = "Server Namen Abk\195\188rzungsoptionen.",
    ["Replace"] = "Ersetze",
    ["Toggle replacing this server."] = "Schaltet das ersetzen dieses Servers ein.",
    ["Blank"] = "Aus",
    ["Don't display the server name"] = "Schaltet diesen Server aus.",
    ["Set"] = "Einstellen",
    ["Server %d"] = "server %d",
    ["'%s - %s' display settings."] = "'%s - %s' Einstellungen.",
    ["Use a custom replacement for the server %s text."] = "Benutze eigenen Namen f\195\188r den Server %s Text.",
    ["randomclr_name"] = "Zeige Leerstelle",
    ["randomclr_desc"] = "Aktiviere eine zus\195\164tzliche Leerstelle nach dem eigenen Servernamen.",
    ["colon_name"] = "Zeige Doppelpunkt",
    ["colon_desc"] = "Aktiviere das hinzuf\195\188gen eines Doppelpunkts nach dem eigenen Servernamen.",
} end)

L:RegisterTranslations("esES", function() return {
    ["ServerNames"] = "Nombres de Servidor",
    ["Server name abbreviation options."] = "Opciones de abreviaci\195\179n de los nombres de servidor",
    ["Replace"] = "Reemplazar",
    ["Toggle replacing this server."] = "Determina si se reemplaza este servidor",
    ["Blank"] = "Desactivar",
    ["Don't display the server name"] = "Desactivar este servidor",
    ["Set"] = "Establecer",
    ["Server %d"] = "Servidor %d",
    ["'%s - %s' display settings."] = "Ajustes de '%s - %s'",
    ["Use a custom replacement for the server %s text."] = "Usa un recambio personalizado para el texto del servidor %s",
    ["randomclr_name"] = "Colores Aleatorios",
    ["randomclr_desc"] = "Usar un color aleatorio para cada servidor.",
    ["colon_name"] = "Mostrar los Dos Puntos",
    ["colon_desc"] = "Determina si e aaden los dos puntos despu\195\169s del recambio de servidor.",
} end)

L:RegisterTranslations("koKR", function() return {
    ["ServerNames"] = "서버명",
    ["Server name abbreviation options."] = "서버명 단축 설정입니다.",
    ["Replace"] = "대체",
    ["Toggle replacing this server."] = "해당 서버명을 대체합니다.",
    ["Blank"] = "공백",
    ["Don't display the server name"] = "서버명을 표시하지 않습니다.",
    ["Set"] = "켬",
    ["Server %d"] = "%d 서버",
    ["'%s - %s' display settings."] = "'%s - %s' 설정입니다.",
    ["Use a custom replacement for the server %s text."] = "서버 %s 글자에 대해 사용자 정의 대체를 사용합니다.",
    ["randomclr_name"] = "무작위 색상",
    ["randomclr_desc"] = "각 서버명에 무작위 색상을 사용합니다.",
    ["Set color"] = "색상 설정",
    ["Change the color for this server name"] = "해당 서버명의 색상을 변경합니다.",
    ["Use custom color"] = "사용자 정의 색상 사용",
    ["Toggle useing custom color this server."] = "해당 서버에 사용자 정의 색상을 사용합니다.",
    ["colon_name"] = "콜론 표시",
    ["colon_desc"] = "대체한 서버명 뒤에 콜론을 추가합니다.",
} end)

L:RegisterTranslations("zhCN", function() return {
    ["ServerNames"] = "频道名称",
    ["Server name abbreviation options."] = "频道名称缩写选项。",
    ["Replace"] = "替换",
    ["Toggle replacing this server."] = "替换频道。",
    ["Blank"] = "关闭",
    ["Don't display the server name"] = "关闭此频道。",
    ["Set"] = "设置",
    ["'%s - %s' display settings."] = "'%s - %s' 设置。",
    ["Use a custom replacement for the server %s text."] = "自定义替换此聊天 %s 文本。",
    ["randomclr_name"] = "显示间隔",
    ["randomclr_desc"] = "切换频道替换后增加间隔空间。",
    ["colon_name"] = "显示冒号",
    ["colon_desc"] = "切换频道替换后增加冒号。",
} end)

L:RegisterTranslations("zhTW", function() return {
    ["ServerNames"] = "頻道名稱",
    ["Server name abbreviation options."] = "頻道名稱縮寫選項。",
    ["Replace"] = "替換",
    ["Toggle replacing this server."] = "替換頻道。",
    ["Blank"] = "關閉",
    ["Don't display the server name"] = "關閉此頻道。",
    ["Set"] = "設置",
    ["Server %d"] = "頻道 %d",
    ["'%s - %s' display settings."] = "'%s - %s' 設定。",
    ["Use a custom replacement for the chat %s text."] = "自定義替換此聊天 %s 文本。",
    ["randomclr_name"] = "顯示空白間隔",
    ["randomclr_desc"] = "切換頻道替換後增加空白間隔空間。",
    ["colon_name"] = "顯示冒號",
    ["colon_desc"] = "切換頻道替換後增加冒號。",
} end)

L:RegisterTranslations("frFR", function() return {
    ["ServerNames"] = "Noms du serveur",
    ["Server name abbreviation options."] = "Options pour abbr\195\169ger les noms des serveurs.",
    ["Replace"] = "Remplacer",
    ["Toggle replacing this server."] = "Active/d\195\169sactive le remplacement pour ce serveur.",
    ["Blank"] = "D\195\169sactiv\195\169",
    ["Don't display the server name"] = "D\195\169sactiver ce serveur.",
    ["Set"] = "Set",
    ["Server %d"] = "Serveur %d",
    ["'%s - %s' display settings."] = "R\195\169glages '%s - %s'",
    ["Use a custom replacement for the server %s text."] = "Utiliser un remplacement personnalis\195\169 pour le texte du chat %s.",
    ["randomclr_name"] = "Couleurs aléatoires",
    ["randomclr_desc"] = "Utiliser une couleur aléatoire pour chaque serveur.",
    ["Set color"] = "Choisir la couleur",
    ["Change the color for this server name"] = "Choisit la couleur pour le nom de ce serveur",
    ["Use custom color"] = "Couleur personnalisée",
    ["Toggle useing custom color this server."] = "Active/désactive l'utilisation d'une couleur personnalisée pour ce serveur.",
    ["colon_name"] = "Afficher ':'",
    ["colon_desc"] = "Ajoute ou non un ':' après le nom du serveur.",
} end)

-- order to show servers
local orderMap = {
        "say",
        "whisper",
        "whisperincome",
        "yell",
        "party",
        "guild",
        "officer",
        "raid",
        "raidleader",
        "raidwarning",
        "battleground",
        "battlegroundleader",
}



-- To do the config we need the data that's in GloryLib
-- We'll set the menus up so that they disable and offer
-- a button to load Glory, or Another way




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

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

-- define key module values
Prat_ServerNames.moduleName = L["ServerNames"]
Prat_ServerNames.moduleDesc = L["Server name abbreviation options."]
Prat_ServerNames.consoleName = "servernames"
Prat_ServerNames.guiName = L["ServerNames"]
Prat_ServerNames.Categories = { cat.NAME, cat.FORMATTING, cat.TEXT, cat.INFO }

-- Set this to true to use Prat Event Support
Prat_ServerNames.supportsPratEvents = true

-- define the default db values
Prat_ServerNames.defaultDB = {
    on = true,
    space = true,
    colon = true,

    chanSave = {},
   
    serveropts = { 
        ["*"] = {
            replace = false,
            customcolor = false,
            shortname = "",
            color = {
                r = 0.65,
                g = 0.65,
                b = 0.65,
            },   
        },
    },

    randomclr = false,
}

-- build the options menu using prat templates
Prat_ServerNames.toggleOptions = { optsep_sep = 240, randomclr = 250}
--Prat_ServerNames.toggleOptions = { optsep_sep = 229, space = 230, colon = 240 }
-- create a moduleOptions stub (for setting self.moduleOptions)
Prat_ServerNames.moduleOptions = {}


-- add module options not covered by templates
function Prat_ServerNames:GetModuleOptions()
    self.moduleOptions = {
        name = L["ServerNames"],
        desc = L["Server name abbreviation options."],
        type = "group",
        hidden = function() self:UpdateServerMenu() return false end,
        args = {
        }
    }
    self:BuildServerOptions()
    return self.moduleOptions
end


local server_tags = {
    ["Normal"] = "(E)",
    ["PvP"] = "(P)",
    ["RP"] = "(R)",
    ["RP-PvP"] = "(PR)",
}

local server_desctags = {
    ["Normal"] = "PvE",
    ["PvP"] = "PvP",
    ["RP"] = "RP",
    ["RP-PvP"] = "RPPvP",
}

local Glory = AceLibrary:HasInstance("Glory-2.0") and AceLibrary("Glory-2.0")

function CLR:Server(server, text) return self:Colorize(Prat_ServerNames:GetServerCLR(server), text or server) end

-- Get the key for the server specified, safe to pass this nil and "", if no key then it returns nil
local function GetServerKey(server) if server and strlen(server)>0 then return string.lower(string.gsub(server, " ", "")) end end

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

function Prat_ServerNames:OnModuleEnable()
    self:BuildServerOptions()

    if not Glory then 
        self:RegisterEvent("ADDON_LOADED")
    end

    self:RegisterEvent("Prat_PreAddMessage")
end

-- things to do when the module is disabled
function Prat_ServerNames:OnModuleDisable()
    -- unregister events
    self:UnregisterAllEvents()
end

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

-- replace text using prat event implementation
function Prat_ServerNames:Prat_PreAddMessage(m, frame)
    local opts
    local serverKey = GetServerKey(m.SERVER) 

    if serverKey then
        opts = self.db.profile.serveropts[serverKey]
    end

    if opts and opts.replace then
        m.SERVER = opts.shortname
    end
    

    if m.SERVER and strlen(m.SERVER)>0 then
        m.SERVER = CLR:Server(serverKey, m.SERVER)
    end

    if not (m.SERVER and strlen(m.SERVER)>0) then
        Prat:ClearSplitItem(m, "SERVER")
        self:Debug(m.sS, m.SERVER, m.Ss)
    end
end

function Prat_ServerNames:GetServerCLR(server)
    local serverKey = GetServerKey(server) 
    
    
    if serverKey then
        local opts = self.db.profile.serveropts[serverKey]
    
        if opts and opts.customcolor then
            return CLR:GetHexColor(opts.color)        
        elseif self.db.profile.randomclr then 
            return Prat_PlayerNames:GetRandomCLR(serverKey)
        end
    end

    return CLR.COLOR_NONE
end

function Prat_ServerNames:ADDON_LOADED()
    Glory = AceLibrary:HasInstance("Glory-2.0") and AceLibrary("Glory-2.0")

    if Glory then
        self:UnregisterEvent("ADDON_LOADED")
        self:BuildServerOptions()
    end
end
--[[------------------------------------------------
    Menu Builder Functions
------------------------------------------------]]--

function Prat_ServerNames:BuildServerOptions()
    if Glory then 
        local serverList = {}
        local homeservName = GetRealmName()
        Glory:GetBattlegroupServers(nil, serverList)

        local serverKey, serverType
        for _,v in pairs(serverList) do
        -- Since we dont detect the home server, dont offer an option
            if v ~= homeservName then
                serverKey = GetServerKey(v)   
                serverType = Glory:GetServerType(v)
                self:CreateServerOption(self.moduleOptions.args, v, serverKey, serverType)
            end
        end
    end
end



--
--
-- "-Name(type)" is how we have it
--
--  so provide
--
--   %S = Full Server Name
--   %s = Abbreviated Server Name
--   %T = Full Realm Type eg PvP
--   %t = Abbreviated Realm Type e.g P
--
--  So the default format is:
--
--      -%S(%t)
--
--   We can support a coloring syntax
--   which can say use the color of
--   (some other field) Here, we can 
--   Set the color of the server to use
--   the color value of the realm type
--   
--
local t_sort = {}
function Prat_ServerNames:UpdateServerMenu()
    local args = self.moduleOptions.args
    if not args then return end

    for k,v in pairs(args) do
        if v.name_org then
            local opts = self.db.profile.serveropts[k]

            v.name = CLR:Server(v.name_org)
            if opts and opts.replace and opts.shortname and strlen(opts.shortname) > 0 then
                v.name = v.name .. " - (" .. CLR:Server(v.name_org, opts.shortname) .. ")"
            end

            v.args.set.name = CLR:Server(v.name_org)
            t_sort[#t_sort+1] = k
        end
    end

    -- Now it must be sorted   
    table.sort(t_sort)

    -- Now apply ordering
    local o = 10
    for i,k in ipairs(t_sort) do
        t_sort[i] = nil
        args[k].order = o
        o = o + 1
    end
end


function Prat_ServerNames:CreateServerOption(args, servername, serverkey, servertype)
    local name = serverkey
    local text = servername
    local type = text



    args[name] = {
        name = CLR:Server(text),
        name_org = text,
        type_org = servertype,
        desc = string.format(L["'%s - %s' display settings."], text, server_desctags[servertype]),
        type = "group",
        args = {
            set = {
                name = CLR:Server(text),
                desc = string.format(L["Use a custom replacement for the server %s text."], text),
                order = 10,
                type = "text",
                usage = "<string>",
                get = function() return self.db.profile.serveropts[name].shortname end,
                set = function(v) self.db.profile.serveropts[name].shortname = v end
            },
            optsep20 = {
                order = 20,
                type = 'header',
            },
            usecustomcolor = {
                name = L["Use custom color"],
                desc = L["Toggle useing custom color this server."],
                type = "toggle",
                order = 24,
                get = function() return self.db.profile.serveropts[name].customcolor end,
                set = function(v) self.db.profile.serveropts[name].customcolor = v end
            },
            customcolor = {
                name = L["Set color"],
                desc = L["Change the color for this server name"],
                type = "color",
                order = 25,
                get = function() local c = self.db.profile.serveropts[name].color
                         return c.r, c.g, c.b end,
                set = function(r, g, b, a) local c = self.db.profile.serveropts[name].color
                        c.r, c.g, c.b = r, g, b end,
                disabled = function() if not self.db.profile.serveropts[name].customcolor then return true else return false end end,
            },
            optsep27 = {
                order = 27,
                type = 'header',
            },
            replace = {
                name = L["Replace"],
                desc = L["Toggle replacing this server."],
                type = "toggle",
                order = 30,
                get = function() return self.db.profile.serveropts[name].replace  end,
                set = function(v) self.db.profile.serveropts[name].replace  = v end,
            },
            off = {
                name = L["Blank"],
                desc = L["Don't display the server name"],
                type = "execute",
                order = 40,
                func = function() self.db.profile.serveropts[name].shortname  = ""  end
            }
        }
    }
end
