"use strict";

import Immutable  from 'immutable';
import { Constants as AssessmentConstants }   from '../actions/assessment_progress';

const initialState = Immutable.fromJS({
  isSubmitted:           false,
  isStarted:             false,
  currentItemIndex:      0,

  // Number of 'check answer' api calls that have not yet returned
  numQuestionsChecking:  0,

  selectedAnswerId:      '',
  checkedResponses:      [],
  responses:             [],
  startedAt:             0,
  finishedAt:            0,
  assessmentResult:      null
});

export default (state = initialState, action) => {

  switch(action.type){
    case AssessmentConstants.ASSESSMENT_NEXT_QUESTIONS:
      var currentItemIndex = state.get("currentItemIndex");
      var increment = action.pageSize;
      state = state.set("currentItemIndex", currentItemIndex + increment);
      break;

    case AssessmentConstants.ASSESSMENT_PREVIOUS_QUESTIONS:
      var currentItemIndex = state.get("currentItemIndex");
      var decrement = action.pageSize;
      state = state.set("currentItemIndex", currentItemIndex - decrement);
      break;

    case AssessmentConstants.ASSESSMENT_VIEWED:
      state = state.set("startedAt", Date.now());
      break;

    case AssessmentConstants.ANSWER_SELECTED:
      var responses = state.getIn(['responses', `${action.questionIndex}`]);
      if(responses === undefined || action.exclusive === true){
        // Create new list if no list exists or multi answer is not allowed
        responses = Immutable.List();
      }

      var answerIndex = responses.indexOf(action.answerId);
      var shouldToggle = !action.exclusive;

      // Only add answer to responses array if it doesn't exist
      if(answerIndex > -1){
        // Don't toggle Radio buttons
        if(shouldToggle){responses = responses.delete(answerIndex);}

      } else {
        responses = responses.push(action.answerId);
      }

      state = state.setIn(["responses", `${action.questionIndex}`], responses);
      break;

    case AssessmentConstants.ASSESSMENT_CHECK_ANSWER_DONE:
      if(!action.error){
        var checkedResponses = Immutable.Map();

        // TODO: Currently we are setting the same response for all userInput.
        // When we have an example of multi answer feedback we should figure out
        // how to assign feedback to each answer.
        action.userInput.forEach((id) => {
          var feedback = Immutable.Map(action.payload);
          checkedResponses = checkedResponses.set(id,feedback);
        });

        state = state.setIn(
          ['checkedResponses', `${action.questionIndex}`],
          checkedResponses
        );
      }

      // Decrement number of questions being checked
      var checked = state.get('numQuestionsChecking');
      if(checked <= 0){
        throw "ASSESSMENT_CHECK_ANSWER_DONE dispatched when no answers were being checked";
      }
      state = state.set('numQuestionsChecking', checked - 1);
      break;

    case AssessmentConstants.CHECK_QUESTIONS:
      var checking = state.get('numQuestionsChecking');
      state = state.set('numQuestionsChecking', action.numQuestions + checking);

      break;

    case AssessmentConstants.ASSESSMENT_SUBMITTED_DONE:
      state = state.set("finishedAt", Date.now());
      state = state.set("isSubmitted", true);
      break;

    default:

  }
  return state;
};

@@ -531,17 +531,33 @@
     null
+,%0A  feedback: %5B%5D
 %0A
-
 %7D);%0A%0Aexp
@@ -2004,16 +2004,104 @@
 onses);%0A
+      state = state.setIn(%5B%22feedback%22, %60$%7Baction.questionIndex%7D%60%5D, %22%22); //TODO document%0A
       br
@@ -3369,24 +3369,24 @@
 ate.now());%0A
-
       state 
@@ -3427,24 +3427,189 @@
       break;
+%0A    case AssessmentConstants.INVALID_ANSWER_CHECK:%0A      state = state.setIn(%5B%22feedback%22, action.index%5D, action.feedback); //TODO use translated string%0A      break;
 %0A%0A    defaul

add question feedback to assessment progress reducer