 
   switch obj.condition
     when 'feedback'
-      '''TODO get user feedback and add to obj'''
       callback('Feedback condition not yet designed', obj)
     else
       callback(null, obj)

                  # We want to know which tasks have already be done max times
                  {$match:{count:{$gte:obj.hit.setsPerItem}}}
  ).exec((err, results) ->
      obj.maxed = (result._id for result in results)
      callback(obj, null)
  )

sampleTaskItems = (obj, callback) ->
  switch obj.condition
    when 'teaching'
      ''' TODO get teaching set '''
      callback(obj, "Teaching set not ready yet")
    when 'fast'
      ''' TODO Prep larger set for fast completion '''
      callback(obj, "Fast set not ready yet")
    when 'basic'
      ''' doStuff '''
      console.log obj.hit.items
      callback(obj, null)
      # Lock in-progress files

      # If there are no tasks left,
      # doStuff()
      # Find items which are *not* is set of items completed by user and
      # in set where HIT(CompletedItems).item.Count is less than hit.setsPerItem

#
mongoose = require 'mongoose'
async = require 'async'
_ = require 'underscore'
#mongoose.connect 'mongodb://localhost/test'
#mongoose.connect 'mongodb://localhost/test'
#db = mongoose.connection

TaskSet = require './models/taskset'
Hit = require './models/hit'
ObjectId = mongoose.Types.ObjectId

loadHIT = (opts) ->
  #db.on('error', console.error.bind(console, 'connection error:'))

  #db.once('open', () ->
    async.waterfall([
      (cb) -> cb(null, opts) # Pipe opts to first func in waterfal
      getBasicInfo,
      getCondition,
      getMaxedItems,
      sampleTaskItems
    ], (err, results) ->
      if (err) then return console.error err
      console.log "Waterfall done"
      console.log results
    )
  #)

# 1. Count how many taskSets this user has done in this condition
# Also fetch HIT info, since it can be done asynchronously
getBasicInfo = (opts, callback) ->
  async.parallel({
    opts: (callback) -> callback(null, opts)
    # Count of all taskSets done by the user
    countUserAll: (callback) -> TaskSet.countUserAll(opts.user, callback)
    # Count of tasksets in the given condition done
    countUserHIT: (callback) -> TaskSet.countUserHIT(opts.user, opts.hit_id, callback)
    # List of user's past items completed (not specific to the current hit)
    userItemList: (callback) -> TaskSet.userItemList(opts.user, callback)
    # Load HIT info
    hit: (callback) -> Hit.findOne({_id: ObjectId(opts.hit_id)}, callback)
  },
  callback
  )

