--[[
Name: Prat_Editbox
Developed by: Prat Development Team
Inspired by: idChat2_Editbox by Industrial
Website: http://www.wowace.com/mediawiki/index.php?title=Prat_Editbox
Description: Module for Prat that repositions the editbox on top of the chatframe and hides its backdrop (default=on).
Dependencies: Prat
]]

Prat_Editbox = Prat:NewModule("editbox")

function Prat_Editbox:OnInitialize()
    self.db = Prat:AcquireDBNamespace("Editbox")
    Prat:RegisterDefaults("Editbox", "profile", {
        on = true,
        normal = false,
        attachtop = true,
        hidebackdrop = true
    })
    Prat.Options.args.editbox = {
        name = "Editbox",
        desc = "Editbox options",
        type = "group",
        args = {
            toggle = {
                name = "Toggle",
                desc = "Toggle the module on and off",
                type = "toggle",
                get = function() return self.db.profile.on end,
                set = function() self.db.profile.on = Prat:ToggleModuleActive("editbox") end
            },
            attachtop = {
                name = "AttachTop",
                desc = "Toggle repositioning editbox to top of chatframe on and off",
                type = "toggle",
                get = function() return self.db.profile.attachtop end,
                set = function(v)
                    self:AttachTop(v)
                    self.db.profile.attachtop = v
                end
            },
            hidebackdrop = {
                name = "HideBackdrop",
                desc = "Toggle hiding editbox backdrop on and off",
                type = "toggle",
                get = function() return self.db.profile.hidebackdrop end,
                set = function(v)
                    self:HideBackdrop(v)
                    self.db.profile.hidebackdrop = v
                end
            }
        }
    }
end

function Prat_Editbox:OnEnable()
    self:AttachTop(self.db.profile.attachtop)
    self:HideBackdrop(self.db.profile.hidebackdrop)
end

function Prat_Editbox:OnDisable()
    self:AttachTop(self.db.profile.normal)
    self:HideBackdrop(self.db.profile.normal)
end

function Prat_Editbox:AttachTop(enabled)
    local eb = VisorEditBox or ChatFrameEditBox
    eb:ClearAllPoints()
    if enabled then
        eb:SetPoint("BOTTOMLEFT",  "ChatFrame1", "TOPLEFT",  -5, 0)
        eb:SetPoint("BOTTOMRIGHT", "ChatFrame1", "TOPRIGHT", 5, 0)
    else
        eb:SetPoint("TOPLEFT",  "ChatFrame1", "BOTTOMLEFT",  -5, 0)
        eb:SetPoint("TOPRIGHT", "ChatFrame1", "BOTTOMRIGHT", 5, 0)
    end
end

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