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


local FADE_IN_TIME = 1
local FADE_OUT_TIME = 0.3
local DEFAULT_DURATION = 10

local f = nil

local function savePosition(frame)
    AutoScrap_GlobalSettings.WowTokenFrame.startPoint, _ , AutoScrap_GlobalSettings.WowTokenFrame.relativePoint, AutoScrap_GlobalSettings.WowTokenFrame.xOfs, AutoScrap_GlobalSettings.WowTokenFrame.yOfs = frame:GetPoint()
end
local function setWowTokenPanelPosition()
    f:ClearAllPoints()
    f:SetPoint(AutoScrap_GlobalSettings.WowTokenFrame.startPoint,UIParent,AutoScrap_GlobalSettings.WowTokenFrame.relativePoint,AutoScrap_GlobalSettings.WowTokenFrame.xOfs,AutoScrap_GlobalSettings.WowTokenFrame.yOfs)
end
ns.setWowTokenPanelPosition=setWowTokenPanelPosition

local windowW, windowH = 209, 329-242
f = CreateFrame("Frame", "AutoScrapWowTokenPanel", UIParent)
f:SetSize(windowW, windowH)
f:SetPoint("TOPLEFT", 300, - 200)
f:SetAlpha(1)
f:Hide()
f:SetMovable(true)
f:EnableMouse(true)
f:RegisterForDrag("LeftButton")
f:SetScript("OnDragStart", f.StartMoving)
f:SetScript("OnDragStop", function() f:StopMovingOrSizing() savePosition(f) end)
f:SetClampedToScreen(true)

local tex = f:CreateTexture(nil, "BACKGROUND")
tex:SetAllPoints()
-- tex:SetTexCoord(0 / 512, 195 / 512, 160 / 512, 231 / 512)
tex:SetTexture("Interface\\AddOns\\AutoScrap\\images\\WowTokenPanel.blp")
tex:SetTexCoord(0 / 512, 209 / 512, 242 / 512, 329 / 512)


local text = f:CreateFontString(nil, "OVERLAY", "GameFontNormal")
text:SetFont(STANDARD_TEXT_FONT, 24, "")
--text:SetFont("Interface\\AddOns\\AutoScrap\\fonts\\MyFont.ttf", 26, "OUTLINE")
--text:SetFont("Fonts\\ArialN.TTF", 26, "")
text:SetPoint("LEFT", 50, 0)
text:SetPoint("RIGHT")
text:SetText("0")
text:SetTextColor(0 / 255, 177 / 255, 221 / 255, 1)
text:SetShadowOffset(2, - 2)
text:SetShadowColor(0 / 256, 10 / 256, 20 / 256, 1)

local nuevoPrecio = f:CreateFontString(nil, "OVERLAY", "GameFontNormal")
nuevoPrecio:SetFont(STANDARD_TEXT_FONT, 14, "")
--text:SetFont("Interface\\AddOns\\AutoScrap\\fonts\\MyFont.ttf", 26, "OUTLINE")
--text:SetFont("Fonts\\ArialN.TTF", 26, "")
nuevoPrecio:SetPoint("TOPLEFT", 70, - 15)
nuevoPrecio:SetText(L["WoW Token:"])
nuevoPrecio:SetTextColor(235 / 255, 224 / 255, 193 / 255, 1)

local optionsBtn = CreateFrame("Button", nil, f)
optionsBtn:SetSize(20, 20)
optionsBtn:SetPoint("BOTTOMRIGHT", - 18, 13)
optionsBtn:SetNormalTexture("Interface\\Buttons\\UI-OptionsButton")
optionsBtn:SetPushedTexture("Interface\\Buttons\\UI-OptionsButton")
optionsBtn:SetHighlightTexture("Interface\\Buttons\\UI-Common-MouseHilight", "ADD")
optionsBtn:SetMotionScriptsWhileDisabled(true)
optionsBtn:EnableMouse(true)

optionsBtn:SetScript("OnClick", function () ns.toggleWowTokenConfig() end)

local fadeIn = f:CreateAnimationGroup()
local aIn = fadeIn:CreateAnimation("Alpha")
aIn:SetFromAlpha(0)
aIn:SetToAlpha(1)
aIn:SetDuration(FADE_IN_TIME)
aIn:SetSmoothing("OUT")

-- local tIn = fadeIn:CreateAnimation("Translation")
-- tIn:SetOffset(30, 0)
-- tIn:SetDuration(TokenPanel.FADE_IN_TIME)
-- tIn:SetSmoothing("OUT")

local fadeOut = f:CreateAnimationGroup()
local aOut = fadeOut:CreateAnimation("Alpha")
aOut:SetFromAlpha(1)
aOut:SetToAlpha(0)
aOut:SetDuration(FADE_OUT_TIME)
aOut:SetSmoothing("IN")

aOut:SetScript("OnFinished", function()
    --f:SetAlpha(0)
    -- print("out finish")
    f:Hide()
end)

-- aIn:SetScript("OnPlay", function()
--     --f:SetAlpha(0)
--     print("in play")
-- end)

-- aIn:SetScript("OnStop", function()
--     --f:SetAlpha(0)
--     print("in stop")
-- end)

aIn:SetScript("OnFinished", function()
    --f:SetAlpha(0)
    -- print("in Finished")
    C_Timer.After(DEFAULT_DURATION, function() fadeOut:Play() end)
end)

-- aOut:SetScript("OnPlay", function()
--     --f:SetAlpha(0)
--     print("out play")
-- end)

-- aOut:SetScript("OnStop", function()
--     --f:SetAlpha(0)
--     print("out stop")
-- end)

--local elapsed = 0
--f:SetScript("OnUpdate", function(self, e) 
--elapsed = elapsed + e
-- if elapsed >  3 then
-- elapsed = 0
--fadeOut:Play()
--f:SetScript("OnUpdate", nil)
-- end        
--end)

--para testing en scrap debug AutoScrapWowTokenPanel.fin:Play()
f.fin = fadeIn
f.fout = fadeOut

--standart bags

local texBackground = ContainerFrameCombinedBags.TitleContainer:CreateTexture(nil, "BACKGROUND")
texBackground:SetPoint("TOPLEFT", 6 , 8)
texBackground:SetSize(100,15)
texBackground:SetTexture("Interface\\AddOns\\AutoScrap\\images\\WowTokenPanel")
texBackground:SetTexCoord(301 / 512, 401 / 512, 4 / 512, 19 / 512)
texBackground:Hide()

local texCoin = ContainerFrameCombinedBags.TitleContainer:CreateTexture(nil, "OVERLAY")
texCoin:SetPoint("TOPLEFT", 12, 5)
texCoin:SetSize(24,24)
texCoin:SetTexture("Interface\\AddOns\\AutoScrap\\images\\WowTokenPanel")
texCoin:SetTexCoord(238 / 512, 294 / 512, 23 / 512, 79 / 512)
texCoin:Hide()

local textCB = ContainerFrameCombinedBags.TitleContainer:CreateFontString(nil, "OVERLAY", "GameFontNormal")
textCB:SetFont(STANDARD_TEXT_FONT, 14, "")
textCB:SetPoint("LEFT", texCoin, "RIGHT", 1, 0)
textCB:SetText(543.456)
textCB:SetTextColor(30 / 255, 217 / 255, 255 / 255, 1)
textCB:Hide()

local coinBaganator= nil
local textBaganador= nil
local coinBaganatorCategorias= nil
local textBaganadorCategorias= nil
local coinBagnon= nil
local textBagnon= nil

function ns.crearWowTokenIconAndTextInOtherAddons()
    if not coinBaganator and not textBaganador and not coinBaganatorCategorias and not textBaganadorCategorias and not coinBagnon and not textBagnon then
        --baganator version bolsas combinadas:
        if Baganator_SingleViewBackpackViewFrameblizzard  and Baganator_SingleViewBackpackViewFrameblizzard.NineSlice then    
            local frame = Baganator_SingleViewBackpackViewFrameblizzard.NineSlice
            coinBaganator = frame:CreateTexture(nil, "OVERLAY")
            coinBaganator:SetPoint( "BOTTOMLEFT", 100 ,6)
            coinBaganator:SetSize(22, 22)
            coinBaganator:SetTexture("Interface\\AddOns\\AutoScrap\\images\\WowTokenPanel")
            coinBaganator:SetTexCoord(238 / 512, 294 / 512, 23 / 512, 79 / 512)
            coinBaganator:Hide()

            textBaganador = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            textBaganador:SetFont(STANDARD_TEXT_FONT, 14, "")
            textBaganador:SetPoint("LEFT", coinBaganator, "RIGHT", 2, 0)
            textBaganador:SetText(543.456)
            textBaganador:SetTextColor(30 / 255, 217 / 255, 255 / 255, 1)
            textBaganador:Hide()
        end

        --baganator version por categorías
        if Baganator_CategoryViewBackpackViewFrameblizzard  and Baganator_CategoryViewBackpackViewFrameblizzard.NineSlice then    
            local frame = Baganator_CategoryViewBackpackViewFrameblizzard.NineSlice
            coinBaganatorCategorias = frame:CreateTexture(nil, "OVERLAY")
            coinBaganatorCategorias:SetPoint( "BOTTOMLEFT", 100 ,6)
            coinBaganatorCategorias:SetSize(22, 22)
            coinBaganatorCategorias:SetTexture("Interface\\AddOns\\AutoScrap\\images\\WowTokenPanel")
            coinBaganatorCategorias:SetTexCoord(238 / 512, 294 / 512, 23 / 512, 79 / 512)
            coinBaganatorCategorias:Hide()

            textBaganadorCategorias = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            textBaganadorCategorias:SetFont(STANDARD_TEXT_FONT, 14, "")
            textBaganadorCategorias:SetPoint("LEFT", coinBaganatorCategorias, "RIGHT", 2, 0)
            textBaganadorCategorias:SetText(543.456)
            textBaganadorCategorias:SetTextColor(30 / 255, 217 / 255, 255 / 255, 1)
            textBaganadorCategorias:Hide()
            end

        --bagnon
        if BagnonInventory1 then    
            local frame = BagnonInventory1
            coinBagnon = frame:CreateTexture(nil, "OVERLAY")
            coinBagnon:SetPoint( "BOTTOMLEFT", 105 , 6)
            coinBagnon:SetSize(22, 22)
            coinBagnon:SetTexture("Interface\\AddOns\\AutoScrap\\images\\WowTokenPanel")
            coinBagnon:SetTexCoord(238 / 512, 294 / 512, 23 / 512, 79 / 512)
            coinBagnon:Hide()
            
            textBagnon = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            textBagnon:SetFont(STANDARD_TEXT_FONT, 14, "")
            textBagnon:SetPoint("LEFT", coinBagnon, "RIGHT", 2, 0)
            textBagnon:SetText(543.456)
            textBagnon:SetTextColor(30 / 255, 217 / 255, 255 / 255, 1)
            textBagnon:Hide()
        end
    end
end

local function showAddonsCoinsAndText(string)
    if coinBaganator then coinBaganator:Show() end
    if textBaganador then textBaganador:Show() textBaganador:SetText(string) end
    if coinBaganatorCategorias then coinBaganatorCategorias:Show() end
    if textBaganadorCategorias then textBaganadorCategorias:Show() textBaganadorCategorias:SetText(string) end
    if coinBagnon then coinBagnon:Show() end
    if textBagnon then textBagnon:Show() textBagnon:SetText(string) end
end

local function hideAddonsCoinsAndText()
    if coinBaganator then coinBaganator:Hide() end
    if textBaganador then textBaganador:Hide() end
    if coinBaganatorCategorias then coinBaganatorCategorias:Hide() end
    if textBaganadorCategorias then textBaganadorCategorias:Hide() end
    if coinBagnon then coinBagnon:Hide() end
    if textBagnon then textBagnon:Hide() end
end

function ns.mostrarWowTokenPanel(string)
    if string then
        text:SetText(ns.Utils.formatNumber(string))
    end
    --f:SetAlpha(1)
    if AutoScrap_GlobalSettings.WowTokenPriceWindowEnabled then
        f:Show()
        fadeIn:Play()
    end
end

function ns.actualizarPrecioWowTokenCombinedBags(string)
    if string then
        textCB:SetText(ns.Utils.formatNumber(string))
        texCoin:Show()
        textCB:Show()
        texBackground:Show()
        showAddonsCoinsAndText(ns.Utils.formatNumber(string))
    else
        texCoin:Hide()
        textCB:Hide()
        texBackground:Hide()
        hideAddonsCoinsAndText()
    end
    if not AutoScrap_GlobalSettings.WowTokenInCombinedBags then
        texCoin:Hide()
        textCB:Hide()
        texBackground:Hide()
        hideAddonsCoinsAndText()
    end
end

function ns.hideCombinedBagsWowTokenText()
    texCoin:Hide()
    textCB:Hide()
    texBackground:Hide()
    hideAddonsCoinsAndText()
end
