//
// Copyright (C) 2013 Instructure, Inc.
//
// This file is part of Canvas.
//
// Canvas is free software: you can redistribute it and/or modify it under
// the terms of the GNU Affero General Public License as published by the Free
// Software Foundation, version 3 of the License.
//
// Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.
//
import AssignmentGroupCollection from 'compiled/collections/AssignmentGroupCollection'
import Course from 'compiled/models/Course'
import AssignmentGroupListView from 'compiled/views/assignments/AssignmentGroupListView'
import CreateGroupView from 'compiled/views/assignments/CreateGroupView'
import IndexView from 'compiled/views/assignments/IndexView'
import AssignmentSettingsView from 'compiled/views/assignments/AssignmentSettingsView'
import AssignmentGroupWeightsView from 'compiled/views/assignments/AssignmentGroupWeightsView'
import ToggleShowByView from 'compiled/views/assignments/ToggleShowByView'
import _ from 'underscore'

const course = new Course()
course.url = ENV.URLS.course_url
course.fetch()

const includes = ['assignments', 'discussion_topic']
if (ENV.PERMISSIONS.manage) {
  includes.push('all_dates')
  includes.push('module_ids')
  // observers
} else if (ENV.current_user_has_been_observer_in_this_course) {
  includes.push('all_dates')
}

const userIsAdmin = _.contains(ENV.current_user_roles, 'admin')

const assignmentGroups = new AssignmentGroupCollection([], {
  course,
  params: {
    include: includes,
    exclude_response_fields: ['description', 'rubric'],
    override_assignment_dates: !ENV.PERMISSIONS.manage
  },
  courseSubmissionsURL: ENV.URLS.course_student_submissions_url
})

const assignmentGroupsView = new AssignmentGroupListView({
  collection: assignmentGroups,
  sortURL: ENV.URLS.sort_url,
  assignment_sort_base_url: ENV.URLS.assignment_sort_base_url,
  course,
  userIsAdmin
})

let assignmentSettingsView = false
let createGroupView = false
let showByView = false
const indexEl = window.location.href.indexOf('assignments') === -1 ? '#course_home_content' : '#content'

if (ENV.PERMISSIONS.manage_course) {
  assignmentSettingsView = new AssignmentSettingsView({
    model: course,
    assignmentGroups,
    weightsView: AssignmentGroupWeightsView,
    userIsAdmin
  })

  createGroupView = new CreateGroupView({
    assignmentGroups,
    course,
    userIsAdmin
  })
} else {
  showByView = new ToggleShowByView({
    course,
    assignmentGroups
  })
}

const app = new IndexView({
  el: indexEl,
  assignmentGroupsView,
  assignmentSettingsView,
  createGroupView,
  showByView,
  collection: assignmentGroups
})

app.render()

  // kick it all off
assignmentGroups.fetch({reset: true}).then(() => {
  if (ENV.HAS_GRADING_PERIODS) { app.filterResults() }
  if (ENV.PERMISSIONS.manage) {
    return assignmentGroups.loadModuleNames()
  }
  return assignmentGroups.getGrades()
})

@@ -1381,16 +1381,36 @@
 rse_url%0A
+const courseFetch = 
 course.f
@@ -2990,18 +2990,16 @@
 nder()%0A%0A
-  
 // kick 
@@ -3009,16 +3009,47 @@
 all off%0A
+Promise.all(%5B%0A  courseFetch,%0A  
 assignme
@@ -3077,16 +3077,19 @@
 : true%7D)
+%0A%5D)
 .then(()
@@ -3185,23 +3185,16 @@
 ) %7B%0A    
-return 
 assignme
@@ -3227,17 +3227,19 @@
 %0A  %7D
-%0A  return
+ else %7B%0A   
  ass
@@ -3264,11 +3264,15 @@
 rades()%0A
+  %7D%0A
 %7D)%0A

fix race_condition in assignment_index.js