 RoomDefs = require './room_defs'
 Types = require './types'
 
-instanceRoom = (roomDef,r,c) ->
+instanceRoom = (roomDef,area,r,c) ->
   if roomDef?
     room = new Types.Room
       id: "room_r#{r}_c#{c}"
       roomDef: roomDef
+      area: area
       row:r
       col:c
       x: c * Config.roomWidthInPixels

  else
    null

# Convert a grid of room types into a grid of room data objects
mapLayoutToRoomGrid = (mapLayout, roomDefs, mapConfig) ->
  roomGrid = emptyGrid(mapLayout.rows, mapLayout.cols)
  for row,r in mapLayout.data
    for roomTypeId,c in row
      roomDef = roomDefs.get(roomTypeId)
      roomGrid[r][c] = instanceRoom(roomDef,r,c)

  roomGrid


    else
      null

expandWorldMap = (mapDef) ->
  roomGrid = mapDefToRoomGrid(mapDef, RoomDefs)
  tileGrid = roomGridToTileGrid(roomGrid)
  # areas = _.map layout.areas, (areaDef) -> makeArea(areaDef)
  # setRoomAreas(roomGrid,areas)
  new WorldMap
    roomGrid: roomGrid
    tileGrid: tileGrid

proto_mapDef =
  rows: 5
  cols: 15
  data: [
    # mainEntry-------------------                       |--|       roomA-----------------   roomB----------------------- 
    ['0x13-E',null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]
    ['0x08-A', '0x17-A', '0x09-A', '0x14-A', '0x13-A',  '0x18-B',  '0x12-C', '0x14-C', '0x19-C', '0x13-C',  '0x12-D', '0x14-D', '0x14-D', '0x14-D', '0x13-D']
  ]
  # areas: [
  #   ["mainEntry", [0,0], [0,4]]
  #   ["bridge", [0,5], [0,5]]
  #   ["roomA", [0,6], [0,9]]
  #   ["roomB", [0,10], [0,14]]
  # ]

module.exports =
  getDefaultWorldMap: FnUtils.memoizeThunk -> expandWorldMap(proto_mapDef)


window.WorldMap = module.exports.getDefaultWorldMap
