'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.passAnswer = passAnswer;
exports.fetchQuestionnaire = fetchQuestionnaire;
exports.resetQuestionnaire = resetQuestionnaire;
exports.goNextQuestion = goNextQuestion;

var _actionTypes = require('../constants/action-types');

var types = _interopRequireWildcard(_actionTypes);

var _index = require('../utils/index');

var _customError = require('../lib/custom-error');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function requestPassAnswer(qId, ans, nextQId, finished, showExplanation) {
  return {
    type: types.PASS_ANSWER_REQUEST,
    qId: qId,
    nextQId: nextQId,
    finished: finished,
    showExplanation: showExplanation,
    ans: ans
  };
}
function requestFetchQuestionnaire(questionnaireId) {
  return {
    type: types.FETCH_QUESTIONNAIRE_REQUEST,
    questionnaireId: questionnaireId
  };
}
function requestResetQuestionnaire(questionnaireId) {
  return {
    type: types.RESET_QUESTIONNAIRE_REQUEST,
    questionnaireId: questionnaireId
  };
}
function requestGoNextQuestion(nextQId, finished) {
  return {
    type: types.GO_NEXT_QUESTION_REQUEST,
    nextQId: nextQId,
    finished: finished
  };
}

function completePassAnswer(qId, ans, nextQId, finished, showExplanation) {
  return {
    type: types.PASS_ANSWER_SUCCESS,
    nextQId: nextQId,
    ans: ans,
    finished: finished,
    showExplanation: showExplanation,
    succAt: Date.now()
  };
}
function completeFetchQuestionnaire(questionnaireId, questSetting) {
  return {
    type: types.FETCH_QUESTIONNAIRE_SUCCESS,
    questionnaireId: questionnaireId,
    questSetting: questSetting,
    succAt: Date.now()
  };
}
function completeResetQuestionnaire(questionnaireId, qId) {
  return {
    type: types.RESET_QUESTIONNAIRE_SUCCESS,
    questionnaireId: questionnaireId,
    qId: qId,
    finished: false,
    succAt: Date.now()
  };
}
function completeGoNextQuestion(nextQId, finished) {
  return {
    type: types.GO_NEXT_QUESTION_SUCCESS,
    nextQId: nextQId,
    finished: finished,
    showExplanation: false,
    succAt: Date.now()
  };
}

function failToPassAnswer(error) {
  return {
    type: types.PASS_ANSWER_FAILURE,
    error: error,
    failedAt: Date.now()
  };
}
function failToFetchQuestionnaire(error) {
  return {
    type: types.FETCH_QUESTIONNAIRE_FAILURE,
    error: error,
    failedAt: Date.now()
  };
}
function failToResetQuestionnaire(error) {
  return {
    type: types.RESET_QUESTIONNAIRE_FAILURE,
    error: error,
    failedAt: Date.now()
  };
}
function failToGoNextQuestion(error) {
  return {
    type: types.GO_NEXT_QUESTION_FAILURE,
    error: error,
    failedAt: Date.now()
  };
}

function _passAnswer(qId, ans, nextQId, finished, showExplanation) {
  var tmpFinished = finished && finished === 'true' ? true : false;
  return qId && ans && finished || qId && ans && nextQId ? new Promise(function (resolve) {
    return resolve({
      qId: qId,
      ans: ans,
      finished: tmpFinished,
      showExplanation: showExplanation,
      nextQId: nextQId
    });
  }) : new Promise(function (resolve, reject) {
    return reject('[ qId, ans, nextQId ] got bad values supposed to be checked.');
  });
}
function _fetchQuestionnaire(url) {
  return fetch(url).then(function (response) {
    var status = response.status;
    if (status === 404) {
      throw new _customError.NotFoundError('Resource are not found by url: ', url);
    } else if (status >= 400) {
      throw new _customError.InternalServerError('Bad response from API, response: ' + JSON.stringify(response));
    }
    return response.json();
  });
}
function _resetQuestionnaire(questionnaireId, qId) {
  return questionnaireId && qId ? new Promise(function (resolve) {
    resolve({
      qId: qId,
      questionnaireId: questionnaireId
    });
  }) : new Promise(function (resolve, reject) {
    return reject('got a bad questionnaireId(' + questionnaireId + ') or qId(' + qId + ') supposed to be checked');
  });
}
function _goNextQuestion(nextQId, finished) {
  var tmpFinished = finished && finished === 'true' ? true : false;
  return finished || nextQId ? new Promise(function (resolve) {
    return resolve({
      finished: tmpFinished,
      nextQId: nextQId
    });
  }) : new Promise(function (resolve, reject) {
    return reject('[ nextQId, finished ] got bad values supposed to be checked.');
  });
}

function passAnswer(qId, ans, nextQId, finished) {
  var showExplanation = arguments.length <= 4 || arguments[4] === undefined ? false : arguments[4];

  return function (dispatch) {
    dispatch(requestPassAnswer(qId, ans, nextQId, finished, showExplanation));
    return _passAnswer(qId, ans, nextQId, finished, showExplanation).then(function (response) {
      dispatch(completePassAnswer(response.qId, response.ans, response.nextQId, response.finished, response.showExplanation));
    }, function (error) {
      return dispatch(failToPassAnswer(error));
    });
  };
}
function fetchQuestionnaire(questionnaireId) {
  var url = _buildQuestionnaireQueryUrl(questionnaireId);
  return function (dispatch) {
    dispatch(requestFetchQuestionnaire(questionnaireId));
    return _fetchQuestionnaire(url).then(function (response) {
      dispatch(completeFetchQuestionnaire(questionnaireId, response));
    }, function (error) {
      return dispatch(failToFetchQuestionnaire(error));
    });
  };
}
function resetQuestionnaire(questionnaireId, qId) {
  return function (dispatch) {
    dispatch(requestResetQuestionnaire(questionnaireId));
    return _resetQuestionnaire(questionnaireId, qId).then(function (response) {
      dispatch(completeResetQuestionnaire(response.questionnaireId, response.qId));
    }, function (error) {
      return dispatch(failToResetQuestionnaire(error));
    });
  };
}
function goNextQuestion(nextQId, finished) {
  return function (dispatch) {
    dispatch(requestGoNextQuestion(nextQId, finished));
    return _goNextQuestion(nextQId, finished).then(function (response) {
      dispatch(completeGoNextQuestion(response.nextQId, response.finished));
    }, function (error) {
      return dispatch(failToGoNextQuestion(error));
    });
  };
}

function _buildQuestionnaireQueryUrl(questionnaireId) {
  return (0, _index.formatUrl)('questionnaire/' + questionnaireId);
}