"use strict";

var Backbone = require('../backbone')
  , _ = require('underscore')
  , NoteSectionList = require('../collections/note_section')
  , RelatedTopicList = require('../collections/topic')

module.exports = Backbone.Model.extend({
  url: function() {
    // Same as EditorsNotes.Models.Document.url (ergo, same TODO as there)
    var origURL = Backbone.Model.prototype.url.call(this);
    return origURL.slice(-1) === '/' ? origURL : origURL + '/';
  },

  defaults: {
    'title': null,
    'content': null,
    'status': 'open',
    'section_ordering': [],
    'related_topics': []
  },

  initialize: function (options) {
    var that = this;

    // Same as in EditorsNotes.Models.Document.initialize (TODO)
    this.project = (this.collection && this.collection.project);
    if (!this.project) {
      throw new Error('Add notes through a project instance');
    }

    // Add a collection of NoteSection items to this note
    this.sections = new NoteSectionList([], {
      url: that.url(),
      project: this.project
    });

    this.related_topics = new RelatedTopicList([], {
      project: this.project
    });

    // Section ordering is a property of the Note, not the the individual
    // sections. So make them aware of that.
    this.sections.comparator = function (section) {
      var ordering = that.get('section_ordering');
      return ordering.indexOf(section.id);
    }
  },

  possibleStatuses: ['open', 'closed', 'hibernating'],

  parse: function (response) {
    var parsedNames = response.related_topics.map(function (t) { return t.name })
      , existingNames = this.related_topics.map(function (t) { return t.get('name') })
      , updateNames = !!_.difference(parsedNames, existingNames).length

    this.sections.set(response.sections);

    if (updateNames) {
      this.related_topics.set(response.related_topics);
      this.set('related_topics', parsedNames);
    }

    delete response.sections;
    delete response.related_topics;

    return response
  }
});

@@ -234,16 +234,89 @@
 xtend(%7B%0A
+  urlRoot: function () %7B%0A    return this.project.url() + 'notes/';%0A  %7D,%0A%0A
   url: f
@@ -690,16 +690,28 @@
 nction (
+attributes, 
 options)
@@ -818,16 +818,48 @@
 roject =
+ (options && options.project) %7C%7C
  (this.c
@@ -949,47 +949,83 @@
 or('
-Add notes through a project instance
+Notes must have a project set, either explicitly or through a collection.
 ')
-;
 %0A   

Allow note models to be created with a project in options