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

local piValue = 3.14
local sine = math.sin
local cosine = math.cos
local squareRoot = math.sqrt

local mainGroup
local gameGroup

local player

local backgroundPast, backgroundFuture
local hourHand, secondHand

local gameLoop, handleScreenTouches

local musicBackgroundStream, musicBackgroundChannel

local textGamePoints


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

    if ( player.tmr ) then
        timer.cancel( player.tmr )
        player.tmr = nil
    end

    if ( player.tmr2 ) then
        timer.cancel( player.tmr2 )
        player.tmr2 = nil
    end
end

local function checkForCollision()
   if ( player.degree >= 0 and player.degree < player.speed and not player.isMultiplierSet ) then
        player.multiplier = player.multiplier + 1
        player.isMultiplierSet = true
        player.degree = player.speed
    else
        player.isMultiplierSet = false
   end

   --if ( player.degree >= hourHand.rotation % 360 and player.degree <= secondHand.rotation % 360 ) then
   if ( player.degree + ( player.multiplier * 360 ) >= hourHand.rotation and player.degree + ( player.multiplier * 360 ) <= secondHand.rotation ) then
        --print "aradayız"
    elseif ( player.degree + ( player.multiplier * 360 ) >= secondHand.rotation ) then
        composer.gotoScene( "restartLevel" , "crossFade", 500 )
        --composer.setVariable( "playerPoints", tonumber(textGamePoints.text) )
        Runtime:removeEventListener( "enterFrame", gameLoop )
        Runtime:removeEventListener( "touch", handleScreenTouches)
    elseif ( player.degree + ( player.multiplier * 360 ) <= hourHand.rotation ) then
        composer.gotoScene( "restartLevel" , "crossFade", 500 )
        --composer.setVariable( "playerPoints", tonumber(textGamePoints.text) )
        Runtime:removeEventListener( "enterFrame", gameLoop )
        Runtime:removeEventListener( "touch", handleScreenTouches)
   end
end

gameLoop = function ()
    if ( player.speed < player.maxSpeed and player.speed > 0 ) then
        player.speed = player.speed + player.acceleration
    elseif ( player.speed > player.maxSpeed and player.speed < 0 ) then
        player.speed = player.speed + player.acceleration
    end

    player.degree = (player.degree + player.speed) % 360
    player.radian = (player.degree / 180) * piValue

    player.x = display.contentCenterX + (cosine(player.radian) * ((display.contentWidth / 1.5) / 2))
    player.y = display.contentCenterY + (sine(player.radian) * ((display.contentWidth / 1.5) / 2))

    textGamePoints.text = player.degree + (player.multiplier * 360) - 50

    if ( player.x > display.contentCenterX ) then
        if ( player.multiplier % 2 == 0 and backgroundPast.isVisible ) then
            backgroundPast.trans = transition.to( backgroundPast, {time = 200, alpha = 0} )
            backgroundFuture.trans = transition.to( backgroundFuture, {time = 200, alpha = 1} )
        elseif ( player.multiplier % 2 == 1 and backgroundFuture.isVisible ) then
            backgroundFuture.trans = transition.to( backgroundFuture, {time = 200, alpha = 0} )
            backgroundPast.trans = transition.to( backgroundPast, {time = 200, alpha = 1} )
        end

    end

    checkForCollision()

    --print (secondHand.rotation, hourHand.rotation, secondHand.valueRotation, hourHand.valueRotation)
    if ( secondHand.rotation - (hourHand.rotation + hourHand.valueRotation) > 30 and secondHand.rotation - (hourHand.rotation + hourHand.valueRotation) < 70 ) then
        hourHand.rotation = hourHand.rotation + hourHand.valueRotation
        secondHand.rotation = secondHand.rotation + secondHand.valueRotation
    else
        timer.cancel( player.tmr2 )
        hourHand.valueRotation = secondHand.valueRotation
    end
end

handleScreenTouches = function (event)
    if ( event.phase == "began" ) then
        if ( event.x < display.contentCenterX ) then
            player.speed = -player.maxSpeed
        elseif ( event.x > display.contentCenterX ) then
            player.speed = player.maxSpeed
        end
    elseif ( event.phase == "ended" ) then
        player.speed = 0
    end
    return true
end

local function loadGameAssets()
    backgroundPast = display.newImageRect( "assets/backgroundPast.jpg", 720, 1280 )
    backgroundPast.x = display.contentCenterX
    backgroundPast.y = display.contentCenterY
    backgroundPast.alpha = 0.01
    --backgroundPast.isVisible = false
    gameGroup:insert(backgroundPast)

    backgroundFuture = display.newImageRect( "assets/backgroundFuture.jpg", 720, 1280 )
    backgroundFuture.x = display.contentCenterX
    backgroundFuture.y = display.contentCenterY
    gameGroup:insert(backgroundFuture)

    hourHand = display.newImageRect("assets/hourHand.png", 287, 50)
    hourHand.x = display.contentCenterX
    hourHand.y = display.contentCenterY
    hourHand.rotation = 10
    hourHand.anchorX = 0
    hourHand.anchorY = 0.5
    gameGroup:insert(hourHand)

    secondHand = display.newImageRect("assets/secondHand.png", 310, 31)
    secondHand.x = display.contentCenterX
    secondHand.y = display.contentCenterY
    secondHand.rotation = 55
    secondHand.anchorX = 0
    secondHand.anchorY = 0.5
    gameGroup:insert(secondHand)

    hourHand.valueRotation = 1
    secondHand.valueRotation = 1
end

local function spawnPlayer()
    player = display.newImageRect( "assets/vi.png", 67, 99 )
    player.x = display.contentCenterX
    player.y = display.contentCenterY
    gameGroup:insert(player)

    player.speed = 0
    player.acceleration = 0.01

    player.maxSpeed = 5

    player.multiplier = 0

    player.degree = 50
    player.radian = 1


    if ( player.speed < player.maxSpeed and player.speed > 0 ) then
        player.speed = player.speed + player.acceleration
    elseif ( player.speed > player.maxSpeed and player.speed < 0 ) then
        player.speed = player.speed + player.acceleration
    end

    player.degree = (player.degree + player.speed) % 360
    player.radian = (player.degree / 180) * piValue

    player.x = display.contentCenterX + (cosine(player.radian) * ((display.contentWidth / 1.5) / 2))
    player.y = display.contentCenterY + (sine(player.radian) * ((display.contentWidth / 1.5) / 2))
end

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

    gameGroup = display.newGroup()

    loadGameAssets()
    spawnPlayer()

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


    mainGroup:insert(gameGroup)
end

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

    if ( phase == "will" ) then

    elseif ( phase == "did" ) then
        Runtime:addEventListener( "enterFrame", gameLoop )
        Runtime:addEventListener( "touch", handleScreenTouches)

        textGamePoints = display.newText( "0", 0, 0, native.systemFont, 50 )
        textGamePoints.x = display.contentCenterX
        textGamePoints.y = textGamePoints.height / 2
        textGamePoints:setFillColor( 0, 0, 0 )
        mainGroup:insert(textGamePoints)

        player.tmr = timer.performWithDelay( 5000, function ( )
            hourHand.defaultValueRotation = hourHand.valueRotation

            math.randomseed( os.time( ) )
            local randomValueRotation = math.random( 2 )

            --print ("random value", randomValueRotation)

            if ( randomValueRotation == 2 ) then
                hourHand.valueRotation = hourHand.valueRotation + 1
            else
                hourHand.valueRotation = hourHand.valueRotation - 1
            end

            player.tmr2 = timer.performWithDelay( 750, function () 
                hourHand.valueRotation = hourHand.defaultValueRotation 
                end, 1 )
         end, 0)
    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