local char_C = {x=0, y=0, w=6, h=7}
local char_a = {x=6, y=0, w=4, h=5}
local char_c = {x=4, y=7, w=3, h=5}
local char_u = {x=12, y=5, w=4, h=5}
local char_s = {x=12, y=0, w=4, h=5}

local frame = { {x=0, y=7, w=2, h=1}, --cantó sup esq
                {x=2, y=7, w=1, h=1}, --linea sup
                {x=3, y=7, w=1, h=1}, -- cantó sup dre 1
                {x=3, y=9, w=1, h=1}, -- cantó sup dre 2
                {x=2, y=8, w=2, h=1}, -- costat dre 1
                {x=2, y=9, w=1, h=1}, -- costat dre 2
                {x=1, y=10, w=2, h=1}, -- costat dre 3
                {x=3, y=10, w=1, h=1}, -- cantó inf dre 1
                {x=3, y=11, w=1, h=1}, -- cantó inf dre 2
                {x=2, y=11, w=1, h=1}, -- linea inf
                {x=1, y=11, w=1, h=1}, -- cantó inf esq 1
                {x=0, y=8, w=1, h=4}, -- cantó inf esq 2 + costat esq 1
                {x=1, y=8, w=1, h=2}, -- costat esq 2
              }
local the = {x=7, y=8, w=5, h=2}
local arcade = {x=7, y=10, w=9, h=2}
local cycle_pal = {12, 13, 14}
local cycle_wait = 0
local cycle_step = 1

local menu_sel = 0

local rect_wait = 0


function title_init()
    title_sf=surf.load("title_tiles.gif")
    surf.source(title_sf)
    music.play(audio_main_song)
    states:next()
end

function title_draw_frame ()
    pal.subpal(9,cycle_pal[(cycle_step%3)+1])
    -- draw.surf( frame[1].x*8,  frame[1].y*8,  frame[1].w*8,  frame[1].h*8,  20, 58+4, frame[1].w*8, frame[1].h*8)
    -- draw.surf( frame[2].x*8,  frame[2].y*8,  frame[2].w*8,  frame[2].h*8,  36, 58+4, 24*frame[2].w*8, frame[2].h*8)
    -- draw.surf( frame[3].x*8,  frame[3].y*8,  frame[3].w*8,  frame[3].h*8, 228, 58+4, frame[3].w*8, frame[3].h*8)
    -- draw.surf( frame[4].x*8,  frame[4].y*8,  frame[4].w*8,  frame[4].h*8, 236, 58+4, frame[4].w*8, frame[4].h*8)
    -- draw.surf( frame[5].x*8,  frame[5].y*8,  frame[5].w*8,  frame[5].h*8, 228, 66+4, frame[5].w*8, frame[5].h*8)
    -- draw.surf( frame[6].x*8,  frame[6].y*8,  frame[6].w*8,  frame[6].h*8, 236, 74+4, frame[6].w*8, frame[6].h*8)
    -- draw.surf( frame[7].x*8,  frame[7].y*8,  frame[7].w*8,  frame[7].h*8, 236, 82+4, frame[7].w*8, frame[7].h*8)
    draw.surf( frame[8].x*8,  frame[8].y*8,  frame[8].w*8,  frame[8].h*8, 244, 90+4, frame[8].w*8, frame[8].h*8)
    draw.surf( frame[9].x*8,  frame[9].y*8,  frame[9].w*8,  frame[9].h*8, 236, 90+4, frame[9].w*8, frame[9].h*8)
    draw.surf(frame[10].x*8, frame[10].y*8, frame[10].w*8, frame[10].h*8,  28, 90+4, 26*frame[10].w*8, frame[10].h*8)
    draw.surf(frame[11].x*8, frame[11].y*8, frame[11].w*8, frame[11].h*8,  20, 90+4, frame[11].w*8, frame[11].h*8)
    draw.surf(frame[12].x*8, frame[12].y*8, frame[12].w*8, frame[12].h*8,  12, 66+4, frame[12].w*8, frame[12].h*8)
    -- draw.surf(frame[13].x*8, frame[13].y*8, frame[13].w*8, frame[13].h*8,  20, 66+4, frame[13].w*8, frame[13].h*8)
    pal.subpal(9,cycle_pal[((cycle_step+1)%3)+1])
    -- draw.surf( frame[1].x*8,  frame[1].y*8,  frame[1].w*8,  frame[1].h*8,  20, 58+2, frame[1].w*8, frame[1].h*8)
    -- draw.surf( frame[2].x*8,  frame[2].y*8,  frame[2].w*8,  frame[2].h*8,  36, 58+2, 24*frame[2].w*8, frame[2].h*8)
    -- draw.surf( frame[3].x*8,  frame[3].y*8,  frame[3].w*8,  frame[3].h*8, 228, 58+2, frame[3].w*8, frame[3].h*8)
    -- draw.surf( frame[4].x*8,  frame[4].y*8,  frame[4].w*8,  frame[4].h*8, 236, 58+2, frame[4].w*8, frame[4].h*8)
    -- draw.surf( frame[5].x*8,  frame[5].y*8,  frame[5].w*8,  frame[5].h*8, 228, 66+2, frame[5].w*8, frame[5].h*8)
    -- draw.surf( frame[6].x*8,  frame[6].y*8,  frame[6].w*8,  frame[6].h*8, 236, 74+2, frame[6].w*8, frame[6].h*8)
    -- draw.surf( frame[7].x*8,  frame[7].y*8,  frame[7].w*8,  frame[7].h*8, 236, 82+2, frame[7].w*8, frame[7].h*8)
    draw.surf( frame[8].x*8,  frame[8].y*8,  frame[8].w*8,  frame[8].h*8, 244, 90+2, frame[8].w*8, frame[8].h*8)
    draw.surf( frame[9].x*8,  frame[9].y*8,  frame[9].w*8,  frame[9].h*8, 236, 90+2, frame[9].w*8, frame[9].h*8)
    draw.surf(frame[10].x*8, frame[10].y*8, frame[10].w*8, frame[10].h*8,  28, 90+2, 26*frame[10].w*8, frame[10].h*8)
    draw.surf(frame[11].x*8, frame[11].y*8, frame[11].w*8, frame[11].h*8,  20, 90+2, frame[11].w*8, frame[11].h*8)
    draw.surf(frame[12].x*8, frame[12].y*8, frame[12].w*8, frame[12].h*8,  12, 66+2, frame[12].w*8, frame[12].h*8)
    draw.rectf(12, 80+2, frame[12].w*8, 8, 16)
    -- draw.surf(frame[13].x*8, frame[13].y*8, frame[13].w*8, frame[13].h*8,  20, 66+2, frame[13].w*8, frame[13].h*8)
    pal.subpal(9,cycle_pal[((cycle_step+2)%3)+1])
    draw.surf( frame[1].x*8,  frame[1].y*8, frame[1].w*8, frame[1].h*8, 20, 58, frame[1].w*8, frame[1].h*8)
    draw.surf( frame[2].x*8,  frame[2].y*8, frame[2].w*8, frame[2].h*8, 36, 58, 24*frame[2].w*8, frame[2].h*8)
    draw.surf( frame[3].x*8,  frame[3].y*8, frame[3].w*8, frame[3].h*8, 228, 58, frame[3].w*8, frame[3].h*8)
    draw.surf( frame[4].x*8,  frame[4].y*8, frame[4].w*8, frame[4].h*8, 236, 58, frame[4].w*8, frame[4].h*8)
    draw.surf( frame[5].x*8,  frame[5].y*8, frame[5].w*8, frame[5].h*8, 228, 66, frame[5].w*8, frame[5].h*8)
    draw.surf( frame[6].x*8,  frame[6].y*8, frame[6].w*8, frame[6].h*8, 236, 74, frame[6].w*8, frame[6].h*8)
    draw.surf( frame[7].x*8,  frame[7].y*8, frame[7].w*8, frame[7].h*8, 236, 82, frame[7].w*8, frame[7].h*8)
    draw.surf( frame[8].x*8,  frame[8].y*8, frame[8].w*8, frame[8].h*8, 244, 90, frame[8].w*8, frame[8].h*8)
    draw.surf( frame[9].x*8,  frame[9].y*8, frame[9].w*8, frame[9].h*8, 236, 90, frame[9].w*8, frame[9].h*8)
    draw.surf(frame[10].x*8, frame[10].y*8, frame[10].w*8, frame[10].h*8, 28, 90, 26*frame[10].w*8, frame[10].h*8)
    draw.surf(frame[11].x*8, frame[11].y*8, frame[11].w*8, frame[11].h*8, 20, 90, frame[11].w*8, frame[11].h*8)
    draw.surf(frame[12].x*8, frame[12].y*8, frame[12].w*8, frame[12].h*8, 12, 66, frame[12].w*8, frame[12].h*8)
    draw.surf(frame[13].x*8, frame[13].y*8, frame[13].w*8, frame[13].h*8, 20, 66, frame[13].w*8, frame[13].h*8)
    pal.subpal(9)
    
    cycle_wait = cycle_wait+1
    if cycle_wait==18 then
        cycle_wait = 0
        cycle_step = (cycle_step+1)%3+1
    end
end

function title_draw_tile( tile, x, y ) 
    draw.surf(tile.x*8,tile.y*8,tile.w*8,tile.h*8, x, y)
end

function title_draw_jg()
    draw.text("JAILGAMES 2026",150,174,13)
end

function title_draw ()
    title_draw_frame()
    title_draw_tile(char_C,30,13)    
    title_draw_tile(char_a,76,28)    
    title_draw_tile(char_c,107,28)    
    title_draw_tile(char_a,129,28)    
    title_draw_tile(char_u,158,29)    
    title_draw_tile(char_s,192,28)    
    title_draw_tile(the,129,76)    
    title_draw_tile(arcade,162,76)
    title_draw_jg()
end

function title_update()

end

function title_show()
    -- print("title_show")
    surf.source(title_sf)
    surf.target(0)
    surf.cls(16)

    title_menu_draw()
    title_draw()
    title_update()
    title_menu_update()
end

function title_end()
    music.stop()
    if menu_sel==0 then
        -- game_exit()
        -- game_init()
        -- fade.fadeoutin()
        to_game()
    elseif menu_sel==1 then
        -- menu_count=0
        -- menu_sel=0
        -- menu_state=update_options_menu--update_redefine_keys_menu;
        to_options()
    end
end

function title_menu_draw()
    draw_shiny_rect(56,120+(20*menu_sel)-4,146,18,14,15)
    arc_text("JUGAR",   108, 120, 14)
    arc_text("OPCIONS", 102, 140, 14)
end

function title_menu_update()
    if key.press(keyShoot) or key.press(key.SPACE) or pad.press(pad.A) or pad.press(btnShoot) or key.press(key.RETURN)  then
        title_end()
    elseif key.press(keyDown) or key.press(key.DOWN) or pad.press(pad.DOWN) or pad.press(btnDown) then
        menu_sel=menu_sel+1
        if menu_sel==2 then menu_sel=0 end
    elseif key.press(keyUp) or key.press(key.UP) or pad.press(pad.UP) or pad.press(btnUp) then
        menu_sel=menu_sel-1
        if menu_sel==-1 then menu_sel=1 end
    elseif key.press(key.ESCAPE) or pad.press(btnPause) then
        sys.quit()
    end
end

function to_game()
    print("to game")
    fade.fadeoutin()
    states:executar("game")
end

function to_options()
    print("to options")
    states:executar("opcions", true)
end

states:registrar("title", {title_init, title_show, title_end})
