---------------------------------------------------------------------------------
--
-- 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: PratSubstitutions
Revision: $Revision: 29578 $
Author(s): Sylvanaar
Website: http://www.wowace.com/files/index.php?path=Prat/
Documentation: http://www.wowace.com/wiki/Prat/Integrated_Modules#Substitutions
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: A module to provide basic chat substitutions. (default=off).
Dependencies: Prat
]]

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

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

L:RegisterTranslations("enUS", function() return {
    ["Substitutions"] = true,
    ["A module to provide basic chat substitutions."] = true,
} end)

L:RegisterTranslations("deDE", function() return {
    ["Substitutions"] = "Ersetzungen",
    ["A module to provide basic chat substitutions."] = "Ein Modul, um grundlegende Chat-Ersetzungen zur Verf\195\188gung zu stellen.",
} end)

L:RegisterTranslations("koKR", function() return {
    ["Substitutions"] = "대체",
    ["A module to provide basic chat substitutions."] = "기본 대화 대체를 제공하는 모듈입니다.",
} end)

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

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

-- define key module values
Prat_Substitutions.moduleName = L["Substitutions"]
Prat_Substitutions.moduleDesc = L["A module to provide basic chat substitutions."]

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

Prat_Substitutions.Categories	= { cat.FILTER, cat.INFO, cat.TEXT }
-- list here: http://groups-beta.google.com/group/wow-prat/web/prat-categories

-- init moduleOoptions (which gets populated by the other
-- various options tables below
Prat_Substitutions.moduleOptions = {}

-- default values for any settings that need them
Prat_Substitutions.defaultDB = {
    on	= false,
}

-- any boolean options
Prat_Substitutions.toggleOptions = {
}


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

-- things to do when the module is enabled
function Prat_Substitutions:OnModuleEnable()
    self:BuildModuleOptions()
end


-- add module options not covered by templates
function Prat_Substitutions:GetModuleOptions()
    self.moduleOptions = {
        name = self.moduleName,
        desc = self.moduleDesc,
        type = "group",
        args = {},
        hidden = function() self:BuildModuleOptions() end,
    }

    self:BuildModuleOptions()

    return self.moduleOptions
end


-- things to do when the module is disabled
function Prat_Substitutions:BuildModuleOptions()
    local args = self.moduleOptions.args

    self.buildingMenu = true
    for k,v in pairs(self.modulePatterns) do
        local name = v.optname    
        local pat = string.gsub(v.pattern, "%%%%", "%%")

        args[name] = args[name] or {}
        local d = args[name]        

        d.name = name.." "..pat
        d.desc = "Current value: '"..v.matchfunc().."'\nClick to paste into the chat."
        d.type = "execute"
        d.func = function() self:DoPat(pat) end
    end    
    self.buildingMenu = false
end

--[[------------------------------------------------
Core Functions
------------------------------------------------]]--
function  Prat_Substitutions:DoPat(pat)
    local e = ChatFrameEditBox
    if not e:IsVisible() then
        return
    end

    e:SetText((e:GetText() or "")..pat:gsub("[%(%)]", ""))
end

local function prat_match(text)
    if Prat_Substitutions.buildingMenu then 
        return text
    end
    
    return Prat:RegisterMatch(text, "OUTBOUND")
end

local function Zone(...)
    return prat_match(GetZoneText())
end
local function Loc(...)
    return prat_match(GetMinimapZoneText())
end
local function Pos()
    local x,y = GetPlayerMapPosition("player")
    local str = "("..math.floor((x * 100) + 0.5)..","..math.floor((y * 100) + 0.5)..")"
    return prat_match(str)
end


local function PlayerHP(...)
    return prat_match(UnitHealth("player"))
end
local function PlayerMaxHP(...)
    return prat_match(UnitHealthMax("player"))
end
local function PlayerPercentHP()
    return prat_match(floor(100 * (UnitHealth("player") / UnitHealthMax("player"))))
end

local function PlayerCurrentMana()
    return prat_match(UnitMana("player"))
end
local function PlayerMaxMana()
    return prat_match(UnitManaMax("player"))
end
local function PlayerPercentMana()
    return prat_match(string.format("%.0f%%%%",
    floor(100 * (UnitMana("player")/UnitManaMax("player")))))
end



local function TargetPercentHP()
    local str = "<no target>"
    if UnitExists("target") then
        str = string.format("%.0f%%%%", floor(100 * (UnitHealth("target") / UnitHealthMax("target"))))
    end

    return prat_match(str)
end

local function TargetClass()
    local class = "<no target>"
    if UnitExists("target") then
        class = UnitClass("target")
    end

    return prat_match(class)
end
local function TargetRace()
    local race = "<no target>"
    if UnitExists("target") then
        if UnitIsPlayer("target") then
            race = UnitRace("target")
        else
            race = UnitCreatureFamily("target")
            if not race then
                race = UnitCreatureType("target")
            end
        end
    end
    return prat_match(race)
end
local function TargetGender()
    local sex = "<no target>"
    if UnitExists("target") then
        s = UnitSex("target")
        if(s == 2) then
            sex = "male"
        elseif (s == 3) then
            sex = "female"
        else
            sex = "unknown sex"
        end
    end

    return prat_match(sex)
end

local function TargetLevel()
    local level = "<no target>"
    if UnitExists("target") then
        level = UnitLevel("target")
    end
    return prat_match(level)
end

-- %tps (possesive)
local function TargetPossesive()
    local p = "<no target>"
    if UnitExists("target") then
        local s = UnitSex("target")
        if(s == 2) then
            p = "his"
        elseif (s == 3) then
            p = "hers"
        else
            p = "its"
        end
    end

    return prat_match(p)
end

-- %tpn (pronoun)
local function TargetPronoun()
    local p = "<no target>"
    if UnitExists("target") then
        local s = UnitSex("target")
        if(s == 2) then
            p = "him"
        elseif (s == 3) then
            p = "her"
        else
            p = "it"
        end
    end
    return prat_match(p)
end


--[[
* %hc = your current health
* %hm = your max health
* %hp = your percentage health
* %mc = your current mana
* %mm = your max mana
* %mp = your percentage mana
* %thp = target's percentage health
* %tc = class of target
* %tr = race of target
* %ts = sex of target
* %tl = level of target
* %tps = possesive for target (his/hers/its)
* %tpn = pronoun for target (him/her/it)
* %fhp = focus's percentage health
* %fc = class of focus
* %fr = race of focus
* %fs = sex of focus
* %fl = level of focus
* %fps = possesive for focus (his/hers/its)
* %fpn = pronoun for focus (him/her/it)
* %tt = target of target
* %zon = your current zone (Dun Morogh, Hellfire Peninsula, etc.)
* %loc = your current subzone (as shown on the minimap)
* %pos = your current coordinates (x,y)
--]]


Prat_Substitutions.modulePatterns = {
    { pattern = "(%%hc)", matchfunc=PlayerHP, optname="PlayerHP",  type = "OUTBOUND"},
    { pattern = "(%%hm)", matchfunc=PlayerMaxHP, optname="PlayerMaxHP",  type = "OUTBOUND"},
    { pattern = "(%%hp)", matchfunc=PlayerPercentHP, optname="PlayerPercentHP",  type = "OUTBOUND"},
    { pattern = "(%%mc)", matchfunc=PlayerCurrentMana, optname="PlayerCurrentMana",  type = "OUTBOUND"},
    { pattern = "(%%mm)", matchfunc=PlayerMaxMana, optname="PlayerMaxMana",  type = "OUTBOUND"},
    { pattern = "(%%mp)", matchfunc=PlayerPercentMana, optname="PlayerPercentMana",  type = "OUTBOUND"},

    { pattern = "(%%thp)", matchfunc=TargetPercentHP, optname="TargetPercentHP",  type = "OUTBOUND"},
    { pattern = "(%%tr)", matchfunc=TargetRace, optname="TargetRace",  type = "OUTBOUND"},
    { pattern = "(%%ts)", matchfunc=TargetGender, optname="TargetGender",  type = "OUTBOUND"},
    { pattern = "(%%tl)", matchfunc=TargetLevel, optname="TargetLevel",  type = "OUTBOUND"},
    { pattern = "(%%tps)", matchfunc=TargetPossesive, optname="TargetPossesive",  type = "OUTBOUND"},
    { pattern = "(%%tpn)", matchfunc=TargetPronoun, optname="TargetPronoun",  type = "OUTBOUND"},


    { pattern = "(%%zon)", matchfunc=Zone, optname="MapZone",  type = "OUTBOUND"},
    { pattern = "(%%loc)", matchfunc=Loc, optname="MapLoc",  type = "OUTBOUND"},
    { pattern = "(%%pos)", matchfunc=Pos, optname="MapPos",  type = "OUTBOUND"},
}



--[[

    { pattern = "(%%fhp)", matchfunc=xxxx, type = "OUTBOUND"},
    { pattern = "(%%fr)", matchfunc=xxxx, type = "OUTBOUND"},
    { pattern = "(%%fc)", matchfunc=xxxx, type = "OUTBOUND"},
    { pattern = "(%%fs)", matchfunc=xxxx, type = "OUTBOUND"},
    { pattern = "(%%fl)", matchfunc=xxxx, type = "OUTBOUND"},
    { pattern = "(%%fvr)", matchfunc=xxxx, type = "OUTBOUND"},
    { pattern = "(%%fvn)", matchfunc=xxxx, type = "OUTBOUND"},

    { pattern = "(%%tt)", matchfunc=xxxx, type = "OUTBOUND"},

    -- Focus substitutions
    { pattern = "(%%f)", matchfunc=xxxx, type = "outbound"},
    -- %fhp (focus' percentage health)
    { pattern = "(%%fhp)", matchfunc=xxxx, type = "outbound"},
    local str = "<no focus>"
    if UnitExists("focus") then
        str = string.format("%.0f%%%%", floor(100 * (UnitHealth("focus") / UnitHealthMax("focus"))))
    end
    local function xxxx()
        return prat_match(str, "outbound")
    end
end
-- %fr (race of focus)
{ pattern = "(%%fr)", matchfunc=xxxx, type = "outbound"},
local race = "<no focus>"
if UnitExists("focus") then
    if UnitIsPlayer("focus") then
        race = UnitRace("focus")
    else
        race = UnitCreatureFamily("focus")
        if(race == nil) then
            race = UnitCreatureType("focus")
        end
    end
end
text = string.gsub(text,"(%%fr)", race)
end
-- %fc (class of focus)
{ pattern = "(%%fc)", matchfunc=xxxx, type = "outbound"},
local class = "<no focus>"
if UnitExists("focus") then
    class = UnitClass("focus")
end
text = string.gsub(text,"(%%fc)", class)
end
-- %fs (sex of focus)
{ pattern = "(%%fs)", matchfunc=xxxx, type = "outbound"},
local sex = "<no focus>"
if UnitExists("focus") then
    s = UnitSex("focus")
    if(s == 2) then
        sex = "male"
        elseif (s == 3) then
            sex = "female"
        else
            sex = "unknown sex"
        end
    end
    text = string.gsub(text,"(%%fs)", sex)
end
-- %fl (level of focus)
{ pattern = "(%%fl)", matchfunc=xxxx, type = "outbound"},
local level = "<no focus>"
if UnitExists("focus") then
    level = UnitLevel("focus")
end
text = string.gsub(text,"(%%fl)", level)
end
-- %fvr (PVP rank)
{ pattern = "(%%fvr)", matchfunc=xxxx, type = "outbound"},
local rank, name
if not UnitExists("focus") then
    rank = "<no focus>"
else
    if UnitIsPlayer("focus") then
        _,_,rank,name = string.find(UnitPVPName("focus"), "(.+) (.+)")
    end
    if not rank then
        rank = "<no rank>"
    end
end
text = string.gsub(text,"(%%fvr)", rank)
end
-- %fvn (PVP name of focus)
{ pattern = "(%%fvn)", matchfunc=xxxx, type = "outbound"},
local name = "<no focus>"
if UnitExists("focus") then
    name = UnitPVPName("focus")
end
text = string.gsub(text,"(%%fvn)", name)
end
-- %fps (possesive of focus)
if string.find(text,"(%%fps)") then
    local p = "<no focus>"
    if UnitExists("focus") then
        local s = UnitSex("focus")
        if(s == 2) then
            p = "his"
            elseif (s == 3) then
                p = "hers"
            else
                p = "its"
            end
        end
        text = string.gsub(text,"(%%fps)", p)
    end
    -- %fpn (pronoun of focus)
    if string.find(text,"(%%fpn)") then
        local p = "<no focus>"
        if UnitExists("focus") then
            local s = UnitSex("focus")
            if(s == 2) then
                p = "him"
                elseif (s == 3) then
                    p = "her"
                else
                    p = "it"
                end
            end
            text = string.gsub(text,"(%%fpn)", p)
        end
        -- %f (name of focus)
        if string.find(text,"(%%f)") then
            local name = "<no focus>"
            if UnitExists("focus") then
                name = UnitName("focus")
            end
            text = string.gsub(text,"(%%f)", name)
        end
    end

    -- Other substitutions
    -- %tt (target of target)
    { pattern = "(%%tt)", matchfunc=xxxx, type = "outbound"},
    local targetoftarget = UnitName("targettarget")
    if(targetoftarget == nil) then
        targetoftarget="<no target>"
    end
    text = string.gsub(text,"(%%tt)", targetoftarget)
end

-- %zon (zone)
{ pattern = "(%%zon)", matchfunc=xxxx, type = "outbound"},
text = string.gsub(text,"(%%zon)", GetZoneText())
end

-- %loc (minimap location)
{ pattern = "(%%loc)", matchfunc=xxxx, type = "outbound"},
text = string.gsub(text,"(%%loc)", GetMinimapZoneText())
end

-- %pos
{ pattern = "(%%pos)", matchfunc=xxxx, type = "outbound"},
local function Pos()
    local x,y = GetPlayerMapPosition("player")
    local str = "("..math.floor((x * 100) + 0.5)..","..math.floor((y * 100) + 0.5)..")"
    return prat_match(str)
end

--]]
