local composer = require ("composer")
local scene = composer.newScene()

local widget = require ("widget")

local mainGroup

local tmr

local musicBackgroundStream, musicBackgroundChannel


local function cleanUp()
    if ( musicBackgroundStream ) then
        audio.stop()
        audio.dispose(musicBackgroundStream)
        musicBackgroundStream = nil
    end
end

local function onButtonRelease (event)
    if ( event.phase == "ended" ) then
        if ( event.target.id == "newGame" ) then
            composer.gotoScene( "introVideo", "crossFade", 1000 )
        elseif ( event.target.id == "credits" ) then
            composer.gotoScene( "creditScreen", "crossFade", 1000 )
        end
    end
end

function scene:create( event )
    local mainGroup = self.view

    local menubg = display.newImageRect( "assets/backgroundMenu.jpg", 720, 1280 )
    menubg.x = display.contentCenterX
    menubg.y = display.contentCenterY
    mainGroup:insert(menubg)

    local buttonNewGame = widget.newButton{
        id = "newGame",
        defaultFile = "assets/newGame.png",
        width = 372,
        height = 158,
        onEvent = onButtonRelease
    }
    buttonNewGame.x = 492
    buttonNewGame.y = 760
    mainGroup:insert(buttonNewGame)

    local buttonCredits = widget.newButton{
        id = "credits",
        defaultFile = "assets/credits.png",
        width = 250,
        height = 92,
        onEvent = onButtonRelease
    }
    buttonCredits.x = 510
    buttonCredits.y = 902
    mainGroup:insert(buttonCredits)

    musicBackgroundStream = audio.loadStream("soundFiles/tuktok.mp3")
    musicBackgroundChannel = audio.play(musicBackgroundStream, {loops = -1})
end

function scene:show( event )
    local phase = event.phase

    if ( phase == "will" ) then

    elseif ( phase == "did" ) then

    end
end

function scene:hide( event )
    local phase = event.phase

    if ( phase == "will" ) then
        cleanUp()
    elseif ( phase == "did" ) then

    end
end

function scene:destroy( event )

end

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

return scene