'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.changeQuizTitle = changeQuizTitle;
exports.changeQuizInstructions = changeQuizInstructions;
exports.changeQuizShuffleQuestions = changeQuizShuffleQuestions;
exports.changeQuizRequireStudentAccessCode = changeQuizRequireStudentAccessCode;
exports.changeQuizStudentAccessCode = changeQuizStudentAccessCode;
exports.changeQuizHasTimeLimit = changeQuizHasTimeLimit;
exports.changeSessionTimeLimitInSeconds = changeSessionTimeLimitInSeconds;
exports.changeQuizOutcomeAlignmentSetGuid = changeQuizOutcomeAlignmentSetGuid;
exports.changeQuizAllowBacktracking = changeQuizAllowBacktracking;
exports.changeQuizOneAtATimeType = changeQuizOneAtATimeType;
exports.changeQuizCalculatorType = changeQuizCalculatorType;
exports.changeItemTitle = changeItemTitle;
exports.changeItemBody = changeItemBody;
exports.changeItemState = changeItemState;
exports.changeItemFeedback = changeItemFeedback;
exports.changeOutcomeAlignmentSetGuid = changeOutcomeAlignmentSetGuid;
exports.setNextQuizEntry = setNextQuizEntry;
exports.clearNextQuizEntry = clearNextQuizEntry;
exports.changeQuizEntryPoints = changeQuizEntryPoints;
exports.changeQuizEntryProperties = changeQuizEntryProperties;
exports.changeQuizEntryEntry = changeQuizEntryEntry;
exports.createItem = createItem;
exports.clearTemporaryItem = clearTemporaryItem;
exports.moveTemporaryQuizEntry = moveTemporaryQuizEntry;
exports.changeFudgePoints = changeFudgePoints;
exports.sessionItemResultsModification = sessionItemResultsModification;
exports.createBank = createBank;
exports.changeBank = changeBank;
exports.createBankItem = createBankItem;
exports.clearTemporaryBankItem = clearTemporaryBankItem;
exports.createStimulus = createStimulus;
exports.modifyStimulus = modifyStimulus;
exports.clearTemporaryStimulus = clearTemporaryStimulus;
exports.changeQuizFilterIpAddress = changeQuizFilterIpAddress;
exports.changeQuizIpFilters = changeQuizIpFilters;
exports.clearModifications = clearModifications;

var _constants = require('../../constants');

// =====================================================
// =====================================================

// These actions change 'modifications' in state, which
// are meant to represent any non-persisted alterations
// to state.

// example: when changing a editing a quiz. If you change
// the title, a modification is saved to state, until that
// modification is persisted, the "workingQuiz" will be
// the persisted quiz state (found in state.get('quizzes'))
// merged with these modifications.

// =====================================================
// =====================================================

// ==================
// ==================
//      QUIZZES
// ==================
// ==================

function changeQuizTitle(quizId, newTitle) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        title: newTitle
      }
    }
  };
}

function changeQuizInstructions(quizId, newInstructions) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        instructions: newInstructions
      }
    }
  };
}

function changeQuizShuffleQuestions(quizId, value) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        shuffleQuestions: Boolean(value)
      }
    }
  };
}

function changeQuizRequireStudentAccessCode(quizId, value) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        requireStudentAccessCode: Boolean(value)
      }
    }
  };
}

function changeQuizStudentAccessCode(quizId, value) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        studentAccessCode: value
      }
    }
  };
}

function changeQuizHasTimeLimit(quizId, value) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        hasTimeLimit: Boolean(value)
      }
    }
  };
}

function changeSessionTimeLimitInSeconds(quizId, value) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        sessionTimeLimitInSeconds: value
      }
    }
  };
}

function changeQuizOutcomeAlignmentSetGuid(quizId, newGuid) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        outcomeAlignmentSetGuid: newGuid
      }
    }
  };
}

function changeQuizAllowBacktracking(quizId, value) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        allowBacktracking: value
      }
    }
  };
}

function changeQuizOneAtATimeType(quizId, value) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        oneAtATimeType: value
      }
    }
  };
}

function changeQuizCalculatorType(quizId, value) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        calculatorType: value
      }
    }
  };
}

// ==================
// ==================
//       ITEMS
// ==================
// ==================

function changeItemTitle(itemId, newTitle) {
  return {
    type: _constants.ITEM_MODIFICATION,
    payload: {
      id: itemId,
      modifications: {
        title: newTitle
      }
    }
  };
}

function changeItemBody(itemId, newInstructions) {
  return {
    type: _constants.ITEM_MODIFICATION,
    payload: {
      id: itemId,
      modifications: {
        itemBody: newInstructions
      }
    }
  };
}

function changeItemState(itemId, modifications) {
  return {
    type: _constants.ITEM_MODIFICATION,
    payload: {
      id: itemId,
      modifications: modifications
    }
  };
}

function changeItemFeedback(itemId, newFeedback) {
  return {
    type: _constants.ITEM_MODIFICATION,
    payload: {
      id: itemId,
      modifications: {
        feedback: newFeedback
      }
    }
  };
}

function changeOutcomeAlignmentSetGuid(itemId, newGuid) {
  return {
    type: _constants.ITEM_MODIFICATION,
    payload: {
      id: itemId,
      modifications: {
        outcomeAlignmentSetGuid: newGuid
      }
    }
  };
}

// =====================
// =====================
//     QUIZZES ITEMS
// =====================
// =====================

function setNextQuizEntry(interactionTypeData, quizId, position, stimulusId, parentEntryId) {
  return {
    type: _constants.SET_NEXT_QUIZ_ENTRY,
    payload: { interactionTypeData: interactionTypeData, quizId: quizId, position: position, stimulusId: stimulusId, parentEntryId: parentEntryId }
  };
}

function clearNextQuizEntry() {
  return {
    type: _constants.CLEAR_NEXT_QUIZ_ENTRY,
    payload: {}
  };
}

function changeQuizEntryPoints(quizEntryId, newPoints) {
  return {
    type: _constants.QUIZ_ENTRIES_MODIFICATION,
    payload: {
      id: quizEntryId,
      modifications: {
        regradeAdjustedPointsPossible: null,
        pointsPossible: newPoints
      }
    }
  };
}

function changeQuizEntryProperties(quizEntryId, properties) {
  return {
    type: _constants.QUIZ_ENTRIES_MODIFICATION,
    payload: {
      id: quizEntryId,
      modifications: {
        properties: properties
      }
    }
  };
}

function changeQuizEntryEntry(quizEntryId, entryId) {
  var entryType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;

  var modifications = {
    entry: { id: entryId }
  };
  if (entryType) {
    modifications.entryType = entryType;
  }
  return {
    type: _constants.QUIZ_ENTRIES_MODIFICATION,
    payload: {
      id: quizEntryId,
      modifications: modifications
    }
  };
}

function createItem(quizId, position, interactionTypeData) {
  var parentEntryId = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
  var stimulusId = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null;

  return function (dispatch, getState) {
    dispatch({
      type: _constants.CREATE_TEMPORARY_ITEM,
      payload: {
        quizId: quizId,
        parentEntryId: parentEntryId,
        position: position,
        interactionTypeData: interactionTypeData,
        stimulusId: stimulusId
      }
    });
  };
}

// dont make this a needed arg
function clearTemporaryItem(quizId) {
  return function (dispatch, getState) {
    dispatch({
      type: _constants.CLEAR_TEMPORARY_ITEM,
      payload: { quizId: quizId }
    });
  };
}

function moveTemporaryQuizEntry(directionOrPosition, stimulusQuizEntryId, movingDown) {
  var payload = directionOrPosition === 'up' || directionOrPosition === 'down' ? { direction: directionOrPosition } : { position: directionOrPosition };
  payload.stimulusQuizEntryId = stimulusQuizEntryId;
  payload.movingDown = movingDown;
  return {
    type: _constants.MOVE_TEMPORARY_ITEM,
    payload: payload
  };
}

// =====================
// =====================
//  QUIZ SESSION RESULTS
// =====================
// =====================

function changeFudgePoints(quizSessionResultId, fudgePoints) {
  return {
    type: _constants.QUIZ_SESSION_RESULT_MODIFICATION,
    payload: {
      id: quizSessionResultId,
      modifications: {
        fudgePoints: fudgePoints
      }
    }
  };
}

// =====================
// =====================
//  SESSION ITEM RESULTS
// =====================
// =====================

function sessionItemResultsModification(sessionItemResultKey, modifications) {
  return {
    type: _constants.SESSION_ITEM_RESULTS_MODIFICATION,
    payload: {
      id: sessionItemResultKey,
      modifications: modifications
    }
  };
}

// =====================
// =====================
//  BANKS
// =====================
// =====================

function createBank() {
  return function (dispatch, getState) {
    dispatch({
      type: _constants.CREATE_TEMPORARY_BANK,
      payload: {}
    });
  };
}

// modifications = { name: 'Current Name' }
function changeBank(bankId, modifications) {
  return {
    type: _constants.BANK_MODIFICATION,
    payload: {
      id: bankId,
      modifications: modifications
    }
  };
}

function createBankItem(bankId, bankEntryType, interactionTypeData) {
  return function (dispatch, getState) {
    dispatch({
      type: _constants.CREATE_TEMPORARY_BANK_ITEM,
      payload: {
        bankId: bankId,
        interactionTypeData: interactionTypeData,
        bankEntryType: bankEntryType
      }
    });
  };
}

function clearTemporaryBankItem(bankId) {
  return function (dispatch, getState) {
    dispatch({
      type: _constants.CLEAR_TEMPORARY_BANK_ITEM,
      payload: { bankId: bankId }
    });
  };
}

// =====================
// =====================
//  STIMULI
// =====================
// =====================

function createStimulus(quizId, position) {
  return function (dispatch, getState) {
    dispatch({
      type: _constants.CREATE_TEMPORARY_STIMULUS,
      payload: {
        quizId: quizId,
        position: position
      }
    });
  };
}

function modifyStimulus(stimulus) {
  return function (dispatch, getState) {
    dispatch({
      type: _constants.STIMULUS_MODIFICATION,
      payload: { stimulus: stimulus }
    });
  };
}

function clearTemporaryStimulus(quizId) {
  return function (dispatch, getState) {
    dispatch({
      type: _constants.CLEAR_TEMPORARY_STIMULUS,
      payload: { quizId: quizId }
    });
  };
}

// =====================
// =====================
//     FILTERING
// =====================
// =====================

function changeQuizFilterIpAddress(quizId, value) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        filterIpAddress: Boolean(value)
      }
    }
  };
}

function changeQuizIpFilters(quizId, value) {
  return {
    type: _constants.QUIZ_MODIFICATION,
    payload: {
      id: quizId,
      modifications: {
        filters: {
          ips: value
        }
      }
    }
  };
}

// =====================
// =====================
//     CLEAR OBJECTS
// =====================
// =====================

function clearModifications(objectType, objectId) {
  var deepKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];

  return {
    type: _constants.CLEAR_MODIFICATIONS,
    payload: {
      objectType: objectType,
      objectId: objectId,
      deepKeys: deepKeys
    }
  };
}