local hasLyrics = false

local subtitlePadding = 15

local centered = false

function onCreatePost()
	for i = 0, getProperty('eventNotes.length')-1 do
		if getPropertyFromGroup('eventNotes', i, 'event') == 'New Lyric' then
            hasLyrics = true
            break
		end
	end

    if hasLyrics then
        makeLuaText("subtitle", "Hey", 0, 0, 0)
        setTextSize("subtitle", 28)
        setTextFont("subtitle", "LTCrow-Bold.ttf")
        setTextBorder("subtitle", 0)
        
        makeLuaSprite("subtitleBackdrop", "monster/subtitle", 0, 0)
        setScrollFactor("subtitleBackdrop", 0, 0)

        setProperty("subtitleBackdrop.alpha", 0.7)

        addLuaSprite("subtitleBackdrop", true)
        addLuaText("subtitle", true)

        setObjectCamera("subtitle", 'camHUD')
        setObjectCamera("subtitleBackdrop", 'camHUD')

        removeLyric()
    end
end

function onEvent(event, value1, value2, strumTime)
    if event == "New Lyric" then
        createLyric(value1)
    elseif event == "End Lyric" then
        removeLyric()
    elseif event == "Move Lyric" then
        centered = (tonumber(value1) == 1) or value1 == 'true'
    end
end

function createLyric(text)
    local yy = 0

    if centered then
        yy = 0.5
    else 
        if downscroll then
            yy = (1 - 0.86)
        else
            yy = 0.86
        end
    end

    setTextString("subtitle", text)

    setProperty("subtitle.visible", true)
    setProperty("subtitleBackdrop.visible", true)

    updateHitbox("subtitle")

    scaleObject("subtitleBackdrop", getProperty("subtitle.width") + subtitlePadding, getProperty("subtitle.height") + subtitlePadding)
    updateHitbox("subtitleBackdrop")

    setProperty("subtitle.x", screenWidth / 2)
    setProperty("subtitle.y", screenHeight * yy)

    setProperty('subtitle.x', getProperty("subtitle.x") - (getProperty("subtitle.width") / 2))
    setProperty('subtitle.y', getProperty("subtitle.y") - (getProperty("subtitle.height") / 2))

    setProperty("subtitleBackdrop.x", screenWidth / 2)
    setProperty("subtitleBackdrop.y", screenHeight * yy)

    setProperty('subtitleBackdrop.x', getProperty("subtitleBackdrop.x") - (getProperty("subtitleBackdrop.width") / 2))
    setProperty('subtitleBackdrop.y', getProperty("subtitleBackdrop.y") - (getProperty("subtitleBackdrop.height") / 2))

end

function removeLyric()
    setProperty("subtitle.visible", false)
    setProperty("subtitleBackdrop.visible", false)
end