import {VLCommonConstants} from 'constants/VLCommon';

let defaultAttackState = {
  levels : [],
  totalLevels : 0,
  currentLevel : 1,
  hints : [],
  currentLevelDetails : {},
  currentLevelSubmissionDetails : {},
  vlTypes : []
};

export function addLockedStateToLevels (levels) {
  let updatedLevels = levels.map(function (level, index) {
    level.isLocked = (index !== 0);
    return level;
  });
  return updatedLevels;
}

export function attackReducer (state = defaultAttackState, action) {
  switch (action.type) {
     case VLCommonConstants.SET_VLLEVELS:
      return Object.assign(
        {},
        state,
        {
          levels : addLockedStateToLevels(action.vlLevels),
          totalLevels : action.vlLevels.length,
          currentLevelDetails : action.vlLevels[state.currentLevel - 1]
        }
      );
    case VLCommonConstants.SET_VLTYPES:
      return Object.assign(
        {},
        state,
        {
          vlTypes : action.vlTypes
        }
      );
    case VLCommonConstants.SET_LEVELS:
      return Object.assign(
        {},
        state,
        {
          levels : addLockedStateToLevels(action.levels),
          totalLevels :  action.totalLevels,
          currentLevel :  action.currentLevel
        }
      );
    case VLCommonConstants.SET_HINTS:
      let currentLevelSubmissionDetails = state.currentLevelSubmissionDetails;
      currentLevelSubmissionDetails.metaData = action.metaData;
      let currentLevelDetails = state.currentLevelDetails;
      currentLevelDetails.instructions = action.instructions
      return Object.assign(
        {},
        state,
        {
          hints: action.hints,
          currentLevelDetails : currentLevelDetails,
          currentLevelSubmissionDetails
        }
      );
    case VLCommonConstants.SET_QUESTION:
      return Object.assign(
        {},
        state,
        {
          question: action.question
        }
      );
    case VLCommonConstants.SET_CURRENT_LEVEL_SUBMISSION_DETAILS:
      let metaData = state.currentLevelSubmissionDetails.metaData
      currentLevelSubmissionDetails = action.currentLevelSubmissionDetails
      currentLevelSubmissionDetails.metaData = metaData
      return Object.assign(
        {},
        state,
        {
          currentLevelSubmissionDetails : currentLevelSubmissionDetails
        }
      );
    case VLCommonConstants.LEVEL_COMPLETED:
      let nextLevelDetails;
      if (state.currentLevel !== state.levels.length) {
        try {
          nextLevelDetails = state.levels[state.currentLevel]
        } catch (e) {
          nextLevelDetails = state.levels[state.currentLevel-1]
        }
        nextLevelDetails.isLocked = false;
        let first = state.levels.slice(0,state.currentLevel);
        let last = state.levels.slice(state.currentLevel+1, state.levels.length);
        first.push(nextLevelDetails);
        return Object.assign(
          {},
          state,
          {
            levels : first.concat(last)
          }
        );
      } else {
        return Object.assign(
          {},
          state,
          {
            currentLevel : state.currentLevel
          }
        );
      }

    case VLCommonConstants.GO_TO_LEVEL:
      currentLevelSubmissionDetails = state.currentLevelSubmissionDetails;
      currentLevelSubmissionDetails.currentSubmissionStatus = 'NOT_COMPLETED';
      currentLevelSubmissionDetails.resultantQuery = [];
      currentLevelDetails = state.currentLevelDetails;
      currentLevelDetails = Object.assign({}, currentLevelDetails, state.levels[action.level-1])
      return Object.assign(
        {},
        state,
        {
         currentLevel :  action.level,
         currentLevelSubmissionDetails,
         currentLevelDetails
        }
      );
    case VLCommonConstants.GO_TO_NEXT_LEVEL:
      currentLevelSubmissionDetails = state.currentLevelSubmissionDetails;
      currentLevelSubmissionDetails.currentSubmissionStatus = 'NOT_COMPLETED';
      currentLevelSubmissionDetails.resultantQuery = [];
      currentLevelDetails = state.currentLevelDetails;
      currentLevelDetails = Object.assign({}, currentLevelDetails, state.levels[state.currentLevel])
      console.log('currentLevelDetails',state.currentLevel, state.levels[state.currentLevel], currentLevelDetails);
      return Object.assign(
        {},
        state,
        {
          currentLevel : action.level,
          currentLevelSubmissionDetails,
          currentLevelDetails
        }
      );
    default:
      return state;
  }
}

export default {
  attackReducer
}