---------------------------------------------------------------------------------
--
-- 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: PratEditbox
Revision: $Revision: 26318 $
Author(s): Curney (asml8ed@gmail.com)
           Krtek (krtek4@gmail.com)
Inspired by: idChat2_Editbox by Industrial
Website: http://www.wowace.com/files/index.php?path=Prat/
Documentation: http://www.wowace.com/wiki/Prat/Integrated_Modules#Editbox
SVN: http://svn.wowace.com/wowace/trunk/Prat/
Description: Module for Prat that adds editbox options.
Dependencies: Prat
]]

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

L:RegisterTranslations("enUS", function() return {
    ["Editbox"] = true,
    ["Editbox options."] = true,
    ["Set Alpha"] = true,
    ["Set the alpha of the editbox."] = true,
    ["Set Position"] = true,
    ["Set the position of the editbox."] = true,
    ["Set Width"] = true,
    ["Set the width of the editbox."] = true,
    ["Lock Position"] = true,
    ["Lock editbox position if undocked."] = true,
    ["Enable Arrowkeys"] = true,
    ["Enable using arrowkeys in editbox without the alt key."] = true,
    ["Toggle Sticky"] = true,
    ["Toggle text clearing when editbox is closed."] = true,
	["Top"] = true,
	["Bottom"] = true,
	["Undocked"] = true,
} end)

L:RegisterTranslations("frFR", function() return {
    ["Editbox"] = "Zone de saisie",
    ["Editbox options."] = "Options de la zone de saisie",
    ["Set Alpha"] = "Transparence",
    ["Set the alpha of the editbox."] = "R\195\168gle la transparence de la zone de saisie.",
    ["Set Position"] = "Position",
    ["Set the position of the editbox."] = "Change la position de la zone de saisie",
    ["Set Width"] = "Largeur",
    ["Set the width of the editbox."] = "R\195\168gle la largeur de la zone de saisie",
    ["Lock Position"] = "Verrouiller la position",
    ["Lock editbox position if undocked."] = "Verrouille la position de la zone de saisie si elle est libre.",
    ["Enable Arrowkeys"] = "Activer les fl\195\168ches du clavier",
    ["Enable using arrowkeys in editbox without the alt key."] = "Permet d'utiliser les fl\195\168ches du clavier sans utiliser Alt.",
    ["Toggle Sticky"] = "Persistance du texte",
    ["Toggle text clearing when editbox is closed."] = "Active/d\195\169sactive l'effacement du tete quand la zone de saisie est ferm\195\169e.",
	["Top"] = "Haut",
	["Bottom"] = "Bas",
	["Undocked"] = "Mouvement libre",
} end)

-- L:RegisterTranslations("zhCN", function() return {
-- 	["Editbox"] = "输入框",
-- 	["Editbox options."] = "输入框选项。",
-- 	["Alpha"] = "透明度",
-- 	["Set the alpha of the editbox."] = "设置输入框透明度。",
-- 	["Position"] = "位置",
-- 	["Set position of editbox."] = "设置输入框位置。",
-- 	["Width"] = "宽度",
-- 	["Set the width of the editbox."] = "设置输入框宽度。",
-- 	["Locked"] = "锁定",
-- 	["Lock editbox position is undocked."] = "锁定输入框位置。",
-- 	["Arrowkeys"] = "方向键",
-- 	["Toggle using arrowkeys in editbox without the alt key."] = "激活输入框使用方向键不带Alt键功能。",
--     ["Sticky"] = "粘滞",
--     ["Toggle text clearing when editbox is closed."] = "激活当聊天框关闭时清除文本。",
-- } end)

L:RegisterTranslations("koKR", function() return {
    ["Editbox"] = "입력창",
    ["Editbox options."] = "입력창 설정입니다.",
    ["Set Alpha"] = "투명도 설정",
    ["Set the alpha of the editbox."] = "입력창의 투명도를 설정합니다.",
    ["Set Position"] = "위치 설정",
    ["Set the position of the editbox."] = "입력창의 위치를 설정합니다.",
    ["Set Width"] = "넓이 설정",
    ["Set the width of the editbox."] = "입력창의 넓이를 설정합니다.",
    ["Lock Position"] = "위치 고정",
    ["Lock editbox position if undocked."] = "입력창의 위치를 고정합니다.",
    ["Enable Arrowkeys"] = "방향키 사용",
    ["Enable using arrowkeys in editbox without the alt key."] = "ALT키를 누르지 않고 입력창에 방향키를 사용합니다.",
    ["Toggle Sticky"] = "고정 사용",
    ["Toggle text clearing when editbox is closed."] = "입력창을 닫았을 때 입력글을 초기화합니다.",
	["Top"] = "상단",
	["Bottom"] = "하단",
	["Undocked"] = "분리됨",
} end)

-- L:RegisterTranslations("zhTW", function() return {
--     ["Editbox"] = "輸入框",
--     ["Editbox options."] = "輸入框選項。",
--     ["Alpha"] = "透明度",
--     ["Set the alpha of the editbox."] = "設定輸入框透明度。",
--     ["Position"] = "位置",
--     ["Set position of editbox."] = "設定輸入框位置。",
--     ["Width"] = "寬度",
--     ["Set the width of the editbox."] = "設定輸入框寬度。",
--     ["Locked"] = "鎖定",
--     ["Lock editbox position is undocked."] = "鎖定輸入框位置。",
--     ["Arrowkeys"] = "方向鍵",
--     ["Toggle using arrowkeys in editbox without the alt key."] = "切換無需按下ALT即可在輸入框中使用方向鍵功能。",
--     ["Sticky"] = "貼附",
--     ["Toggle text clearing when editbox is closed."] = "切換當輸入框關閉時清除文字。",
-- } end)

L:RegisterTranslations("esES", function() return {
    ["Editbox"] = "Caja de Edici\195\179n",
    ["Editbox options."] = "Opciones de la caja de edici\195\179n",
    ["Set Alpha"] = "Transparencia",
    ["Set the alpha of the editbox."] = "Establece la transparencia de la caja de edici\195\179n",
    ["Set Position"] = "Posici\195\179n",
    ["Set the position of the editbox."] = "Establece la posici\195\179n de la caja de edici\195\179n",
    ["Set Width"] = "Anchura",
    ["Set the width of the editbox."] = "Establece la anchura de la caja de edici\195\179n",
    ["Lock Position"] = "Bloqueado",
    ["Lock editbox position if undocked."] = "Bloquea la posici\195\179n de la caja de edici\195\179n cuando no est\195\161 anclada",
    ["Enable Arrowkeys"] = "Teclas de Direcci\195\179n",
    ["Enable using arrowkeys in editbox without the alt key."] = "Determina si se usan las teclas de direcci\195\179n en la caja de edici\195\179n sin la tecla Alt",
    ["Toggle Sticky"] = "Pegajoso",
    ["Toggle text clearing when editbox is closed."] = "Determina si el texto es borrado cuando la caja de edici\195\179n se cierra",
	["Top"] = "Arriba",
	["Bottom"] = "Abajo",
	["Undocked"] = "Suelto",

} end)
 
L:RegisterTranslations("deDE", function() return {
	["Editbox"] = "Eingabefeld",
	["Editbox options."] = "Eingabefeld Optionen",
	["Set Alpha"] = "Helligkeit einstellen",
	["Set the alpha of the editbox."] = "Stellt die Helligkeit f\195\188r das Eingabefeld ein.",
	["Set Position"] = "Position einstellen",
	["Set the position of the editbox."] = "Position des Eingabefelds festlegen",
	["Set Width"] = "Breite einstellen",
	["Set the width of the editbox."] = "Stellt die Breite des Eingabefelds ein.",
	["Lock Position"] = "Sperre Position",
	["Lock editbox position if undocked."] = "Sperrt die Position des Eingabefelds falls freigegeben.",
	["Enable Arrowkeys"] = "Aktiviere Pfeiltasten",
	["Enable using arrowkeys in editbox without the alt key."] = "Aktiviere das benutzen von Pfeiltasten ohne die Alt-Taste benutzen zu m\195\188ssen.",
	["Toggle Sticky"] = "Aktiviere Haftend",
	["Toggle text clearing when editbox is closed."] = "Aktiviere Textentfernung wenn das Eingabefeld geschlossen wird.",
} end)

Prat_Editbox = Prat:NewModule(Prat:RequestModuleName("PratEditbox"))

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

-- Prat categories relevant to this module. list at: http://groups.google.com/group/wow-prat/web/prat-categories
local cat = Prat.Categories
Prat_Editbox.Categories	= { cat.FRAME, cat.BUTTON, cat.TEXT }	-- eg { cat.TEXT, cat.INFO }

Prat_Editbox.defaultDB = {
        on = true,
        alpha = 1,
        position = "TOP",
        width = 400,
        undocked = {point = "CENTER", relativeTo="UIParent", relativePoint = "CENTER", xoff = 0, yoff = 0},
        locked = false,
        sticky = false,
        stickychan = false,
        arrowkeys = true
}

Prat_Editbox.moduleOptions = {}

function Prat_Editbox:GetModuleOptions()
    self.moduleOptions = {
        name = L["Editbox"],
        desc = L["Editbox options."],
        type = "group",
        args = {
            alpha = {
                name = L["Set Alpha"],
                desc = L["Set the alpha of the editbox."],
                type = "range",
                get = function() return self.db.profile.alpha end,
                set = function(v)
                    self.db.profile.alpha = v
                    self:Alpha(v)
                end,
                min = 0,
                max = 1,
                step = 0.05,
                order = 195
            },
            position = {
                name = L["Set Position"],
                desc = L["Set the position of the editbox."],
                type = "text",
                get = function() return self.db.profile.position end,
                set = function(v)
                    self.db.profile.position = v
                    self:Position(v)
                end,
                validate = {["TOP"] = L["Top"], ["BOTTOM"] = L["Bottom"], ["UNDOCKED"] = L["Undocked"]},
                order = 196
            },
            width = {
                name = L["Set Width"],
                desc = L["Set the width of the editbox."],
                type = "range",
                get = function() return self.db.profile.width end,
                set = function(v)
                    self.db.profile.width = v
                    self:Position(self.db.profile.position)
                end,
                min = 267,
                max = 800,
                step = 1,
                disabled = function() if self.db.profile.position == "UNDOCKED" then return false else return true end end,
                order = 197
            },
            locked = {
                name = L["Lock Position"],
                desc = L["Lock editbox position if undocked."],
                type = "toggle",
                get = function() return self.db.profile.locked end,
                set = function(v)
                    self.db.profile.locked = v
                    self:Position(self.db.profile.position)
                end,
                disabled = function() if self.db.profile.position == "UNDOCKED" then return false else return true end end,
                order = 198
            },
            arrowkeys = {
                name = L["Enable Arrowkeys"],
                desc = L["Enable using arrowkeys in editbox without the alt key."],
                type = "toggle",
                get = function() return self.db.profile.arrowkeys end,
                set = function(v)
                    self.db.profile.arrowkeys = v
                    self:ArrowKeys(v)
                end,
                order = 199
            },
            sticky = {
                name = L["Toggle Sticky"],
                desc = L["Toggle text clearing when editbox is closed."],
                type = "toggle",
                get = function() return self.db.profile.sticky end,
                set = function(v)
                    self.db.profile.sticky = v
                    self:Sticky_Old(v)
                end,
                disabled = function() return true end,
                order = 200
            },
        }
    }
   	return self.moduleOptions
end



function Prat_Editbox:OnModuleEnable()
--	self:SetDebugging(true)
    self:Position(self.db.profile.position)
    self:Alpha(self.db.profile.alpha)
    self:ArrowKeys(self.db.profile.arrowkeys)
--    self:Sticky(self.db.profile.sticky)
end

function Prat_Editbox:OnModuleDisable()
    self:Position("BOTTOM")
    self:Alpha(1)
    self:ArrowKeys(false)
end

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

function Prat_Editbox:Alpha(value)
    local eb = VisorEditbox or ChatFrameEditBox
    for i,v in ipairs({eb:GetRegions()}) do
        if i==6 or i==7 or i==8 then
            v:SetAlpha(value)
        end
    end
end

function Prat_Editbox:ArrowKeys(enabled)
    local eb = VisorEditBox or ChatFrameEditBox
    if enabled then eb:SetAltArrowKeyMode(false) else eb:SetAltArrowKeyMode(true) end
end

function Prat_Editbox:Position(option)
    local eb = VisorEditBox or ChatFrameEditBox
    eb:ClearAllPoints()
    if option == "TOP" then
        eb:SetPoint("BOTTOMLEFT",  "ChatFrame1", "TOPLEFT",  -5, 0)
        eb:SetPoint("BOTTOMRIGHT", "ChatFrame1", "TOPRIGHT", 5, 0)
    end
    if option == "BOTTOM" then
        eb:SetPoint("TOPLEFT",  "ChatFrame1", "BOTTOMLEFT",  -5, 0)
        eb:SetPoint("TOPRIGHT", "ChatFrame1", "BOTTOMRIGHT", 5, 0)
    end
    if option == "UNDOCKED" then
        eb:SetMovable(true)
        eb:EnableMouse(true)
        eb:RegisterForDrag("LeftButton")
        eb:SetScript("OnDragStart", function() if not self.db.profile.locked then eb:StartMoving() end end)
        eb:SetScript("OnDragStop", function()
            eb:StopMovingOrSizing()
            local point, relativeTo, relativePoint, xoff, yoff = eb:GetPoint()
            self.db.profile.undocked.point = point
            -- self.db.profile.undocked.relativeTo = relativeTo:GetName() -- this causes game crash, keep commented out
            self.db.profile.undocked.relativePoint = relativePoint
            self.db.profile.undocked.xoff = xoff
            self.db.profile.undocked.yoff = yoff
        end)
        eb:SetPoint(self.db.profile.undocked.point, self.db.profile.undocked.relativeTo, self.db.profile.undocked.relativePoint, self.db.profile.undocked.xoff, self.db.profile.undocked.yoff)
        eb:SetWidth(self.db.profile.width)
    end
end

function Prat_Editbox:Sticky_Old(enabled)
	if true then return end
    if not enabled then
        DEFAULT_CHAT_FRAME.editBox:SetScript("OnEscapePressed",
            function() ChatEdit_OnEscapePressed(this) end
        )
		DEFAULT_CHAT_FRAME.editBox:SetScript("OnEnterPressed",
			function() ChatEdit_OnEnterPressed() end
		)
        self:UnhookAll()
    else
        DEFAULT_CHAT_FRAME.editBox:SetScript("OnEscapePressed",
            function()
                this.oldText = this:GetText()
                this.oldChatType = this.chatType
                ChatEdit_OnEscapePressed(this)
            end
        )
        DEFAULT_CHAT_FRAME.editBox:SetScript("OnEnterPressed",
            function()
                this.oldText = nil
                this.oldChatType = nil
                ChatEdit_OnEnterPressed()
            end
        )
        self:SecureHook("ChatFrame_OpenChat",
            function(text, chatFrame)
                local editBox =  securecall(GetDefaultChatEditBox)
                if (not chatFrame or chatFrame == DEFAULT_CHAT_FRAME) and text == "" and
                        editBox.oldText and editBox.oldText ~= "" then
                    editBox:SetText(editBox.oldText)
                    editBox.chatType = editBox.oldChatType
                    editBox:Show()
            --    else
             --       self.hooks["ChatFrame_OpenChat"](text, chatFrame)
                end
            end
        )
        self:SecureHook("ChatFrame_ReplyTell",
            function(chatFrame)
                if ( not chatFrame ) then
		            chatFrame = DEFAULT_CHAT_FRAME;
                end
                chatFrame.editBox.oldText = nil
               -- self.hooks["ChatFrame_ReplyTell"](chatFrame)
            end
        )
        self:SecureHook("ChatFrame_ReplyTell2",
            function(chatFrame)
                if ( not chatFrame ) then
		            chatFrame = DEFAULT_CHAT_FRAME;
                end
                chatFrame.editBox.oldText = nil
               -- self.hooks["ChatFrame_ReplyTell2"](chatFrame)
            end
        )
    end
end