local logo_step=1
local logo_step_wait=0
local logo_anim={}
local step1_finished = false
local brillo_y = 10
local logo_wait = 400

function logo_anim_init ()
    logo_anim={ [1] = {x0=-126, y=80, w=126, h=4, speed= 1, accel= 0.25, x1=65, delay=0}, 
                [2] = {x0= 256, y=84, w=126, h=4, speed=-1, accel=-0.25, x1=65, delay=4}, 
                [3] = {x0=-126, y=88, w=126, h=4, speed= 1, accel= 0.25, x1=65, delay=8}, 
                [4] = {x0= 256, y=92, w=126, h=4, speed=-1, accel=-0.25, x1=65, delay=12}, 
                [5] = {x0=-126, y=96, w=126, h=4, speed= 1, accel= 0.25, x1=65, delay=16}}
end

function logo_init()
    game_update = logo_intro
    surf.cls(16)
	
    local font_default = font.current()
    font.current(font_sf)
    surf.target(back)
    surf.cls(16)
    draw.text("JAILGAMES",0,0,15)
    draw.text("presenta",0,20,14)
    draw.text("JAILGAMES",0,40,2)
    font.current(font_default)
    surf.target(0)
    logo_anim_init()
end

function logo_intro()
    logo_wait=logo_wait-1
    
    if logo_wait==0 or key.press(key.ESCAPE) or key.press(keyShoot) or pad.press(btnShoot) or pad.press(btnPause) then
        logo_wait=1
        game_update = logo_update
        fade.fadeoutin()
    end
    
    surf.source(back)
    surf.target(0)
    surf.cls(16)
    
    -- Animació per a l'aparició del logo
    if logo_step==1 then
        draw.surf(0,0,63,2, logo_anim[1].x0,logo_anim[1].y,logo_anim[1].w,logo_anim[1].h)
        draw.surf(0,2,63,2, logo_anim[2].x0,logo_anim[2].y,logo_anim[2].w,logo_anim[2].h)
        draw.surf(0,4,63,2, logo_anim[3].x0,logo_anim[3].y,logo_anim[3].w,logo_anim[3].h)
        draw.surf(0,6,63,2, logo_anim[4].x0,logo_anim[4].y,logo_anim[4].w,logo_anim[4].h)
        draw.surf(0,8,63,2, logo_anim[5].x0,logo_anim[5].y,logo_anim[5].w,logo_anim[5].h)
    
        step1_finished = true
        logo_step_wait = logo_step_wait+1
        if logo_step_wait>=1 then
            logo_step_wait = 0
            for i, data in ipairs(logo_anim) do
                local check = false
                if logo_anim[i].accel>=0 then
                    check = logo_anim[i].x0+logo_anim[i].speed<logo_anim[i].x1
                else
                    check = logo_anim[i].x0+logo_anim[i].speed>logo_anim[i].x1
                end 
                if check then
                    if logo_anim[i].delay==0 then
                        logo_anim[i].x0 = logo_anim[i].x0 + logo_anim[i].speed
                        logo_anim[i].speed = logo_anim[i].speed + logo_anim[i].accel
                    else
                        logo_anim[i].delay=logo_anim[i].delay-1
                    end
                    step1_finished = step1_finished and false
                else
                    logo_anim[i].x0 = logo_anim[i].x1
                    step1_finished = step1_finished and true
                end
            end
        end
        
        if step1_finished then logo_step=2 end
        
    end
    
    -- presenta
    if logo_step==2 then
        -- draw.surf(0,0, 63,10, 65,80,126,20); -- JAILGAMES
        draw.surf(0,0,63,10, logo_anim[1].x0,logo_anim[1].y,logo_anim[1].w,logo_anim[1].h*5); -- JAILGAMES
        draw.surf(0,20,55,10, 100,102,55,10); -- presenta
        logo_step = 3
    end
    
    -- pausa
    if logo_step==3 then
        draw.surf(0,0,63,10, logo_anim[1].x0,logo_anim[1].y,logo_anim[1].w,logo_anim[1].h*5); -- JAILGAMES
        draw.surf(0,20,55,10, 100,102,55,10); -- presenta
        logo_step_wait = logo_step_wait+1
        if logo_step_wait>=20 then
            logo_step_wait = 0
            logo_step = 4
        end
    end
    
    -- Brillo
    if logo_step==4 then
        draw.surf(0,0,63,10, logo_anim[1].x0,logo_anim[1].y,logo_anim[1].w,logo_anim[1].h*5); -- JAILGAMES
        draw.surf(0,20,55,10, 100,102,55,10); -- presenta
        
        logo_step_wait = logo_step_wait+1
        if logo_step_wait>=1 then
            logo_step_wait = 0
            draw.surf(0,40+math.floor(brillo_y),63,1,
                    logo_anim[1].x0, logo_anim[1].y+math.floor(brillo_y*2), logo_anim[1].w, 1);
            
            brillo_y = brillo_y-0.5
            
            if brillo_y==0 then logo_step = 5 end
        end
    end
    
    -- Final
    if logo_step>=5 then
        draw.surf(0,0,63,10, logo_anim[1].x0,logo_anim[1].y,logo_anim[1].w,logo_anim[1].h*5); -- JAILGAMES
        draw.surf(0,20,55,10, 100,102,55,10); -- presenta
        if logo_step==5 then logo_wait = 75 end
        logo_step = 6
    end
end

function logo_update()
	fps_print()
    intro_init()
end