local logo_step=1
local logo_step_wait=0
local logo_anim={}
local step1_finished = false
local brillo_y = 10
local logo_wait = 500
local logo_sf = 99
local font_sf = 99
local logo_sf_w = 63
local logo_sf_h = 20

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_config(font_surface)
    font_sf = font_surface
end

function logo_init()
-- print("LOGO INIT")
    local font_default = font.current()
    -- Crear la surface
    logo_sf=surf.new(logo_sf_w,logo_sf_h)
    -- logo_sf=surf.new(256,192)
    -- Netejar el back
    surf.target(logo_sf)
    surf.cls(0)
    -- Font per a escriure
    font.current(font_sf)
    -- Logo
    draw.text("JAILGAMES",0,0,15)
    draw.text("presenta",0,10,14)
    -- draw.text("JAILGAMES",0,20,2)
    -- Restaurar font
    font.current(font_default)
    -- Inicialitzar animació
    logo_anim_init()
    -- flow:add_path({logo_animate, logo_end})
    
    -- print("LOGO_ANIMATE= ")
    -- print(logo_animate)
    -- print("LOGO_END= ")
    -- print(logo_end)
    
    -- Següent bucle
    -- game_update = logo_intro
    local modul, pas=states:next()
    print("LOGO_INIT= "..modul..", "..pas)
end

function logo_draw()
-- print("LOGO DRAW "..logo_step)
    -- Seleccionar font i destí
    surf.source(logo_sf)
    surf.target(0)
    -- Netejar destí
    surf.cls(16)
    
    -- Animació per a l'aparició del logo
    if logo_step==1 then
        -- Crear les barres
        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)
    end
    
    -- presenta
    if logo_step==2 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,10,55,10, 100,102,55,10); -- presenta
    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,10,55,10, 100,102,55,10); -- presenta
    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,10,55,10, 100,102,55,10); -- presenta

        -- if logo_step_wait>=1 then
            pal.subpal(15,2)
            draw.surf(0,0+math.floor(brillo_y),63,1,
                    logo_anim[1].x0, logo_anim[1].y+math.floor(brillo_y*2), logo_anim[1].w, 1);
            pal.subpal(15)
        -- end
    end
    
    -- Final
    if logo_step>=5 then
        pal.subpal(15,2)
        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,10,55,10, 100,102,55,10); -- presenta
        pal.subpal(15)
    end  
end

function logo_update()
-- print("LOGO UPDATE "..logo_step)
    -- Animació per a l'aparició del logo
    if logo_step==1 then
        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 logo_step = 3 end
    
    -- pausa
    if logo_step==3 then
        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
        logo_step_wait = logo_step_wait+1
        if logo_step_wait>=2 then
            logo_step_wait = 0
            brillo_y = brillo_y-0.5
            if brillo_y<0 then logo_step = 5 end
        end
    end
    
    -- Pausa final
    if logo_step==5 then
        logo_step = 6
        logo_wait = 125 
    end
    
    -- Final
    if logo_step==6 and logo_wait==0 then logo_step = 7 end
end

function logo_end()
-- print("LOGO_END")
    -- game_update = intro_init
    print("logo_end")
    states:executar("intro")
    -- surf.free(logo_sf)
end

function logo_animate()
-- print("LOGO_INTRO "..logo_step)
    logo_wait=logo_wait-1
    
    -- Següent bucle
    if logo_step==7 or controller:check("ESC") or controller:check("shoot") then
        local modul, pas=states:next()
        -- print("LOGO_ANIMATE 1= "..modul..", "..pas)
    end
    
    logo_draw()
    logo_update()
end

states:registrar("logo",{logo_init, logo_animate, logo_end})
