local _, ns = ...
local point, relativePoint, xOfs, yOfs = nil,nil,nil,nil
local L = ns.L
function ns.windowGuildMovedTo(...)
    point, _, relativePoint, xOfs, yOfs = ...
end
function ns.getWindowGuildMovedTo()
    if xOfs == nil or yOfs == nil then
        return "CENTER", UIParent, "CENTER", 0, 0
    else
        return point, UIParent, relativePoint, xOfs, yOfs
    end
end

--local bankOutterFrame = CreateFrame("Frame",nil,BankFrame)
--bankOutterFrame:SetSize(504,-304,105-0)
--bankOutterFrame:SetPoint("CENTER",UIParent,"CENTER")

--bankOutterFrame:SetPoint("TOP",BankFrame,"TOP",0,0)

--local AutoScrapBank = ns.Utils.crearFrameConTextura (BankFrame,304,504,0,105,1,"mailpanel")
local framew, frameh, scale = 205,90, 1.4
local bgoutterframe = CreateFrame("Frame",nil, GuildBankFrame)
bgoutterframe:SetSize(framew,frameh)
bgoutterframe:SetPoint("TOPRIGHT",GuildBankFrame,"BOTTOMRIGHT",0,-9)
bgoutterframe:SetFrameStrata("HIGH");

local bg = CreateFrame("Frame",nil,bgoutterframe)
bg:SetSize((framew-15)*(1+scale),(frameh-15)*(1+scale))
bg:SetPoint("CENTER",bgoutterframe,"CENTER")
bg.tex =bg:CreateTexture(nil, "BACKGROUND")
bg.tex:SetAllPoints(bg)
bg.tex:SetHorizTile(true)
bg.tex:SetVertTile(true)
--bg.tex:SetTexture("Interface\\AddOns\\AutoScrap\\images\\backdrop\\woodbackground", "MIRROR", "MIRROR")
bg.tex:SetTexture("Interface\\AddOns\\AutoScrap\\images\\backdrop\\woodbackground", "REPEAT", "REPEAT")
bg:SetScale(100/(1+scale)/100)


local AutoScrapBank = CreateFrame("Frame", nil, bgoutterframe, "BackdropTemplate")
AutoScrapBank:SetPoint("CENTER",bgoutterframe,"CENTER")
AutoScrapBank:SetSize(framew, frameh    )
--dialog:SetClipsChildren(true)
AutoScrapBank:SetBackdrop({
   -- bgFile = "Interface\\AddOns\\AutoScrap\\images\\test",
    edgeFile = "Interface\\AddOns\\AutoScrap\\images\\backdrop\\backdrop",
    edgeSize = 32,
    tileEdge = true,
--tile = true, --background
--tileSize = 0 --background
    insets = { left = 4, right = 4, top = 4, bottom = 4 },
})

--AutoScrapBank:SetPoint("TOPLEFT",BankFrame,"TOPRIGHT",0,0)
--local AutoScrapBank = ns.Utils.crearFrameConTextura (bankOutterFrame,304,504,0,105,1,"mailpanel")
--AutoScrapBank:SetPoint("CENTER",bankOutterFrame,"CENTER",0,0)
AutoScrapBank:RegisterEvent("GUILDBANKFRAME_OPENED")
AutoScrapBank:RegisterEvent("PLAYER_INTERACTION_MANAGER_FRAME_SHOW")
AutoScrapBank:RegisterEvent("PLAYER_INTERACTION_MANAGER_FRAME_HIDE")
AutoScrapBank:RegisterEvent("GUILDBANKFRAME_CLOSED")
--AutoScrapBank:RegisterEvent("GUILDBANK_ITEM_LOCK_CHANGED") --muy útil para el futuro mantener la ventana abierta
AutoScrapBank:RegisterEvent("GUILDBANKBAGSLOTS_CHANGED") -- cuando mueves objetos

--AutoScrapBank:RegisterEvent("GUILDBANKLOG_UPDATE") -- click en registro o registro de dinero sale este evento
--AutoScrapBank:RegisterEvent("GUILDBANK_TEXT_CHANGED")
--AutoScrapBank:RegisterEvent("GUILDBANK_UPDATE_TEXT") -- cuando haces click en la pestaña de texto. pasa de argumento el número de pestaña
--AutoScrapBank:RegisterEvent("GUILDBANK_UPDATE_MONEY")
--AutoScrapBank:RegisterEvent("GUILDBANK_UPDATE_WITHDRAWMONEY")
AutoScrapBank:RegisterEvent("GUILDBANK_UPDATE_TABS") -- cuando compras tabs, o cambias tu icono o texto

------------------------
--[[ AutoScrapBank:RegisterEvent("GUILDBANKBAGSLOTS_CHANGED");
AutoScrapBank:RegisterEvent("GUILDBANK_ITEM_LOCK_CHANGED");
AutoScrapBank:RegisterEvent("GUILDBANK_UPDATE_TABS");
AutoScrapBank:RegisterEvent("GUILDBANK_UPDATE_MONEY");
AutoScrapBank:RegisterEvent("GUILDBANK_UPDATE_WITHDRAWMONEY");
AutoScrapBank:RegisterEvent("GUILD_ROSTER_UPDATE");
AutoScrapBank:RegisterEvent("GUILDBANKLOG_UPDATE");
AutoScrapBank:RegisterEvent("GUILDTABARD_UPDATE");
AutoScrapBank:RegisterEvent("GUILDBANK_UPDATE_TEXT");
AutoScrapBank:RegisterEvent("GUILDBANK_TEXT_CHANGED");
AutoScrapBank:RegisterEvent("PLAYER_MONEY");
AutoScrapBank:RegisterEvent("INVENTORY_SEARCH_UPDATE"); ]]
------------------------



local function mostrarFrame()
    --bankOutterFrame:Show()
    -----
    --[[ for b=GetNumGuildBankTabs(),1,-1 do
        QueryGuildBankTab(b)
    end
    QueryGuildBankTab(1) ]]
    -----
    --print("fired mostrarFrame")
    ns.abortMovingObjects()
    AutoScrapBank:RegisterEvent("BAG_UPDATE")
    bgoutterframe:Show()
    C_Timer.After(0, function ()ns.attachGuildBankFrameToBagOrBank()end)
end
ns.mostrarCartelGuild2botones = mostrarFrame


local function ocultarFrame()
    --bankOutterFrame:Hide()
    AutoScrapBank:UnregisterEvent("BAG_UPDATE")
    bgoutterframe:Hide()
end
ns.ocultarCartelGuild2botones = ocultarFrame

local checkTabs
local function bankHandler(self,event,...)
    --print(event,...)
    if event == "GUILDBANKFRAME_OPENED" or (event == "PLAYER_INTERACTION_MANAGER_FRAME_SHOW" and 10==...) then --en 10.0.2 el banko es el argumento 10 del nuevo manager frame show
        checkTabs()
        mostrarFrame()
    elseif event == "GUILDBANKFRAME_CLOSED" or (event == "PLAYER_INTERACTION_MANAGER_FRAME_HIDE" and 10==...)then
        ocultarFrame()
        ns.ocultarGuildFrame()
        ns.ocultarGuildToInvFrame()
        ns.abortMovingObjects()
    elseif event == "GUILDBANKBAGSLOTS_CHANGED" or event == "PLAYERBANKBAGSLOTS_CHANGED" or event == "GUILDBANK_UPDATE_TABS" or event == "BAG_UPDATE" then
        --print ("message:",GetCurrentGuildBankTab())
        --print ("message:",GetGuildBankTabInfo(GetCurrentGuildBankTab()))
        if not ns.isInventoryToGuildMovingObject() and not ns.isGuildToInventoryMovingObject() then
            ns.ocultarGuildFrame()
            ns.ocultarGuildToInvFrame()
        end
    end
end

function ns.refreshDataGuildBankFrame()
    if ns.isInvToGuildShown() then
        ns.refreshDataInvToGuild()
    elseif ns.isGuildToInvShown() then
        ns.refreshDataGuildToInv()
    end
end

AutoScrapBank:SetScript("OnEvent",bankHandler)

AutoScrapBank.spam=ns.Utils.crearFrameConTextura (AutoScrapBank,0,105,366,385,1,"mailpanel")
AutoScrapBank.spam:SetPoint("TOPLEFT",AutoScrapBank,"TOPLEFT",15    ,-15)
AutoScrapBank.spam:SetScale(0.70)

AutoScrapBank.button1 = ns.Utils.crearBoton(AutoScrapBank,165,22,L["Inventory to GuildBank"],function ()
    ns.toggleGuildFrame()
    ns.ocultarGuildToInvFrame()
end)
AutoScrapBank.button1:SetPoint("TOP",AutoScrapBank,"TOP",0,-28)

AutoScrapBank.button2 = ns.Utils.crearBoton(AutoScrapBank,165,22,L["GuildBank to Inventory"],function ()
    ns.toggleGuildToInvFrame()
    ns.ocultarGuildFrame()
end) --al añadir el togle del inventario recordar ponerlo también en ocultar cuando te alejas del banco en bank_close
AutoScrapBank.button2:SetPoint("TOP",AutoScrapBank.button1,"BOTTOM",0,-5)

AutoScrapBank.buyTabs=AutoScrapBank:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
AutoScrapBank.buyTabs:SetText(L["|cffff0000Buy some tabs!!|r"])
AutoScrapBank.buyTabs:SetPoint("CENTER",AutoScrapBank,"CENTER",0,0)
AutoScrapBank.buyTabs:Hide()


local mostrarBotones = true
checkTabs = function ()
    if GetNumGuildBankTabs()>0 then
        if mostrarBotones then
            AutoScrapBank.button1:Show()
            AutoScrapBank.button2:Show()
        end
        AutoScrapBank.buyTabs:Hide()
    else
        AutoScrapBank.button1:Hide()
        AutoScrapBank.button2:Hide()
        AutoScrapBank.buyTabs:Show()
    end
end

function ns.disableGuildWindow()
    mostrarBotones = false
    AutoScrapBank.button1:Hide()
    AutoScrapBank.button2:Hide()
    AutoScrapBank.mainTitle=AutoScrapBank:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    AutoScrapBank.mainTitle:SetText(L["|cffff0000No|r |cff00ff00TSM 4|r |cffff0000 found|r"])
    AutoScrapBank.mainTitle:SetPoint("CENTER",AutoScrapBank,"CENTER",0,0)
end

ocultarFrame()

function ns.attachGuildBankFrameToBagOrBank()
    --print("fired")
    if BagnonGuildFrame1 ~= nil then
        bgoutterframe:SetParent(BagnonGuildFrame1)
        bgoutterframe:ClearAllPoints()
        bgoutterframe:SetPoint("TOPLEFT",BagnonGuildFrame1,"TOPRIGHT",0,0)
    elseif GuildBankFrame:IsShown() == false or GuildBankFrame:GetParent()~= UIParent then
        --print("esto")
        bgoutterframe:SetParent(UIParent)
        bgoutterframe:ClearAllPoints()
        bgoutterframe:SetPoint("TOPLEFT",UIParent,"TOPLEFT",150,-150)
        --bgoutterframe:Show()
    else
        bgoutterframe:SetParent(GuildBankFrame)
        bgoutterframe:ClearAllPoints()
        bgoutterframe:SetPoint("TOPRIGHT",GuildBankFrame,"BOTTOMRIGHT",0,-9)
    end
end