import {genericWebAPICall} from '../../../utils/AppAPI';
import {VLCommonConstants} from 'constants/VLCommon';

function setLevels (response) {
  return {
    type: VLCommonConstants.SET_LEVELS,
    levels : response.vlDetails,
    totalLevels : response.totalLevels,
    currentLevel : response.currentLevel
  }
}

function setHints (response) {
  return {
    type: VLCommonConstants.SET_HINTS,
    hints : response.hints,
    metaData : response.metaData,
    instructions : response.instructions
  }
}

function setQuestion (question) {
  return {
    type: VLCommonConstants.SET_QUESTION,
    question : question
  };
}

function setCurrentLevelSubmissionDetails (response) {
  return {
    type: VLCommonConstants.SET_CURRENT_LEVEL_SUBMISSION_DETAILS,
    currentLevelSubmissionDetails : response
  }
}

function levelCompleted () {
  return {
    type: VLCommonConstants.LEVEL_COMPLETED
  }
}

function goToLevel (levelNumber) {
  return {
    type: VLCommonConstants.GO_TO_LEVEL,
    level : levelNumber
  }
}

function goToNextLevel (levelNumber) {
  return {
    type: VLCommonConstants.GO_TO_NEXT_LEVEL,
    level : levelNumber
  }
}

function setVLLevels (vlLevels) {
  return {
    type : VLCommonConstants.SET_VLLEVELS,
    vlLevels : vlLevels
  }
}
function setVLTypes (vlTypes) {
  return {
    type : VLCommonConstants.SET_VLTYPES,
    vlTypes : vlTypes
  }
}


function getVLQIdsForVLQSetAPI (requestObject) {
  return dispatch => {
    const requestedObject = requestObject;
    genericWebAPICall(
      'vlabs/getVLQIdsForVLQSetAPI/v1/',
      requestedObject,
      function (response) {
        console.log('Success @getVLQIdsForVLQSetAPI response >> ', response);
        if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
          return;
        }
        $('.loading-icon').hide();
        dispatch(setVLLevels(response));
      },
      function (error) {
        console.log('Error @getVLQIdsForVLQSetAPI error >> ', error);
        $('.loading-icon').hide();
      }
    );
  }
}

function getVLTypesAPI (getVLTypesObject) {
  return dispatch => {
    const requestedObject = getVLTypesObject;
    genericWebAPICall(
      'vlabs/getVLTypesAPI/v1/',
      requestedObject,
      function (response) {
        console.log('Success @getVLTypesAPI response >> ', response);
        if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
          $('.tryAgain').show();
          return;
        }
        $('.loading-icon').hide();
        dispatch(setVLTypes(response));
      },
      function (error) {
        console.log('Error @getVLTypesAPI error >> ', error);
        $('.internetDisconnected').show();
        $('.loading-icon').hide();
      }
    );
  }
}

function getLevelsForVLAPI (getLevelsObject) {
  return dispatch => {
    const requestedObject = getLevelsObject;
    genericWebAPICall(
      'vlabs/getLevelsForVLAPI/v1/',
      requestedObject,
      function (response) {
        console.log('Success @getLevelsForVL response >> ',response);
        if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
          return;
        }
        dispatch(setLevels(response));
      },
      function (response) {
        console.log('ERROR @getLevelsForVL response >> ',response);
      }
    );
  }
}

function getVLQDetailsAPI (vlqObject) {
  return dispatch => {
    const requestedObject = vlqObject;
    genericWebAPICall(
      'vlabs/getVLQDetailsAPI/v1/',
      requestedObject,
      function (response) {
        console.log('Success @getVLQDetails response >> ',response);
        if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
          return;
        }
        dispatch(setHints(response));
        if (response.hasOwnProperty('question') && response.question !== null) {
          dispatch(setQuestion(response.question));
        }
      },
      function (response) {
        console.log('ERROR @getVLQDetails response >> ', response);
      }
    );
  }
}

function evaluateVLQAPI (payloadObject) {
  return dispatch => {
    const requestedObject = payloadObject;
    genericWebAPICall(
      'vlabs/evaluateVLQAPI/v1/',
      requestedObject,
      function (response) {
        console.log('Success @evaluateVLQAPI response ',response);
        if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
          $('#icon-basicsqli-Loader').hide();
          $('#basicSQLiQueryStatus').show();
          return;
        }
        dispatch(setCurrentLevelSubmissionDetails(response));
        if (response.currentSubmissionStatus === 'COMPLETED') {
          dispatch(levelCompleted())
        } else if (response.currentSubmissionStatus === 'NOT_COMPLETED') {
          $('.incorrectSubmissionClass').show();
        }
        $('#icon-basicsqli-Loader').hide();
        $('#basicSQLiQueryStatus').show();
      },
      function (response) {
        console.log('ERROR @evaluateVLQAPI response ',response);
        $('#icon-basicsqli-Loader').hide();
        $('#basicSQLiErrorMessage').html('Connection error, Try Again!');
        $('#basicSQLiErrorMessage').show();
      }
    );
  }
}

export const VLCommonActions = {
  getVLQIdsForVLQSetAPI,
  getVLTypesAPI,
  getLevelsForVLAPI,
  getVLQDetailsAPI,
  evaluateVLQAPI,
  goToLevel,
  goToNextLevel
}
