---------------------------------------------------------------------------------
--
-- 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: PratJustify
Revision: $Revision: 29189 $
Author(s): Curney (asml8ed@gmail.com)
Thanks to: Wubin for helping me understand variables better
Website: http://www.wowace.com/files/index.php?path=Prat/
Documentation: http://www.wowace.com/wiki/Prat/Integrated_Modules#Justify
SVN: http://svn.wowace.com/wowace/trunk/Prat/
Description: Module for Prat that sets the justification for chat windows (default=left).
Dependencies: Prat
]]

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

L:RegisterTranslations("enUS", function() return {
    ["Justify"] = true,
    ["Chat window justification options."] = true,
    ["Set Justification"] = true,
    ["Set justification for each chat window."] = true,
    ["Chat%dJustify"] = true,
    ["Set justification for chat window %d."] = true,
    ["adjustlinks_name"] = "Fix placement of player/item links",
    ["adjustlinks_desc"] = "Adjust links to restore their clickability when justification is right or center.",    
    ["justification_name"] = "Set Justification",
    ["justification_desc"] = "Set justification for each chat window.",
    ["justification_perframename"] = "Chat%dJustify",
    ["justification_perframedesc"] = "Set Justification",
	["Center"] = true,
	["Right"] = true,
	["Left"] = true,
} end)

L:RegisterTranslations("frFR", function() return {
    ["Justify"] = "Justification",
    ["Chat window justification options."] = "Options de justification",
    ["Set Justification"] = "Justification",
    ["Set justification for each chat window."] = "R\195\168gle la justification pour toutes les fen\195\170tres",
    ["Chat%dJustify"] = "Fen\195\170tre %d",
    ["Set justification for chat window %d."] = "R\195\168gle la justification pour la fen\195\170tre %d.",
    ["adjustlinks_name"] = "Corrige le placement des liens",
    ["adjustlinks_desc"] = "Corrige le placement des liens lorsque la justification est sur Centr\195\169e ou Droite.",    
    ["justification_name"] = " Justification",
    ["justification_desc"] = "R\195\168gle la justification pour toutes les fen\195\170tres.",
    ["justification_perframename"] = "Fen\195\170tre %d",
    ["justification_perframedesc"] = "R\195\168gle la justification",
	["Center"] = "Centr\195\169",
	["Right"] = "Align\195\169 à droite",
	["Left"] = "Align\195\169 à gauche",
} end)

-- L:RegisterTranslations("zhCN", function() return {
-- 	["Justify"] = "对齐",
-- 	["Chat window justification options."] = "聊天窗口对齐模式.",
-- 	["Set"] = "设置",
-- 	["Set justify for each window."] = "为每一个聊天窗口设置对齐模式.",
-- 	["Chat%dJustify"] = "窗口%d对齐",
-- 	["Sets justification for chat window %d."] = "设置聊天窗口%d对齐模式.",
-- } end)

L:RegisterTranslations("koKR", function() return {
    ["Justify"] = "정렬",
    ["Chat window justification options."] = "대화창 정렬 설정입니다.",
    ["Set Justification"] = "정렬 설정",
    ["Set justification for each chat window."] = "각 대화창의 정렬을 설정합니다.",
    ["Chat%dJustify"] = "대화창%d정렬",
    ["Set justification for chat window %d."] = "대화창 %d의 정렬을 설정합니다.",
    ["adjustlinks_name"] = "플레이어/아이템 링크 위치 수정",
    ["adjustlinks_desc"] = "우측 혹은 가운데 정렬일 때 클릭가능한 것들을 복원하기 위내 링크를 조절합니다.",
    ["justification_name"] = "정렬 설정",
    ["justification_desc"] = "각 대화창의 정렬을 설정합니다.",
    ["justification_perframename"] = "대화창%d정렬",
    ["justification_perframedesc"] = "대화창 %d의 정렬을 설정합니다.",
	["Center"] = "가운데",
	["Right"] = "우측",
	["Left"] = "좌측",
} end)

-- L:RegisterTranslations("zhTW", function() return {
--     ["Justify"] = "對齊",
--     ["Chat window justification options."] = "聊天視窗對齊格式。",
--     ["Set"] = "設定",
--     ["Set justify for each window."] = "為每一個聊天視窗設定對齊格式。",
--     ["Chat%dJustify"] = "視窗%d對齊",
--     ["Sets justification for chat window %d."] = "設定聊天視窗 %d 對齊格式。",
-- } end)

L:RegisterTranslations("esES", function() return {
    ["Justify"] = "Justificar",
    ["Chat window justification options."] = "Opciones de justificaci\195\179n para la ventana de chat",
    ["Set Justification"] = "Establecer Justificaci\195\179n",
    ["Set justification for each chat window."] = "Establece la justificaci\195\179n para cada ventana",
    ["Chat%dJustify"] = "Justificaci\195\179n del Chat %d",
    ["Set justification for chat window %d."] = "Establece la justificaci\195\179n para la ventana de chat %d",
    ["adjustlinks_name"] = "Arreglar la posici\195\179n de los enlaces de jugador/objeto",
    ["adjustlinks_desc"] = "Permite volver a hacer clic sobre los enlaces cuando la justificaci\195\179n es derecha o centro.",
    ["justification_name"] = "Establecer Justificaci\195\179n",
    ["justification_desc"] = "Establece la justificaci\195\179n para cada ventana",
    ["justification_perframename"] = "Justificaci\195\179n del Chat %d",
    ["justification_perframedesc"] = "Establece Justificaci\195\179n",
	["Center"] = "Centro",
	["Right"] = "Derecha",
	["Left"] = "Izquierda",
} end)
 
L:RegisterTranslations("deDE", function() return {
	["Justify"] = "Text B\195\188ndigkeit",
	["Chat window justification options."] = "Chatfenster Text B\195\188ndigkeits Optionen",
	["Set Justification"] = "B\195\188ndigkeit einstellen",
	["Set justification for each chat window."] = "Setze B\195\188ndigkeit f\195\188r jedes Fenster.",
	["Chat%dJustify"] = "Chat %d B\195\188ndigkeit",
	["Set justification for chat window %d."] = "Setze B\195\188ndigkeit f\195\188r Chatfenster %d.",
	["adjustlinks_name"] = "Fixiere Plazierung von Spieler/Item Links",
	["adjustlinks_desc"] = "Justiere Links um ihre anklickbarkeit zu gew\195\164rleisten wenn die B\195\188ndigkeit auf rechts oder mitte eingestellt ist.",    
	["justification_name"] = "B\195\188ndigkeit einstellen",
	["justification_desc"] = "Setze B\195\188ndigkeit f\195\188r jedes Fenster.",
	["justification_perframename"] = "Chat %d B\195\188ndigkeit",
	["justification_perframedesc"] = "B\195\188ndigkeit einstellen",
} end)

Prat_Justify = Prat:NewModule(Prat:RequestModuleName("PratJustify"))

Prat_Justify.revision = tonumber(string.sub("$Revision: 29189 $", 12, -3))

Prat_Justify.defaultDB = {
    on = true,
    adjustlinks = false,
    justification = {"LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT"}
}

local cat = Prat.Categories
Prat_Justify.Categories = { cat.TEXT, cat.FORMATTING,}

Prat_Justify.moduleOptions = {}
Prat_Justify.toggleOptions = {  adjustlinks=true}

function Prat_Justify:GetModuleOptions()
    self.moduleOptions = {
        name = L["Justify"],
        desc = L["Chat window justification options."],
        type = "group",
        args = {
            set = {
                name = L["Set Justification"],
                desc = L["Set justification for each chat window."],
                type = "group",
                order = 150,
                args = {
                    chat1 = {
                        name = string.format(L["Chat%dJustify"], 1),
                        desc = string.format(L["Set justification for chat window %d."], 1),
                        type = "text",
                        get = function() return self.db.profile.justification[1] end,
                        set = function(v) self.db.profile.justification[1] = v; self:SetJustify(1,v) end,
                        validate = {["LEFT"] = L["Left"], ["RIGHT"] = L["Right"], ["CENTER"] = L["Center"]}
                    },
                    chat2 = {
                        name = string.format(L["Chat%dJustify"], 2),
                        desc = string.format(L["Set justification for chat window %d."], 2),
                        type = "text",
                        get = function() return self.db.profile.justification[2] end,
                        set = function(v) self.db.profile.justification[2] = v; self:SetJustify(2,v) end,
                        validate = {["LEFT"] = L["Left"], ["RIGHT"] = L["Right"], ["CENTER"] = L["Center"]}
                    },
                    chat3 = {
                        name = string.format(L["Chat%dJustify"], 3),
                        desc = string.format(L["Set justification for chat window %d."], 3),
                        type = "text",
                        get = function() return self.db.profile.justification[3] end,
                        set = function(v) self.db.profile.justification[3] = v; self:SetJustify(3,v) end,
                        validate = {["LEFT"] = L["Left"], ["RIGHT"] = L["Right"], ["CENTER"] = L["Center"]}
                    },
                    chat4 = {
                        name = string.format(L["Chat%dJustify"], 4),
                        desc = string.format(L["Set justification for chat window %d."], 4),
                        type = "text",
                        get = function() return self.db.profile.justification[4] end,
                        set = function(v) self.db.profile.justification[4] = v; self:SetJustify(4,v) end,
                        validate = {["LEFT"] = L["Left"], ["RIGHT"] = L["Right"], ["CENTER"] = L["Center"]}
                    },
                    chat5 = {
                        name = string.format(L["Chat%dJustify"], 5),
                        desc = string.format(L["Set justification for chat window %d."], 5),
                        type = "text",
                        get = function() return self.db.profile.justification[5] end,
                        set = function(v) self.db.profile.justification[5] = v; self:SetJustify(5,v) end,
                        validate = {["LEFT"] = L["Left"], ["RIGHT"] = L["Right"], ["CENTER"] = L["Center"]}
                    },
                    chat6 = {
                        name = string.format(L["Chat%dJustify"], 6),
                        desc = string.format(L["Set justification for chat window %d."], 6),
                        type = "text",
                        get = function() return self.db.profile.justification[6] end,
                        set = function(v) self.db.profile.justification[6] = v; self:SetJustify(6,v) end,
                        validate = {["LEFT"] = L["Left"], ["RIGHT"] = L["Right"], ["CENTER"] = L["Center"]}
                    },
                    chat7 = {
                        name = string.format(L["Chat%dJustify"], 7),
                        desc = string.format(L["Set justification for chat window %d."], 7),
                        type = "text",
                        get = function() return self.db.profile.justification[7] end,
                        set = function(v) self.db.profile.justification[7] = v; self:SetJustify(7,v) end,
                        validate = {["LEFT"] = L["Left"], ["RIGHT"] = L["Right"], ["CENTER"] = L["Center"]}
                    }
                }
            },
        }
    }
   	return self.moduleOptions
end


function Prat_Justify:OnModuleEnable()
    for i=1,NUM_CHAT_WINDOWS do
        self:SetJustify(i,self.db.profile.justification[i])
    end

    self:SecureHook("ChatFrame_OnUpdate")
end

function Prat_Justify:OnModuleDisable()
    for i=1,NUM_CHAT_WINDOWS do
        self:SetJustify(i,"LEFT")
    end
end

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

function Prat_Justify:SetJustify(id, justification)
    local cf = getglobal("ChatFrame"..id)
    cf:SetJustifyH(justification)
end

function Prat_Justify:ChatFrame_OnUpdate(...)
    if ( not this:IsVisible() ) then
        return
    end
    if self.db.profile.adjustlinks then
        local just = self.db.profile.justification[this:GetID()]
        if just ~= "LEFT" then
            self:MoveLink(just == "CENTER" and 2 or 1, this:GetChildren())
        end
    end
end

function Prat_Justify:MoveLink(coeff, ...)
  for i=1,select("#", ...) do
    local o = select(i, ...)
        a,b,c,d,e = o:GetPoint()
        if b and b:GetObjectType() == "FontString" and o:GetObjectType() == "Button" then
        o:ClearAllPoints()
        o:SetPoint(a, b, c, (b:GetWidth()-b:GetStringWidth())/coeff, e)
        end
  end
end