require "sprites"

LAYER_FOREGROUND = 1
LAYER_BACKGROUND = 2
LAYER_ITEMS = 4
LAYER_SPRITES = 8
LAYER_SHADOWS = 16
LAYER_ALL = 31
ROOM_FILE_EXT = ".gif"

rooms = {
	surf_background = nil,
	surf_foreground = nil,
	surf_items = nil,
	surf_original_items = nil,
	visibility = LAYER_ALL,
	pos = {x=0, y=4*13},
}

function rooms.current()
	return (rooms.pos.x//20) + (rooms.pos.y//13) * 8
end

function rooms.convert(src)
	local dst = surf.new(20*8,13*8)
	surf.source(src)
	surf.target(dst)

	for ry=0,7 do
		for rx=0,7 do
			for ty=0,11 do
				for tx=0,19 do
					draw.pixel(rx*20+tx, ry*13+ty, draw.pixel(rx*20+tx, ry*12+ty))
				end
			end
		end
	end
	return dst
end

function rooms.reload()
	if rooms.surf_background ~= nil then surf.free(rooms.surf_background) end
	rooms.surf_background = surf.load("maps/rooms_background"..ROOM_FILE_EXT)
	--rooms.surf_background = surf.new(20*8,13*8)
	--rooms.surf_background = rooms.convert(surf.load("rooms_background"..ROOM_FILE_EXT))

	if rooms.surf_foreground ~= nil then surf.free(rooms.surf_foreground) end
	rooms.surf_foreground = surf.load("maps/rooms_foreground"..ROOM_FILE_EXT)
	--rooms.surf_foreground = surf.new(20*8,12*8)
	--rooms.surf_foreground = rooms.convert(surf.load("rooms_foreground"..ROOM_FILE_EXT))

	if rooms.surf_items ~= nil then surf.free(rooms.surf_items) end
	rooms.surf_items = surf.load("maps/rooms_items"..ROOM_FILE_EXT)
	--rooms.surf_items = surf.new(20*8,12*8)
	--rooms.surf_items = rooms.convert(surf.load("rooms_items"..ROOM_FILE_EXT))
end

function rooms.init()
	rooms.pos.x, rooms.pos.y = 0,4*13
	rooms.reload()
	sprites.init()
end

function rooms.save()
	local p = {}
	for i=0,255 do p[i] = {r=i,g=i,b=i} end
	surf.save(rooms.surf_background, "data/maps/rooms_background"..ROOM_FILE_EXT, p)
	surf.save(rooms.surf_foreground, "data/maps/rooms_foreground"..ROOM_FILE_EXT, p)
	surf.save(rooms.surf_items, "data/maps/rooms_items"..ROOM_FILE_EXT, p)
	editor.modified = false
end

function rooms.is_outside(x, y)
	return x < rooms.pos.x or y < rooms.pos.y or x > rooms.pos.x + 20 or y > rooms.pos.y + 13
end

function rooms.draw()
	-- Retallem la pantalla a la zona de joc
	--view.clip(0,8,160,96)

	-- Movem la càmara a l'habitació on estem
	view.origin(-rooms.pos.x*8,-rooms.pos.y*8)

	-- Pintem el background
	surf.source(surf_tiles)
	map.surf(rooms.surf_background)
	if rooms.is_visible(LAYER_BACKGROUND) then
		map.draw()
	else
		draw.rectf(0,0,160,96,1)
	end

	-- Movem 4x4 pixels la càmara per a pintar les sombres dels sprites i el foreground
	view.origin(-rooms.pos.x*8+4,-rooms.pos.y*8+4)

	-- Pintem el foreground de negre
	map.surf(rooms.surf_foreground)
	pal.subpal(1,32,1)
	if rooms.is_visible(LAYER_FOREGROUND | LAYER_SHADOWS) then map.draw() end

	-- Pintem els sprites de negre
	if rooms.is_visible(LAYER_SPRITES | LAYER_SHADOWS) then
		sprites.draw(true)
		--draw.surf(0, 0, 16, 17, 20, 15, 16, 17)
	end

	-- Movem la càmara al lloc que toca de nou, i tornem la paleta normal
	view.origin(-rooms.pos.x*8,-rooms.pos.y*8)
	pal.subpal()

	-- Pintem el foreground
	surf.source(surf_tiles)
	map.surf(rooms.surf_foreground)
	if rooms.is_visible(LAYER_FOREGROUND) then map.draw() end

	-- Pintem els sprites
	if rooms.is_visible(LAYER_SPRITES) then
		sprites.draw()
		--draw.surf(0, 0, 16, 17, 20, 15, 16, 17)
	end

	-- Pintem la rejilla
	--for y=0,12 do draw.line(0,y*8, 160, y*8, 27) end
	--for x=0,20 do draw.line(x*8, 0, x*8, 104, 27) end
end

function rooms.is_visible(layer)
	return (app.update==game.update) or (rooms.visibility & layer == layer)
end

function rooms.set_visibility(layer, visibility)
	if visibility then
		rooms.visibility = rooms.visibility | layer
	else
		rooms.visibility = rooms.visibility & ~layer
	end
end

function rooms.toggle_visibility(layer)
	if rooms.visibility & layer == layer then
		rooms.visibility = rooms.visibility & ~layer
	else
		rooms.visibility = rooms.visibility | layer
	end
end

function rooms.go_to_room(room)
	rooms.pos.x = (room % 8) * 20
	rooms.pos.y = (room // 8) * 13
end
