MapChunk = class("MapChunk")

function MapChunk:new(col, row, cols, rows, cell_w, cell_h, rng)
	local chunk = instance(self)
	chunk.col_offset = (col-1)*cols
	chunk.row_offset = (row-1)*rows
	chunk.col = col
	chunk.row = row
	chunk.cols = cols
	chunk.rows = rows
	chunk.cell_w = cell_w
	chunk.cell_h = cell_h
	chunk.rng = rng	
	chunk.activated_at = 0
	chunk.active = false
	chunk.areas = {}
	chunk.ents_to_add = {}
	chunk.ents = {}
	chunk.updated_lights = {}
	return chunk
end

function MapChunk:set_tile(col, row, tile)
	local c,r = col-self.col_offset, row-self.row_offset
	local map = self.map
	if map:tileExists(c,r) then
		self.map:setTile(c,r, tile)
	end
end

function MapChunk:get_tile(col, row)
	local c,r = col-self.col_offset, row-self.row_offset
	local map = self.map
	if map and map:tileExists(c,r) then
		return map:getTile(c,r)
	end
	return "blocking"
end

function MapChunk:get_light_at(col, row)
	local c,r = col-self.col_offset, row-self.row_offset
	local map = self.map
	if map then
		return map:getLight(c,r)
	end
	return COLORS.err
end

function MapChunk:update_lights(dt)
	for light, _ in pairs(self.updated_lights) do
		if light:update(dt*5) then
			self.updated_lights[light] = nil
		end
	end
end

function MapChunk:adjust_light(col, row, color, factor)
	local c,r = col-self.col_offset, row-self.row_offset
	local map = self.map
	if map then--assert(map)	
		local light = map:adjust_light(c,r,color,factor)	
		self.updated_lights[light] = true
	end
end

function MapChunk:is_tile_used(col, row)
	local c,r = col-self.col_offset, row-self.row_offset
	local map = self.map
	if map then
		return map:getUsed(c,r)
	end
	return false
end

function MapChunk:is_tile_layedout(col, row)
	local c,r = col-self.col_offset, row-self.row_offset
	local map = self.map
	--assert(map)
	return map:getLayouted(c,r)		
end

function MapChunk:set_tile_layedout(col, row, v)
	local c,r = col-self.col_offset, row-self.row_offset
	local map = self.map
	--assert(map)
	return map:setLayouted(c,r,v)
end

function MapChunk:is_generated()
	return self.map ~= nil
end

function MapChunk:generate()
	local seed = self.seed	
	local map = TileMap:new(self.cols, self.rows, self.cell_w, self.cell_h)
	map:clear_with_tile("forest_grass")
	self.map = map
end

function MapChunk:set_populated(v)
	self.populated = v
end

function MapChunk:set_decorated(v)
	self.decorated = v
end

function MapChunk:add_ent_to_add(ent)
	ent.chunk = self
	table.insert(self.ents_to_add,ent)	
	self._ents_to_add = true	
end

function MapChunk:get_ent_at_tile(col, row)
	local cw = self.cell_w
	local ch = self.cell_h
	for ent, _ in pairs(self.ents) do
		local c = math.floor(ent.x/cw)
		local r = math.floor(ent.y/ch)
		if c == col and r == row then
			--print("Found", col, row, ent.x, ent.y, ent._index)
			return ent
		end
	end
	return nil
end

function MapChunk:add_ent(ent)
	ent.chunk = self
	self.ents[ent] = true	
end

function MapChunk:remove_ent(ent)
	ent.chunk = nil
	self.ents[ent] = nil
	for index, to_add in pairs(self.ents_to_add) do
		if to_add == ent then
			table.remove(self.ents_to_add, index)
			break
		end
	end
end

function MapChunk:clear_to_add()
	table.clear(self.ents_to_add)
	self._ents_to_add = false
end

function MapChunk:has_ents_to_add()
	return self._ents_to_add
end

function MapChunk:is_decorated()
	return self.decorated ~= nil
end

function MapChunk:is_populated()
	return self.populated ~= nil
end

function MapChunk:add_area(area)
	table.insert(self.areas, area)
end

function MapChunk:get_areas()
	return self.areas
end

function MapChunk:get_ents()
	return self.ents
end


function MapChunk:activate()
	self.active = true
end

function MapChunk:deactivate()	
	self.active = false
end

function MapChunk:is_active()	
	return self.active
end

function MapChunk:was_activated_at(stamp)
	return stamp <= self.activated_at
end
