local battleGeneral = require("scripts/battleGeneral")
local battleTimer = require("scripts/battleTimer")
local battleHUD = require("scripts/battleHUD")

local onlinePlay = require("scripts/onlinePlay")

local textplus = require("textplus")

local maskCollection,waveManager

local hud = {}


hud.phantoCounterOffsetY = 20
hud.phantoCounterTextOffsetY = 3

hud.phantoCounterIconImage = Graphics.loadImageResolved("maskCollection/resources/hud/phanto.png")
hud.phantoCounterIconGap = 4

hud.phantoCounterQuotaMetColor = Color.fromHexRGB(0xFF3869):lerp(Color.white,0.5)


hud.coinCounterOffsetY = 32
hud.coinCounterTextOffsetY = 3

hud.coinCounterIconImage = Graphics.loadImageResolved("maskCollection/resources/hud/coin.png")
hud.coinCounterIconGap = 4


hud.counterTextFormat = {font = textplus.loadFont("textplus/font/1.ini")}


hud.countdownTextFormat = {
    font = textplus.loadFont("resources/font/outlinedFont.ini"),
    align = "center",
    xscale = 2,
    yscale = 2,
}
hud.countdownTextLightColor = Color.black*0.5
hud.countdownTextDarkColor = Color.fromHexRGB(0xBFBFBF)
hud.countdownStartScale = 20
hud.countdownSettleScale = 6
hud.countdownFadeInTime = 8
hud.countdownFadeOutTime = 8
hud.countdownOpacity = 0.6


hud.phantoCounterBumpTimer = 0

hud.coinCounterBumpTimer = 0
hud.coinCounterOpacity = 0


local countdownTextShader = Shader.fromFile(nil,"maskCollection/resources/hud/countdownText.frag")

local textBuffer = Graphics.CaptureBuffer(192,96)


function hud.updatePhantoCounterLayout()
    hud.phantoCounterCurrentLayout = textplus.layout(tostring(waveManager.collectedMasks),nil,hud.counterTextFormat)
    hud.phantoCounterQuotaLayout = textplus.layout("/".. tostring(waveManager.maskQuota),nil,hud.counterTextFormat)

    hud.phantoCounterWidth = hud.phantoCounterCurrentLayout.width + hud.phantoCounterQuotaLayout.width + hud.phantoCounterIconImage.width + hud.phantoCounterIconGap
end

local function drawPhantoCounter(cam)
    local width,height = battleGeneral.getScreenSize()
    local x = math.floor((width - hud.phantoCounterWidth)*0.25)*2 - cam.renderX
    local y = battleTimer.boxOffsetY + battleTimer.boxHeight*0.5 + hud.phantoCounterOffsetY - cam.renderY

    local textColor = ((waveManager.collectedMasks >= waveManager.maskQuota and hud.phantoCounterQuotaMetColor) or Color.white)*battleHUD.opacity

    Graphics.drawImageWP(hud.phantoCounterIconImage,x,y - hud.phantoCounterIconImage.height,battleHUD.opacity,battleHUD.priority)
    x = x + hud.phantoCounterIconImage.width + hud.phantoCounterIconGap

    textplus.render{
        layout = hud.phantoCounterCurrentLayout,
        priority = battleHUD.priority,
        color = textColor,
        x = x,
        y = y - hud.phantoCounterCurrentLayout.height + hud.phantoCounterTextOffsetY - math.floor(math.sin(hud.phantoCounterBumpTimer*math.pi)*4 + 0.5),
    }
    x = x + hud.phantoCounterCurrentLayout.width

    textplus.render{
        layout = hud.phantoCounterQuotaLayout,
        priority = battleHUD.priority,
        color = textColor,
        x = x,
        y = y - hud.phantoCounterQuotaLayout.height + hud.phantoCounterTextOffsetY,
    }
end

local function drawCoinCounter(cam,playerIdx)
    local width,height = battleGeneral.getScreenSize()

    local playerSaveInfo = maskCollection.getPlayerSaveInfo(playerIdx)
    local coinCount = (playerSaveInfo ~= nil and playerSaveInfo.coins) or 0

    local layout = textplus.layout(tostring(coinCount),nil,hud.counterTextFormat)
    local counterWidth = layout.width + hud.coinCounterIconGap + hud.coinCounterIconImage.width

    local x = math.floor((cam.width - counterWidth)*0.25)*2
    local y = hud.coinCounterOffsetY

    Graphics.drawImageWP(hud.coinCounterIconImage,x,y - hud.coinCounterIconImage.height,hud.coinCounterOpacity,battleHUD.priority)
    x = x + hud.coinCounterIconImage.width + hud.coinCounterIconGap

    textplus.render{
        layout = layout,
        priority = battleHUD.priority,
        color = Color.white*hud.coinCounterOpacity,
        x = x,
        y = y - layout.height + hud.coinCounterTextOffsetY - math.floor(math.sin(hud.coinCounterBumpTimer*math.pi)*4 + 0.5),
    }
end


function hud.drawCountdown(layout,x,y,scale,opacity,left,right)
    textBuffer:clear(battleHUD.priority)

    textplus.render{
        layout = layout,
        target = textBuffer,
        priority = battleHUD.priority,
        x = 0,
        y = 0,
    }

    Graphics.drawBox{
        texture = textBuffer,priority = battleHUD.priority,
        color = Color.white.. opacity*hud.countdownOpacity,
        centred = true,

        x = x,
        y = y,
        width = layout.width*scale*0.5,
        height = layout.height*scale*0.5,
        sourceWidth = layout.width,
        sourceHeight = layout.height,

        shader = countdownTextShader,uniforms = {
            lightColor = hud.countdownTextLightColor,
            darkColor = hud.countdownTextDarkColor,

            left = x + layout.width*scale*((left or 0) - 0.5)*0.5,
            right = x + layout.width*scale*((right or 1) - 0.5)*0.5,
        },
    }
end


function hud.onCameraDraw(camIdx)
    if not maskCollection.activeOnClientside then
        return
    end

    local cam = Camera(camIdx)

    if battleHUD.opacity > 0 then
        drawPhantoCounter(cam)
    end

    if hud.coinCounterOpacity > 0 then
        if onlinePlay.currentMode ~= onlinePlay.MODE_OFFLINE then
            drawCoinCounter(cam,onlinePlay.playerIdx)
        else
            drawCoinCounter(cam,camIdx)
        end
    end
end

function hud.onTick()
    hud.phantoCounterBumpTimer = math.max(0,hud.phantoCounterBumpTimer - 1/12)
    hud.coinCounterBumpTimer = math.max(0,hud.coinCounterBumpTimer - 1/12)
end

function hud.onStart()
    hud.updatePhantoCounterLayout()

    battleHUD.opacity = 0
end


function hud.onInitAPI()
    registerEvent(hud,"onCameraDraw")
    registerEvent(hud,"onTick")
    registerEvent(hud,"onStart")

    maskCollection = require("maskCollection/scripts/maskCollection")
    waveManager = require("maskCollection/scripts/waveManager")
end


return hud