 async = require 'async'
 requestFactory = require './requestfactory.js'
 request = requestFactory()
-
+ 
 requested_guid = {}
-
-Entity = GLOBAL.Entity = 
-    
-    counter: 
+ 
+Entity = GLOBAL.Entity =
+ 
+    counter:
         portals: 0
         fields:  0
         links:   0
-
+ 
     entityCount: 0
-
+ 
     add: (id, timestamp, data, callback) ->
-
+        data = remap data
         # update counter every 100 entities
-        
+ 
         main = ->
-
+ 
             Entity.entityCount++
-
+ 
             if data.type is 'portal'
                 createPortalEntity id, timestamp, data, callback
             else if data.type is 'region'
-                createFieldEntity id, timestamp, data, callback
+                #createFieldEntity id, timestamp, data, callback
+                return callback()
             else if data.type is 'edge'
-                createLinkEntity id, timestamp, data, callback
+                #createLinkEntity id, timestamp, data, callback
+                return callback()
             else
                 logger.warn "[Entity] Unknown entity type, id=#{id}, type=#{data.type}"
                 callback && callback()
-
-        
+ 
+ 
         if Entity.entityCount % 100 is 0
-
+ 
             async.parallel [
-
+ 
                 (callback) ->
-                    
+ 
                     Database.db.collection('Portals').count {}, (err, count) ->
                         Entity.counter.portals = count if not err
                         callback()
-
+ 
                 (callback) ->
-
+ 
                     Database.db.collection('Fields').count {}, (err, count) ->
                         Entity.counter.fields = count if not err
                         callback()
-
+ 
                 (callback) ->
-                    
+ 
                     Database.db.collection('Links').count {}, (err, count) ->
                         Entity.counter.links = count if not err
                         callback()
-
+ 
             ], main
-
+ 
         else
-        
+ 
             main()
-
+ 
     requestPortalDetail: (guid, outerCallback) ->
-
+ 
         # TODO: WTF?
         return outerCallback() if requested_guid[guid]?
-
+ 
         requested_guid[guid] = true
-
+ 
         t = 0
-
+ 
         request.push
-
+ 
             action: 'getPortalDetails'
             data:
                 guid: guid
             beforeRequest: (callback) ->
-
+ 
                 t = Date.now()
                 callback()
-
+ 
             onSuccess: (response, callback) ->
-
+                response = remap response.result
+ 
                 if response.capturedTime?
                     response.capturedTime = parseInt response.capturedTime
-
+ 
                 return callback() if ig.indexOf(response.owner) > -1
-
+ 
                 Database.db.collection('Portals').findAndModify
                     _id: guid   #query
                 ,

        ,
            _id: true
        ).toArray (err, portals) ->

            if err
                logger.error '[Details] Failed to fetch missing portal list: %s', err.message
                return callback()

            if portals
                async.each portals, (po, callback) ->
                    Entity.requestPortalDetail po._id, callback
                , callback
            else
                callback()

createEntity = (collection, id, timestamp, data, callback) ->
    
    data.time = timestamp

    Database.db.collection(collection).update
        _id: id
    ,

                ,
                    new: true   # options
                , (err, data) ->
 
                    if err
                        logger.error '[Details] Failed to update portal detail (guid=%s) in database: %s', guid, err.message
 
                    # resolve agent information
                    Agent.resolveFromPortalDetail data, callback
 
            onError: (err, callback) ->
 
                logger.error "[Details] #{err.message}"
                callback()
 
            afterResponse: (callback) ->
 
                logger.info "[Details] " +
                    Math.round(request.done / request.max * 100).toString() +
                    "%\t[#{request.done}/#{request.max}]\t#{Date.now() - t}ms"
 
                callback()
                outerCallback()
 
    requestMissingPortals: (callback) ->
 
        # request missing portal details
 
        Database.db.collection('Portals').find(
            team:
                $ne: 'NEUTRAL'
