score={
    bar_meter = nil
}

local gorro_img_x = 10*16 -- col
local gorro_img_y = 11*16 -- row
local peu_img_x   = 11*16
local peu_img_y   = 11*16
local clau_img_x  = 12*16
local clau_img_y  = 11.5*16

function score.avatar()
    local x = 64
    local y = 0
    local w = 13
    local h = 15
    draw.rectf( x, y, w, h, 15 ); -- fons negre
    draw.surf(211,128,w-2,h-2,x+1,y+1,w-2,h-2,true)
end

function score.border()
    draw.surf(32*o2aX,64*o2aX,8*o2aX,8*o2aX,0,0)       draw.surf(48*o2aX,72*o2aX,8*o2aX,8*o2aX,8*o2aX,0)       draw.surf(48*o2aX,72*o2aX,8*o2aX,8*o2aX,16*o2aX,0)       draw.surf(40*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,0)
    draw.surf(48*o2aX,64*o2aX,8*o2aX,8*o2aX,0,8*o2aX)  draw.surf(56*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,8*o2aX) draw.surf(48*o2aX,64*o2aX,8*o2aX,8*o2aX,0,16*o2aX)       draw.surf(56*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,16*o2aX)
    draw.surf(32*o2aX,64*o2aX,8*o2aX,8*o2aX,0,24*o2aX) draw.surf(48*o2aX,72*o2aX,8*o2aX,8*o2aX,8*o2aX,24*o2aX) draw.surf(48*o2aX,72*o2aX,8*o2aX,8*o2aX,16*o2aX,24*o2aX) draw.surf(40*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,24*o2aX)
    draw.surf(48*o2aX,64*o2aX,8*o2aX,8*o2aX,0,32*o2aX) draw.surf(56*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,32*o2aX)
    draw.surf(32*o2aX,64*o2aX,8*o2aX,8*o2aX,0,40*o2aX) draw.surf(48*o2aX,72*o2aX,8*o2aX,8*o2aX,8*o2aX,40*o2aX) draw.surf(48*o2aX,72*o2aX,8*o2aX,8*o2aX,16*o2aX,40*o2aX) draw.surf(40*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,40*o2aX)

    draw.surf(48*o2aX,64*o2aX,8*o2aX,8*o2aX,0,48*o2aX) draw.surf(56*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,48*o2aX) draw.surf(48*o2aX,64*o2aX,8*o2aX,8*o2aX,0,56*o2aX)       draw.surf(56*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,56*o2aX)
    draw.surf(48*o2aX,64*o2aX,8*o2aX,8*o2aX,0,64*o2aX) draw.surf(56*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,64*o2aX) --draw.surf(48*o2aX,64*o2aX,8*o2aX,8*o2aX,0,72*o2aX)     draw.surf(56*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,72*o2aX)
    draw.surf(32*o2aX,64*o2aX,8*o2aX,8*o2aX,0,72*o2aX) draw.surf(48*o2aX,72*o2aX,8*o2aX,8*o2aX,8*o2aX,72*o2aX)  draw.surf(48*o2aX,72*o2aX,8*o2aX,8*o2aX,16*o2aX,72*o2aX) draw.surf(40*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,72*o2aX)
    draw.surf(48*o2aX,64*o2aX,8*o2aX,8*o2aX,0,80*o2aX) draw.surf(56*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,80*o2aX) draw.surf(48*o2aX,64*o2aX,8*o2aX,8*o2aX,0,88*o2aX)       draw.surf(56*o2aX,64*o2aX,8*o2aX,8*o2aX,24*o2aX,88*o2aX)
end

function score.jailgames()
	arc_text("jail",6*o2aX,77*o2aX,2)
    arc_text("games",8*o2aX, 82*o2aX,2)
    arc_text("2026",9*o2aX,89*o2aX,1)
end

function score.life_meter()
    local x = 0
    local y = 2
    local w = 6
    local h = 7
    -- borde blau
    draw.hline(1+x, y, 8*w+4-2, 15) 
    draw.hline(1+x, y+h+3, 8*w+4-2, 15) 
    draw.vline(0+x, y+1, y+h+2, 15) 
    draw.vline(8*w+4-1, y+1, y+h+2, 15) 

    draw.rectf( 1+x, 1+y, 8*w+2, h+2, 16 ); -- fons negre
    
    if abad.energia>abad.max_energia/2 then
        -- 50%
        color = 10; -- verd
    elseif abad.energia>abad.max_energia/3 then
        -- 33%
        color = 8; -- groc
    else
        color = 3; -- roig
    end
    
    local full_width = 48
    local curr_width = math.floor(full_width*(abad.energia/abad.max_energia))
    local life_segment = math.floor(full_width/6)
    draw.rectf( 2+0+x+0*w, 2+y, full_width, h,  16 );
    draw.rectf( 2+0+x+0*w, 2+y, curr_width, h,  color );
    draw.vline( 2+0+x+0*w+life_segment*1, y+2, y+h+1, 16)
    draw.vline( 2+0+x+0*w+life_segment*2, y+2, y+h+1, 16)
    draw.vline( 2+0+x+0*w+life_segment*3, y+2, y+h+1, 16)
    draw.vline( 2+0+x+0*w+life_segment*4, y+2, y+h+1, 16)
    draw.vline( 2+0+x+0*w+life_segment*5, y+2, y+h+1, 16)
end

function score.create()
    score.surf=surf.new(arcade_config.resolucion.width,arcade_config.resolucion.height)
    surf.target(score.surf)
    surf.cls(0)
    
    score.avatar()
    score.bar_meter=bar_meter.new(0,2,51,10,1,1,abad)
    -- score.border()
    -- score.jailgames()
    
    -- score.life_meter()
    surf.target(0)
end

function score.draw()
    local x = 225
    local y = 12
    surf.target(score.surf)
    surf.cls(0)
    score.avatar()
    -- score.life_meter()
    score.bar_meter:draw()
    surf.target(0)
    
    surf.source(score.surf)
    draw.surf(0,0,arcade_config.resolucion.width,arcade_config.resolucion.height,170,10)
    surf.source(tiles)
    if abad.objects.gorro~=nil then 
        draw.surf(gorro_img_x,gorro_img_y,16,16,137,7) 
    end
    if abad.objects.peu~=nil   then
        local peu_offset = 0
        if abad.objects.clau_premiere~=nil then 
            peu_offset = 16
        end 
        draw.surf(peu_img_x,  peu_img_y,  16,16,121+peu_offset,7) 
    end
    -- if abad.objects.gps~=nil   then draw.surf(gps_img_x,  gps_img_y,  16,16,100*o2aX,55*o2aX) end

    if abad.objects.clau_premiere~=nil then draw.surf(clau_img_x,clau_img_y,8,8,153,13) end
    if abad.objects.clau_elalien~=nil then  draw.surf(clau_img_x+8,clau_img_y,8,8,161,13) end

    if abad.objects.bol~=nil then draw.surf(112*o2aX,88*o2aX,16*o2aX,8*o2aX,104*o2aX,46*o2aX) end
    
    arc_text(abad.vides,x,y,2)
    
    -- draw.rectf((102+(abad.energia>>1))*o2aX,30*o2aX,(21-(abad.energia>>1))*o2aX,8*o2aX,2)
end