local _,ns = ...
local L = ns.L

--ns.frameOutterFrame
--local ASMailBoxFrame = CreateFrame("Frame", nil, UIParent)
local ASMailBoxFrame = ns.Utils.crearFrameConTextura (ns.frameOutterFrame,304,504,0,157,1,"mailpanel")
--local ASMailBoxFrame = CreateFrame("Frame", nil, ns.frameOutterFrame)  --cambiar attached to mailframe para show y hide
--ASMailBoxFrame:SetPoint("TOP",MailFrame,"BOTTOM",0,22)
local saveOptions
local bgMOF


--[[ ASMailBoxFrame:SetSize(200,120)
ASMailBoxFrame.tex = ASMailBoxFrame:CreateTexture()
ASMailBoxFrame.tex:SetAllPoints(ASMailBoxFrame)
--ASMailBoxFrame.tex:SetTexture("Interface\\AddOns\\AutoScrap\\fondo2.blp")
ASMailBoxFrame.tex:SetColorTexture(255/255, 255/255, 255/255 , 0.2); --rgb ]]

ASMailBoxFrame:RegisterEvent("MAIL_SHOW");
--AutoScrap:RegisterEvent("MAIL_INBOX_UPDATE");
ASMailBoxFrame:RegisterEvent("MAIL_CLOSED");
--ASMailBoxFrame:RegisterEvent("MAIL_SEND_INFO_UPDATE");
ASMailBoxFrame:RegisterEvent("MAIL_SEND_SUCCESS");
ASMailBoxFrame:RegisterEvent("MAIL_FAILED");
--ASMailBoxFrame:RegisterEvent("MAIL_SUCCESS");	
--ASMailBoxFrame:RegisterEvent("CLOSE_INBOX_ITEM");
--ASMailBoxFrame:RegisterEvent("MAIL_LOCK_SEND_ITEMS");
--ASMailBoxFrame:RegisterEvent("MAIL_UNLOCK_SEND_ITEMS");
--ASMailBoxFrame:RegisterEvent("TRIAL_STATUS_UPDATE");
local countStamp = 0
local enableDelayed
local enableAll
local function mailHandler(self, event, ...)
    --print(event,...)
    if event=="MAIL_SHOW" then
        --ASMailBoxFrame:Show() --attached to mailframe
        --ns.mostrarFrames()
        ASMailBoxFrame:SetParent(MailFrame)
        ASMailBoxFrame:SetPoint("TOPLEFT",MailFrame,"TOPRIGHT",0,0)
        enableAll()
        ASMailBoxFrame:Show()
    elseif event=="MAIL_CLOSED" then
        --ASMailBoxFrame:Hide() --attached to mailframe
        ASMailBoxFrame:Hide()
        bgMOF:Hide()
        --ns.ocultarFrames()
    elseif event=="MAIL_SEND_SUCCESS" or  event=="MAIL_FAILED" then
        --ASMailBoxFrame:Hide() --attached to mailframe
        enableDelayed()
    end
end

ASMailBoxFrame:SetScript("OnEvent", mailHandler)
OpenMailFrame:SetScript("OnShow", function()
    ASMailBoxFrame:SetParent(OpenMailFrame)
    ASMailBoxFrame:SetPoint("TOPLEFT",OpenMailFrame,"TOPRIGHT",0,0)
end)
OpenMailFrame:SetScript("OnHide", function()
    ASMailBoxFrame:SetParent(MailFrame)
    ASMailBoxFrame:SetPoint("TOPLEFT",MailFrame,"TOPRIGHT",0,0)
end)

--ASMailBoxFrame:Hide() --attached to mailframe empieza en show, hace lo que el padre
ASMailBoxFrame.ebBackGround = ns.Utils.crearFrameConTextura (ASMailBoxFrame,146,304,0,29,1,"mailpanel")
ASMailBoxFrame.ebBackGround:SetPoint("TOP", ASMailBoxFrame, "TOP", 0, -24)

ASMailBoxFrame.spam=ns.Utils.crearFrameConTextura (ASMailBoxFrame,0,105,366,385,1,"mailpanel")
ASMailBoxFrame.spam:SetPoint("TOPLEFT",ASMailBoxFrame,"TOPLEFT",31-6,-27+15+4)
ASMailBoxFrame.spam:SetScale(0.80)

ASMailBoxFrame.eb = CreateFrame("EditBox", nil, ASMailBoxFrame, "AutoCompleteEditBoxTemplate")
ASMailBoxFrame.eb:SetSize(130, 20)
ASMailBoxFrame.eb:SetMaxLetters(77)
ASMailBoxFrame.eb:SetPoint("LEFT", ASMailBoxFrame.ebBackGround, "LEFT", 28, 0)
ASMailBoxFrame.eb:SetPoint("RIGHT", ASMailBoxFrame.ebBackGround, "RIGHT", -5, 0)
ASMailBoxFrame.eb:SetMultiLine(false)
ASMailBoxFrame.eb:SetAutoFocus(false) -- probar false
ASMailBoxFrame.eb:SetFontObject("ChatFontNormal")
--eb:SetText("oxx")
--eb:HighlightText(0)
--eb:SetScript("OnEscapePressed",function() dialog:Hide() end)
--eb:SetScript("OnEnterPressed",function() dialog:Hide() comprobarNumeros(eb:GetText()) end)RegisterEvent

--código interesante para poner una textura alrededor de un editbox sin poner de fondo un layer.
--[[ ASMailBoxFrame.eb.left = ASMailBoxFrame.eb:CreateTexture()
ASMailBoxFrame.eb.left:SetTexture("Interface\\Common\\Common-Input-Border")
ASMailBoxFrame.eb.left:SetSize(8,20)
ASMailBoxFrame.eb.left:SetPoint("TOPLEFT",-8,0)
ASMailBoxFrame.eb.left:SetTexCoord(0,0.0625,0,0.625)
ASMailBoxFrame.eb.middle = ASMailBoxFrame.eb:CreateTexture()
ASMailBoxFrame.eb.middle:SetTexture("Interface\\Common\\Common-Input-Border")
ASMailBoxFrame.eb.middle:SetSize(150,20)
ASMailBoxFrame.eb.middle:SetPoint("LEFT",ASMailBoxFrame.eb.left,"RIGHT")
ASMailBoxFrame.eb.middle:SetTexCoord(0.0625,0.9375,0,0.625)
ASMailBoxFrame.eb.right = ASMailBoxFrame.eb:CreateTexture()
ASMailBoxFrame.eb.right:SetTexture("Interface\\Common\\Common-Input-Border")
ASMailBoxFrame.eb.right:SetSize(8,20)
ASMailBoxFrame.eb.right:SetPoint("LEFT",ASMailBoxFrame.eb.middle,"RIGHT")
ASMailBoxFrame.eb.right:SetTexCoord(0.9375,1,0,0.625) ]]


    AutoCompleteEditBox_SetAutoCompleteSource(ASMailBoxFrame.eb, GetAutoCompleteResults, AUTO_COMPLETE_ACCOUNT_CHARACTER, AUTOCOMPLETE_FLAG_NONE);
                --eb.addHighlightedText = true;
                --eb.autoCompleteContext = "mail";

ASMailBoxFrame.eb:SetScript("OnEnterPressed", function (self,...)
    if ( AutoCompleteEditBox_OnEnterPressed(self) ) then
        --SendMailSubjectEditBox:SetFocus();
        self:ClearFocus();
    else
        --self:SetCursorPosition(0)
        self:ClearFocus();
    end
end)
ASMailBoxFrame.eb:SetScript("OnEscapePressed", function (self,...)      
    AutoCompleteEditBox_OnEscapePressed(self)  
    self:ClearFocus();
end)


--[[ eb:SetScript("OnTextChanged", function (self,...)
    
end) ]]

--[[ eb:SetScript("OnEnterPressed", function (self,...)
        self:ClearFocus();
end)
eb:SetScript("OnEscapePressed", function (self,...)            
    self:ClearFocus();
end)
eb:SetScript("OnEditFocusLost", function (self,...)
    --print("focus lost")
end)
 ]]
--[[ eb:SetScript("OnTabPressed", function (self,...)
    if ( not AutoCompleteEditBox_OnTabPressed(self) ) then
        EditBox_HandleTabbing(self, SEND_MAIL_TAB_LIST);
    end
end)    
eb:SetScript("OnEditFocusLost", function (self,...)
    AutoCompleteEditBox_OnEditFocusLost(self);
    EditBox_ClearHighlight(self)
end)
eb:SetScript("OnEnterPressed", function (self,...)
if ( not AutoCompleteEditBox_OnEnterPressed(self) ) then
    SendMailSubjectEditBox:SetFocus();
end
end)
eb:SetScript("OnEscapePressed", function (self,...)            
    if ( not AutoCompleteEditBox_OnEscapePressed(self) ) then
        EditBox_ClearFocus(self);
    end
end) ]]
--[[ eb:SetScript("OnTextChanged", function (self,...) 
    AutoCompleteEditBox_OnTextChanged(self, userInput)
end) ]]

ASMailBoxFrame.enviarLock = CreateFrame("Button", nil, ASMailBoxFrame, "UIPanelButtonTemplate")
ASMailBoxFrame.enviarLock:SetSize(130, 24)
ASMailBoxFrame.enviarLock:SetText(L["Send LockedBox"])
ASMailBoxFrame.enviarLock:SetPoint("TOPLEFT", ASMailBoxFrame.ebBackGround, "BOTTOMLEFT", 5-3, -1)

ASMailBoxFrame.enviar = CreateFrame("Button", nil, ASMailBoxFrame, "UIPanelButtonTemplate")
ASMailBoxFrame.enviar:SetSize(130, 24)
ASMailBoxFrame.enviar:SetText(L["Send Inventory"])
ASMailBoxFrame.enviar:SetPoint("TOP", ASMailBoxFrame.enviarLock, "BOTTOM", 0, -2)

ASMailBoxFrame.enviarMoney = CreateFrame("Button", nil, ASMailBoxFrame, "UIPanelButtonTemplate")
ASMailBoxFrame.enviarMoney:SetSize(130, 24)
ASMailBoxFrame.enviarMoney:SetText(L["Send all money"])
ASMailBoxFrame.enviarMoney:SetPoint("TOP", ASMailBoxFrame.enviar, "BOTTOM", 0, -2)

--[[ local dialogButton = CreateFrame("Button", nil, ASMailBoxFrame, "UIPanelButtonTemplate")
--dialogButton:SetNormalTexture(130832) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Up"
--dialogButton:SetPushedTexture(130830) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Down"
--dialogButton:SetHighlightTexture(130831) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight"
dialogButton:SetSize(50, 22)
dialogButton:SetText("Reload")
dialogButton:SetPoint("CENTER", ASMailBoxFrame, "BOTTOM", 0, 11)
dialogButton:SetScript("OnClick", function() ReloadUI() end) ]]

enableAll = function ()
    ASMailBoxFrame.enviar:Enable()
    ASMailBoxFrame.enviarMoney:Enable()
    ASMailBoxFrame.enviarLock:Enable()
end
local function disableAll()
    ASMailBoxFrame.enviar:Disable()
    ASMailBoxFrame.enviarMoney:Disable()
    ASMailBoxFrame.enviarLock:Disable()
    local currentCountStamp = countStamp
    C_Timer.After(2000/1000,function () -- si no ha dicho nada en 4secs.
        --print("Waking up current:",currentCountStamp, " now:",countStamp, ASMailBoxFrame.enviar:IsEnabled())
        if not ASMailBoxFrame.enviar:IsEnabled() and countStamp==currentCountStamp then
            enableAll()
        end
    end)
end

local function enviarItems()
    SendMail(ASMailBoxFrame.eb:GetText(), "AutoScrap", "")
    AutoScrap_CharSettings.destinatario = ASMailBoxFrame.eb:GetText()
    countStamp = countStamp + 1
    if countStamp == 999999 then
        countStamp = 0
    end
    disableAll()
end


enableDelayed = function ()
    if not ASMailBoxFrame.enviar:IsEnabled() then
        local currentCountStamp = countStamp
        C_Timer.After(250/1000,function ()
            --print("curent:",currentCountStamp, " now:",countStamp, ASMailBoxFrame.enviar:IsEnabled())
            if countStamp==currentCountStamp then
                enableAll() --recordar los enables de si no llega en 4 segundos de arriba.
            end
        end)
    end
end

local function enviarTodo()
    ClearSendMail()
    if AutoScrap_CharSettings.mailOptInventory and AutoScrap_CharSettings.mailOptInventoryName ~= "" then
        ASMailBoxFrame.eb:SetText(AutoScrap_CharSettings.mailOptInventoryName)
    end
    if ASMailBoxFrame.eb:GetText() ~= "" then
        local sentItems = 0
        for b = 0, NUM_TOTAL_BAG_FRAMES do
            for i = 1, C_Container.GetContainerNumSlots(b) do
                local itemID = C_Container.GetContainerItemID(b,i)
                --local texture, itemCount, locked = GetContainerItemInfo(b,i)
                local tablaContainerItemInfo = C_Container.GetContainerItemInfo(b,i)
                if tablaContainerItemInfo and tablaContainerItemInfo["isLocked"]~=nil and itemID and not tablaContainerItemInfo["isLocked"] and sentItems<ATTACHMENTS_MAX_SEND and not AutoScrap_CharSettings.noVenderList[itemID] and not (C_Item.IsBound(ItemLocation:CreateFromBagAndSlot(b,i)) == true) then
                    ClearCursor()
                    C_Container.PickupContainerItem(b,i)
                    ClickSendMailItemButton()
                    sentItems=sentItems+1
                end
            end
        end
        if CursorHasItem() then
            ClearCursor()
        end
        if sentItems>0 then
            enviarItems()
        end
    end
end

local function enviarBoxes()
    ClearSendMail()
    if AutoScrap_CharSettings.mailOptLockedBox and AutoScrap_CharSettings.mailOptLockedBoxName ~= "" then
        ASMailBoxFrame.eb:SetText(AutoScrap_CharSettings.mailOptLockedBoxName)
    end
    if ASMailBoxFrame.eb:GetText() ~= "" then
        local sentItems = 0
        for b = 0, NUM_TOTAL_BAG_FRAMES do
            for i = 1, C_Container.GetContainerNumSlots(b) do
                local itemID = C_Container.GetContainerItemID(b,i)
                --local texture, itemCount, locked = C_Container.GetContainerItemInfo(b,i)
                local tablaContainerItemInfo = C_Container.GetContainerItemInfo(b,i)
                --if and itemID and not tablaContainerItemInfo["isLocked"]
                if tablaContainerItemInfo and tablaContainerItemInfo["isLocked"]~=nil and itemID and not tablaContainerItemInfo["isLocked"] and sentItems<ATTACHMENTS_MAX_SEND and not AutoScrap_CharSettings.noVenderList[itemID] and not (C_Item.IsBound(ItemLocation:CreateFromBagAndSlot(b,i)) == true) then
                    local itemName, itemLink, itemQuality, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, sellPrice, classID, subclassID, bindType, expacID, setID, isCraftingReagent = GetItemInfo(itemID)
                    if classID and subclassID and classID == 15 and subclassID == 0 and ns.Utils.isBox(itemID, b, i) then
                        ClearCursor()
                        C_Container.PickupContainerItem(b,i)
                        ClickSendMailItemButton()
                        sentItems=sentItems+1
                    end
                end
            end
        end
        if CursorHasItem() then
            ClearCursor()
        end
        if sentItems>0 then
            enviarItems()
        end
    end
end

local function sendMoneyFinally()
    local money = GetMoney()-30
        if money > 0 then
            ClearSendMail()
            MailFrameTab2:Click()
            SendMailNameEditBox:SetText(ASMailBoxFrame.eb:GetText())
            SendMailMoneyGold:SetText(math.floor(money/10000))
            SendMailMoneySilver:SetText(math.floor((money%10000)/100))
            SendMailMoneyCopper:SetText(math.floor(money%100))
            SendMailSubjectEditBox:SetText("AutoScrap money")
            C_Timer.After(0,function()
                SendMailMailButton:Click()
            end)
            disableAll()
        end
end

local function enviarDinero()
    if AutoScrap_CharSettings.mailOptMoney and AutoScrap_CharSettings.mailOptMoneyName ~= "" then
        ASMailBoxFrame.eb:SetText(AutoScrap_CharSettings.mailOptMoneyName)
    end
    local sentItems = 0
    if ASMailBoxFrame.eb:GetText() ~= "" and MailFrameTab2 and SendMailNameEditBox and SendMailMoneyGold and SendMailMoneySilver and SendMailMoneyCopper and SendMailSubjectEditBox then
        for b = 0, NUM_TOTAL_BAG_FRAMES do
            for i = 1, C_Container.GetContainerNumSlots(b) do
                local itemID = C_Container.GetContainerItemID(b,i)
                --local texture, itemCount, locked = C_Container.GetContainerItemInfo(b,i)
                local tablaContainerItemInfo = C_Container.GetContainerItemInfo(b,i)
                if tablaContainerItemInfo and tablaContainerItemInfo["isLocked"]~=nil and itemID and not tablaContainerItemInfo["isLocked"] and not AutoScrap_CharSettings.noVenderList[itemID] and not (C_Item.IsBound(ItemLocation:CreateFromBagAndSlot(b,i)) == true) then
                    sentItems=sentItems+1
                end
            end
        end
        if sentItems>0 then
            RaidNotice_AddMessage(RaidWarningFrame, L["You still have items on your inventory"], ChatTypeInfo["SYSTEM"])
            return
        end
        sendMoneyFinally()
    end
end
--HideUIPanel(MailFrame)
ASMailBoxFrame.enviar:SetScript("OnClick", enviarTodo)
ASMailBoxFrame.enviarLock:SetScript("OnClick", enviarBoxes)
ASMailBoxFrame.enviarMoney:SetScript("OnClick", enviarDinero)
ASMailBoxFrame:Hide()

local function AddDestinatario(name)
    ASMailBoxFrame.eb:SetText(name)
end
ns.AddDestinatario = AddDestinatario


local oSizeX, oSizeY =  440,300-50-20
bgMOF = CreateFrame("FRAME", nil, UIParent )
bgMOF:SetSize(oSizeX-5-2,oSizeY-17-23)
bgMOF:SetPoint("CENTER")
bgMOF:SetClampedToScreen(true)
bgMOF:SetMovable(true);
bgMOF:SetResizable(false);
bgMOF:EnableMouse(true);
bgMOF:SetToplevel(true);
bgMOF:SetFrameStrata("HIGH");
bgMOF:SetScript("OnMouseDown",function (self,button) if button=="LeftButton" then bgMOF:StartMoving() end end); --StartSizing()
bgMOF:SetScript("OnMouseUp",function (self,button) if button=="LeftButton" then bgMOF:StopMovingOrSizing()end end);
bgMOF:Hide()

local mailOptionsFrame = CreateFrame("Frame", nil, bgMOF, "BackdropTemplate")
mailOptionsFrame:SetPoint("CENTER",bgMOF, "CENTER")
mailOptionsFrame:SetSize(oSizeX, oSizeY )
mailOptionsFrame:SetBackdrop({
    bgFile = "Interface\\AddOns\\AutoScrap\\images\\backdrop\\woodbackdropground",
    edgeFile = "Interface\\AddOns\\AutoScrap\\images\\backdrop\\woodbackdrop",
    edgeSize = 55,
    tileEdge = true,
    tile = true, --background
    tileSize = 70, --background
    insets = { left = 10, right = 10, top = 25, bottom = 30 },
})

--mailOptionsFrame:SetBackdropColor(25/255, 40/256, 60/255  , 1)
mailOptionsFrame:SetBackdropColor(30/255, 50/256, 150/255  , 1)
--mailOptionsFrame:SetBackdropBorderColor(255/255, 215/256, 178/255  , 1)
--mailOptionsFrame:SetScale(1)

--[[ mailOptionsFrame.c = CreateFrame("Button", nil, mailOptionsFrame)
mailOptionsFrame.c:SetNormalTexture(130832) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Up"
mailOptionsFrame.c:SetPushedTexture(130830) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Down"
mailOptionsFrame.c:SetHighlightTexture(130831) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight"
mailOptionsFrame.c:SetSize(32, 32)
mailOptionsFrame.c:SetPoint("TOPRIGHT", mailOptionsFrame, "TOPRIGHT", -11, -33)
mailOptionsFrame.c:SetScript("OnClick", function() bgMOF:Hide() end) ]]

mailOptionsFrame.spam=ns.Utils.crearFrameConTextura (mailOptionsFrame,0,105,366,385,1,"mailpanel")
mailOptionsFrame.spam:SetPoint("TOPLEFT",mailOptionsFrame,"TOPLEFT",31,-27)
mailOptionsFrame.spam:SetScale(0.80)

local textLockedBox = CreateFrame("CheckButton", nil, mailOptionsFrame, "UICheckButtonTemplate")
textLockedBox:SetPoint("TOPLEFT", mailOptionsFrame, "TOPLEFT", 25, -22-25)
--textLockedBox.text:SetText(L["Send LockedBox always to:"]) -- no me gusta el tipo de letra del checkbox
textLockedBox.texto = mailOptionsFrame:CreateFontString (nil, "OVERLAY", "GameFontNormal")
textLockedBox.texto:SetText(L["Send LockedBox always to:"])
textLockedBox.texto:SetPoint("LEFT",textLockedBox,"RIGHT")

textLockedBox.ebBackGround = ns.Utils.crearFrameConTextura (mailOptionsFrame,146,304,0,29,1,"mailpanel")
textLockedBox.ebBackGround:SetPoint("LEFT", textLockedBox.texto, "RIGHT", 5 , 0)

textLockedBox.eb = CreateFrame("EditBox", nil, textLockedBox, "AutoCompleteEditBoxTemplate")
textLockedBox.eb:SetSize(130, 20)
textLockedBox.eb:SetMaxLetters(77)
textLockedBox.eb:SetPoint("LEFT", textLockedBox.ebBackGround, "LEFT", 28, 0)
textLockedBox.eb:SetPoint("RIGHT", textLockedBox.ebBackGround, "RIGHT", -5, 0)
textLockedBox.eb:SetMultiLine(false)
textLockedBox.eb:SetAutoFocus(false) -- probar false
textLockedBox.eb:SetFontObject("ChatFontNormal")

AutoCompleteEditBox_SetAutoCompleteSource(textLockedBox.eb, GetAutoCompleteResults, AUTO_COMPLETE_ACCOUNT_CHARACTER, AUTOCOMPLETE_FLAG_NONE);

textLockedBox.eb:SetScript("OnEnterPressed", function (self,...)
    if ( AutoCompleteEditBox_OnEnterPressed(self) ) then
        --SendMailSubjectEditBox:SetFocus();
        self:ClearFocus()
    else
        --self:SetCursorPosition(0)
        self:ClearFocus()
    end
end)
textLockedBox.eb:SetScript("OnEscapePressed", function (self,...)
    AutoCompleteEditBox_OnEscapePressed(self)
    self:ClearFocus()
end)
textLockedBox.eb:SetScript("OnTextChanged", function (self,...)
    --print("changed",...,textLockedBox.eb:GetText())
    AutoCompleteEditBox_OnTextChanged(self,...)
    if textLockedBox.eb:GetText() == "" then
        textLockedBox:SetChecked(false)
    else
        textLockedBox:SetChecked(true)
    end
end)


local textInventory = CreateFrame("CheckButton", nil, mailOptionsFrame, "UICheckButtonTemplate")
textInventory:SetPoint("TOP", textLockedBox, "BOTTOM", 0, 0)
--textLockedBox.text:SetText(L["Send LockedBox always to:"]) -- no me gusta el tipo de letra del checkbox
textInventory.texto = mailOptionsFrame:CreateFontString (nil, "OVERLAY", "GameFontNormal")
textInventory.texto:SetText(L["Send Inventory always to:"])
textInventory.texto:SetPoint("LEFT",textInventory,"RIGHT")

textInventory.ebBackGround = ns.Utils.crearFrameConTextura (mailOptionsFrame,146,304,0,29,1,"mailpanel")
textInventory.ebBackGround:SetPoint("LEFT", textInventory.texto, "RIGHT", 5 , 0)

textInventory.eb = CreateFrame("EditBox", nil, textInventory, "AutoCompleteEditBoxTemplate")
textInventory.eb:SetSize(130, 20)
textInventory.eb:SetMaxLetters(77)
textInventory.eb:SetPoint("LEFT", textInventory.ebBackGround, "LEFT", 28, 0)
textInventory.eb:SetPoint("RIGHT", textInventory.ebBackGround, "RIGHT", -5, 0)
textInventory.eb:SetMultiLine(false)
textInventory.eb:SetAutoFocus(false) -- probar false
textInventory.eb:SetFontObject("ChatFontNormal")

AutoCompleteEditBox_SetAutoCompleteSource(textInventory.eb, GetAutoCompleteResults, AUTO_COMPLETE_ACCOUNT_CHARACTER, AUTOCOMPLETE_FLAG_NONE);

textInventory.eb:SetScript("OnEnterPressed", function (self,...)
    if ( AutoCompleteEditBox_OnEnterPressed(self) ) then
        --SendMailSubjectEditBox:SetFocus();
        self:ClearFocus()
    else
        --self:SetCursorPosition(0)
        self:ClearFocus()
    end
end)
textInventory.eb:SetScript("OnEscapePressed", function (self,...)
    AutoCompleteEditBox_OnEscapePressed(self)
    self:ClearFocus()
end)
textInventory.eb:SetScript("OnTextChanged", function (self,...)
    --print("changed",...,textLockedBox.eb:GetText())
    AutoCompleteEditBox_OnTextChanged(self,...)
    if textInventory.eb:GetText() == "" then
        textInventory:SetChecked(false)
    else
        textInventory:SetChecked(true)
    end
end)


local textMoney = CreateFrame("CheckButton", nil, mailOptionsFrame, "UICheckButtonTemplate")
textMoney:SetPoint("TOP", textInventory, "BOTTOM", 0, 0)
--textLockedBox.text:SetText(L["Send LockedBox always to:"]) -- no me gusta el tipo de letra del checkbox
textMoney.texto = mailOptionsFrame:CreateFontString (nil, "OVERLAY", "GameFontNormal")
textMoney.texto:SetText(L["Send Money always to:"])
textMoney.texto:SetPoint("LEFT",textMoney,"RIGHT")

textMoney.ebBackGround = ns.Utils.crearFrameConTextura (mailOptionsFrame,146,304,0,29,1,"mailpanel")
textMoney.ebBackGround:SetPoint("LEFT", textMoney.texto, "RIGHT", 5 , 0)

textMoney.eb = CreateFrame("EditBox", nil, textMoney, "AutoCompleteEditBoxTemplate")
textMoney.eb:SetSize(130, 20)
textMoney.eb:SetMaxLetters(77)
textMoney.eb:SetPoint("LEFT", textMoney.ebBackGround, "LEFT", 28, 0)
textMoney.eb:SetPoint("RIGHT", textMoney.ebBackGround, "RIGHT", -5, 0)
textMoney.eb:SetMultiLine(false)
textMoney.eb:SetAutoFocus(false) -- probar false
textMoney.eb:SetFontObject("ChatFontNormal")

AutoCompleteEditBox_SetAutoCompleteSource(textMoney.eb, GetAutoCompleteResults, AUTO_COMPLETE_ACCOUNT_CHARACTER, AUTOCOMPLETE_FLAG_NONE);

textMoney.eb:SetScript("OnEnterPressed", function (self,...)
    if ( AutoCompleteEditBox_OnEnterPressed(self) ) then
        --SendMailSubjectEditBox:SetFocus();
        self:ClearFocus()
    else
        --self:SetCursorPosition(0)
        self:ClearFocus()
    end
end)
textMoney.eb:SetScript("OnEscapePressed", function (self,...)
    AutoCompleteEditBox_OnEscapePressed(self)
    self:ClearFocus()
end)
textMoney.eb:SetScript("OnTextChanged", function (self,...)
    --print("changed",...,textLockedBox.eb:GetText())
    AutoCompleteEditBox_OnTextChanged(self,...)
    if textMoney.eb:GetText() == "" then
        textMoney:SetChecked(false)
    else
        textMoney:SetChecked(true)
    end
end)

local saveButton = ns.Utils.crearBoton(mailOptionsFrame,100,20,L["Save"],function () saveOptions() bgMOF:Hide() end)
saveButton:SetPoint("BOTTOM", bgMOF, "BOTTOM", -60, 30)

local cancelButton = ns.Utils.crearBoton(mailOptionsFrame,100,20,L["Cancel"],function () bgMOF:Hide() end)
cancelButton:SetPoint("LEFT", saveButton, "RIGHT", 10, 0)

function ns.setMailOpt(locked,inventory,money,uno,dos,tres)
    textLockedBox:SetChecked(locked)
    textInventory:SetChecked(inventory)
    textMoney:SetChecked(money)
    textLockedBox.eb:SetText(uno)
    textInventory.eb:SetText(dos)
    textMoney.eb:SetText(tres)
end

saveOptions = function ()
    AutoScrap_CharSettings.mailOptLockedBox = textLockedBox:GetChecked()
    AutoScrap_CharSettings.mailOptInventory = textInventory:GetChecked()
    AutoScrap_CharSettings.mailOptMoney = textMoney:GetChecked()
    AutoScrap_CharSettings.mailOptLockedBoxName = textLockedBox.eb:GetText()
    if AutoScrap_CharSettings.mailOptLockedBoxName == "" then
        AutoScrap_CharSettings.mailOptLockedBox = false
    end
    AutoScrap_CharSettings.mailOptInventoryName = textInventory.eb:GetText()
    if AutoScrap_CharSettings.mailOptInventoryName == "" then
        AutoScrap_CharSettings.mailOptInventory = false
    end
    AutoScrap_CharSettings.mailOptMoneyName = textMoney.eb:GetText()
    if AutoScrap_CharSettings.mailOptMoneyName == "" then
        AutoScrap_CharSettings.mailOptMoney = false
    end
    if AutoScrap_CharSettings.mailOptLockedBox == false then
        AutoScrap_CharSettings.mailOptLockedBoxName = ""
    end
    if AutoScrap_CharSettings.mailOptInventory == false then
        AutoScrap_CharSettings.mailOptInventoryName = ""
    end
    if AutoScrap_CharSettings.mailOptMoney == false then
        AutoScrap_CharSettings.mailOptMoneyName = ""
    end
end

local function crearBotonMailFrameOpt()
    local frame = CreateFrame ("Frame", nil, ASMailBoxFrame)
    frame:SetSize(26,157-122)
    frame.tex = frame:CreateTexture()
    frame.tex:SetTexture("Interface\\AddOns\\AutoScrap\\images\\mailpanel")
    frame.tex:SetTexCoord(0/512,26/512,122/512,158/512)
    frame.tex:SetAllPoints(frame)

    frame.enterTex = frame:CreateTexture()
    frame.enterTex:SetTexture("Interface\\AddOns\\AutoScrap\\images\\mailpanel")
    frame.enterTex:SetTexCoord(26/512,(26*2)/512,122/512,157/512)
    frame.enterTex:SetAllPoints(frame)
    frame.enterTex:Hide()

    frame.pressedTex = frame:CreateTexture()
    frame.pressedTex:SetTexture("Interface\\AddOns\\AutoScrap\\images\\mailpanel")
    frame.pressedTex:SetTexCoord((26*2)/512,(26*3)/512,122/512,157/512)
    frame.pressedTex:SetAllPoints(frame)
    frame.pressedTex:Hide()

    local pressed = false
    frame:SetScript("OnEnter",function (self,motion)
        --if motion then
            if pressed then
                frame.pressedTex:Show()
                frame.enterTex:Hide()
            else
            frame.enterTex:Show()
            frame.pressedTex:Hide()
            end
        --end
    end)
    frame:SetScript("OnLeave",function (self,motion)
        --if motion then
            frame.pressedTex:Hide()
            frame.enterTex:Hide()
        --end
    end)
    frame:SetScript("OnHide",function (self,...)
        frame.pressedTex:Hide()
        frame.enterTex:Hide()
        pressed = false
    end)
    frame:SetScript('OnMouseDown', function (self,button)
        if button=="LeftButton" then
            frame.pressedTex:Show()
            frame.enterTex:Hide()
            pressed = true
        end
    end)
    frame:SetScript('OnMouseUp', function (self,button)
        if button=="LeftButton" then
            frame.pressedTex:Hide()
            if self:IsMouseOver() then
                frame.enterTex:Show()
                if not bgMOF:IsShown() then
                    ns.setMailOpt(AutoScrap_CharSettings.mailOptLockedBox,AutoScrap_CharSettings.mailOptInventory,AutoScrap_CharSettings.mailOptMoney,AutoScrap_CharSettings.mailOptLockedBoxName,AutoScrap_CharSettings.mailOptInventoryName,AutoScrap_CharSettings.mailOptMoneyName)
                    bgMOF:Show()
                else
                    bgMOF:Hide()
                end
            else
                frame.enterTex:Hide()
            end
            pressed = false
        end
    end)

    return frame
end

local libro = crearBotonMailFrameOpt()
libro:SetPoint("RIGHT",ASMailBoxFrame,"RIGHT",-22,6)
libro:Show()