import github from 'octonode'

function matchSprint (sprint, assignment) {
  if (assignment.split('-')[0] === 'p') {
    return true
  }
  const prefix = assignment.split('.')[0]
  const n = parseInt(prefix, 10)
  if (isNaN(n)) {
    return false
  }
  return sprint === n
}

function getAssignmentFile (assignment, next) {
  return new Promise((resolve, reject) => {
    client.repo('dev-academy-programme/curriculum-private')
      .contents(`${assignment}/README.md`, (err, file) => {
        if (err) {
          return reject(new Error(`Couldn't get assignment content for ${assignment}`))
        }
        return resolve(file)
      })
  })
}

function getAssignmentFiles (assignments) {
  const client = github.client(process.env['WTR_OAUTH_TOKEN']);
  return Promise.resolve(assignments)
  //return Promise.all([
  //])
}

function getAssignmentList (sprint) {
  const client = github.client(process.env['WTR_OAUTH_TOKEN']);

  return new Promise((resolve, reject) => {
    client.repo('dev-academy-programme/curriculum-private')
      .contents('assignments', (err, assignments) => {
        if (err) {
          return reject(new Error(`Couldn't get the assignments from the curriculum repo.`))
        }
        if (assignments.length === 0) {
          return reject(new Error(`No assignments found for that sprint.`))
        }

        const sprintAssignments = assignments
          .filter((assignment) => {
            return matchSprint(sprint, assignment.name)
          })
          .map((assignment) => {
            return assignment.path
          })

        return resolve(sprintAssignments)
      })
  })
}

function sortAndProcess (assignments) {
  return assignments
}

function getAssignments (sprint) {
  console.log(`Getting assignments...`)
  return getAssignmentList(sprint)
    .then(getAssignmentFiles)
    .then(sortAndProcess)
}

export default getAssignments

@@ -313,24 +313,89 @@
 nt, next) %7B%0A
+  const client = github.client(process.env%5B'WTR_OAUTH_TOKEN'%5D);%0A%0A
   return new
@@ -418,32 +418,32 @@
 e, reject) =%3E %7B%0A
-
     client.repo(
@@ -757,72 +757,8 @@
 ) %7B%0A
-  const client = github.client(process.env%5B'WTR_OAUTH_TOKEN'%5D);%0A
   re
@@ -822,24 +822,544 @@
 %5B%0A  //%5D)%0A%7D%0A%0A
+// Take an array of assignment.path and check to be sure it isn't all just%0A// 'p' assignments, which are generic to all sprints%0Afunction checkAssignments (assignments) %7B%0A  return new Promise((resolve, reject) =%3E %7B%0A    const numericOnly = assignments.reject((path) =%3E %7B%0A      const name = path.split('/').pop()%0A      return isNaN(parseInt(name%5B0%5D))%0A    %7D)%0A%0A    if (numericOnly.length === 0) %7B%0A      return reject(new Error(%60Couldn't find any assignments for that sprint.%60))%0A    %7D%0A    return resolve (assignments)%0A  %7D)%0A%7D%0A%0A
 function get
@@ -1826,31 +1826,28 @@
 s found 
-for
+in
  that 
-sprint
+repo
 .%60))%0A   
@@ -2285,16 +2285,16 @@
 ts...%60)%0A
-
   return
@@ -2320,16 +2320,44 @@
 sprint)%0A
+    .then(checkAssignments)%0A
     .the

Check assignments