// this function is also present in assignment-edit-views.js, we need to consolidate using moment.js
function formatDate(value) {
  if(!Ember.none(value)) {
    return value.getFullYear() + "/" + (value.getMonth() + 1) + "/" + value.getDate();
  } else return null;
}


FLOW.QuestionAnswerView = Ember.View.extend({
  isTextType: false,
  isOptionType: false,
  isNumberType: false,
  isBarcodeType: false,
  isDateType: false,
  optionsList: [],
  content: null,
  optionChoice: null,
  inEditMode: false,
  isNotEditable: false,
  value: null,
  date: null,

  init: function() {
    this._super();
    this.doInit();
  },

  setInitialValue: function() {
    var opList, opListArray, i, sizeList, q, questionId, qaValue, choice, type,date;

    questionId = this.content.get('questionID');
    q = FLOW.store.find(FLOW.Question, questionId);

    // set value
    this.set('value', this.content.get('value'));
    if(this.get('isDateType') && !Ember.none(this.content.get('value'))) {
      date = new Date(parseInt(this.content.get('value'),10));
      this.set('date',formatDate(date));
    }
    // fill option list
    if(this.get('isOptionType') && q.get('optionList') !== null) {
      this.set('optionsList', []);
      opList = q.get('optionList');
      opListArray = opList.split('\n');
      sizeList = opListArray.length;

      for(i = 0; i < sizeList; i++) {
        this.get('optionsList').push(Ember.Object.create({
          isSelected: false,
          value: opListArray[i]
        }));
      }
      // set answer
      qaValue = this.content.get('value');
      this.get('optionsList').forEach(function(item) {
        if(item.get('value') == qaValue) {
          choice = item;
        }
      });
      this.set('optionChoice', choice);
    }
  },

  doInit: function() {
    var opList, opListArray, i, sizeList, q, questionId, qaValue, choice, type;

    // TODO use filter instead: if the question is not yet there, don't do anything
    // it will be picked up later at isLoaded.
    questionId = this.content.get('questionID');
    q = FLOW.store.find(FLOW.Question, questionId);
    type = q.get('type');
    this.set('isTextType', type == 'FREE_TEXT');
    this.set('isOptionType', type == 'OPTION');
    this.set('isNumberType', type == 'NUMBER');
    this.set('isBarcodeType', type == 'BARCODE');
    this.set('isDateType', type == 'DATE');
    this.set('isNotEditable', (type == 'GEO' || type == 'PHOTO' || type == 'VIDEO'));

    this.setInitialValue();
  }.observes('FLOW.questionControl.content.isLoaded'),

  doEdit: function() {
    this.set('inEditMode', true);
  },

  doCancel: function() {
    // revert answer
    this.setInitialValue();
    this.set('inEditMode', false);
  },

  doSave: function() {
    if(this.get('isDateType')){
      this.content.set('value',Date.parse(this.get('date')));
    } else if (this.get('isOptionType')){
      this.content.set('value',this.optionChoice.get('value'));
    } else {
      this.content.set('value',this.get('value'));
    }
    FLOW.store.commit();
    this.set('inEditMode', false);
  }

});
@@ -539,16 +539,36 @@
 : null,%0A
+  numberValue:null,%0A
   date: 
@@ -573,16 +573,16 @@
 : null,%0A
-
 %0A  init:
@@ -639,16 +639,787 @@
 ;%0A  %7D,%0A%0A
+doInit: function() %7B%0A    var opList, opListArray, i, sizeList, q, questionId, qaValue, choice, type;%0A%0A    // TODO use filter instead: if the question is not yet there, don't do anything%0A    // it will be picked up later at isLoaded.%0A    questionId = this.content.get('questionID');%0A    q = FLOW.store.find(FLOW.Question, questionId);%0A    type = q.get('type');%0A    this.set('isTextType', type == 'FREE_TEXT');%0A    this.set('isOptionType', type == 'OPTION');%0A    this.set('isNumberType', type == 'NUMBER');%0A    this.set('isBarcodeType', type == 'BARCODE');%0A    this.set('isDateType', type == 'DATE');%0A    this.set('isNotEditable', (type == 'GEO' %7C%7C type == 'PHOTO' %7C%7C type == 'VIDEO'));%0A%0A    this.setInitialValue();%0A  %7D.observes('FLOW.questionControl.content.isLoaded'),%0A%0A%0A
   setIni
@@ -1692,24 +1692,128 @@
 t('value'));
+%0A%0A    if(this.get('isNumberType')) %7B%0A      this.set('numberValue',this.content.get('value'));%0A    %7D%0A    
 %0A    if(this
@@ -2667,780 +2667,8 @@
 %7D,%0A%0A
-  doInit: function() %7B%0A    var opList, opListArray, i, sizeList, q, questionId, qaValue, choice, type;%0A%0A    // TODO use filter instead: if the question is not yet there, don't do anything%0A    // it will be picked up later at isLoaded.%0A    questionId = this.content.get('questionID');%0A    q = FLOW.store.find(FLOW.Question, questionId);%0A    type = q.get('type');%0A    this.set('isTextType', type == 'FREE_TEXT');%0A    this.set('isOptionType', type == 'OPTION');%0A    this.set('isNumberType', type == 'NUMBER');%0A    this.set('isBarcodeType', type == 'BARCODE');%0A    this.set('isDateType', type == 'DATE');%0A    this.set('isNotEditable', (type == 'GEO' %7C%7C type == 'PHOTO' %7C%7C type == 'VIDEO'));%0A%0A    this.setInitialValue();%0A  %7D.observes('FLOW.questionControl.content.isLoaded'),%0A%0A
   do
@@ -3060,24 +3060,123 @@
 ('value'));%0A
+    %7D else if (this.get('isNumberType'))%7B%0A      this.content.set('value',this.get('numberValue'));%0A
     %7D else %7B
@@ -3293,16 +3293,261 @@
 false);%0A
-
   %7D
+,%0A%0A  doValidateNumber: function () %7B%0A    // TODO should check for minus sign and decimal point, depending on question setting%0A    this.set('numberValue',this.get('numberValue').toString().replace(/%5B%5E%5Cd.%5D/g, %22%22));%0A  %7D.observes('this.numberValue')
 %0A%0A%7D);

validate number in question answer view