 # Load landing page to retrieve the csrf token needed in login request.
 # More info on csrf -> https://en.wikipedia.org/wiki/Cross-site_request_forgery
 #
-# The sourceAlias input is also needed in login request, his goal is unknown
-#
 # The html parsing is done with cheerio, a "jquery like" server side
 retrieveTokens = (requiredFields, entries, data, next) ->
     log.info 'Retrieve Tokens'

            entries.tag = null
        else
            entries.tag = tag
        entries.listContacts.forEach (contact) ->
            queue.push contact, (err, finalContact) ->
                log.error err if err
                saveContact finalContact, entries



    request.get opts, (err, res, body) ->
        return next err if err
        if body.status? and body.status is 'error'
            return next new Error(body.status_details)
        entries.listContacts = body.contacts
        if not entries.listContacts?
            return next new Error("Error retrieving contacts from request")
        log.info 'List contact OK'
        next()


# Retrieve a list of all Cozy contacts, find all the contacts linked to the
# current Linkedin account an put them in a list by id
prepareCozyContacts = (requiredFields, entries, data, next) ->
    log.info 'Prepare Cozy contacts'

    Contact.all (err, contacts) ->
        return next err if err
        entries.cozyContacts = contacts
        entries.ofAccountByIds = {}
        for contact in contacts
            account = contact.getAccount ACCOUNT_TYPE, entries.accountName
            if account?
                entries.ofAccountByIds[account.id] = contact
        next()


# Create or retrieve a specific to Linkedin, then
#
# make a request for each contact retrieved in the previous request
# (retrieveListContact) with all data needed.
#
# In order to reduce the waiting betwen each request an async queue
# is create. It process the 10 first contacts. (see Async lib)
retrieveContacts = (requiredFields, entries, data, next) ->

    processRetrievingContactData = (contactId, done) ->
        contacts = """https://www.linkedin.com/contacts/api/contacts/\
        #{contactId}/?fields=name,first_name,last_name,\
        #emails_extended,phone_numbers,sites,addresses,\
        #company,title,location,profiles,twitter,\
        #display_sources&_=1444864592009"""

        opts =
            url: contacts
