import {genericWebAPICall} from 'utils/AppAPI';

import * as AceEditorConstants from 'constants/AceEditorConstants';
let constants = AceEditorConstants.default;

function editorApiCall(code, successFunction, failureFunction, language) {
    let xhr = new XMLHttpRequest();
    var url = AppConstants.COMPILER_URL;
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
        var json = JSON.parse(xhr.responseText);
        successFunction(json) ;
      }else {
        failureFunction()
      }
    }
    var data = JSON.stringify({"language": language,"code": code,"stdin":"11"});
    xhr.send(data);
}

function languageToId(language) {
  switch (language) {
    case 'python':
      return 0;
    case 'c_lang':
      return 7;
    case 'cpp_lang':
      return 7;
    default :
      return 0;
  }
}

function setAceEditor (editor) {
  return dispatch => {
    dispatch({
      type: constants.SET_ACE_EDITOR,
      editor : editor
    })
  }
}

function changeAceEditorTheme (aceEditorTheme) {
  return dispatch => {
    dispatch({
      type : constants.CHANGE_ACE_EDITOR_ACEEDITORTHEME,
      aceEditorTheme :aceEditorTheme
    })
  }
}


function changeAceEditorProgrammingLanguage (programmingLanguage) {
  return dispatch => {
    dispatch({
      type : constants.CHANGE_ACE_EDITOR_PROGRAMMINGLANGUAGE,
      programmingLanguage : programmingLanguage
    })
  }
}

function changeAceEditorCode (code) {
  return dispatch => {
    dispatch({
      type : constants.CHANGE_ACE_EDITOR_CODE,
      code : code
    })
  }
}

function changeAceEditorFontSize (fontSize) {
  return dispatch => {
    dispatch({
      type : constants.CHANGE_ACE_EDITOR_FONTSIZE,
      fontSize :fontSize
    })
  }
}

function changeAceEditorIsLoading (isLoading) {
  return dispatch => {
    dispatch({
      type : constants.CHANGE_ACE_EDITOR_ISLOADING,
      isLoading : isLoading
    })
  }
}


function sendCodeForExecution(code, programmingLanguage, isSubmitted, onSuccess=function(){})   {
  let language = languageToId(programmingLanguage);
  return (dispatch,getState) => {
    var successFunction = function(response){
      console.log("response from sendCodeForExecution", response);
      dispatch({
        type : constants.CHANGE_ACE_EDITOR_OUTPUT,
        output : response,
        isLoading : false
      });

      if(isSubmitted){
        console.log('isSubmitted --', getState())
        let requestObject = {};
        let currentLevelDetails = getState().attack.levels[getState().attack.currentLevel-1];
        requestObject.vlqId = getState().attack.currentLevelDetails.vlqId;
        requestObject.metaData = getState().attack.currentLevelSubmissionDetails.metaData;
        let output = {
          optionId: -1,
          text: response.output.replace('\n','')
        }
        requestObject.userSubmission = [output];
        console.log('isSubmitted', requestObject);
        onSuccess(requestObject);
      }
    }
    var failureFunction = function() {
      dispatch({
        type : constants.CHANGE_ACE_EDITOR_OUTPUT,
        output : {
          output:"",
          errors:"",
          time:" .02 "
        } ,
        isLoading : false
      })
    }
    editorApiCall(code, successFunction, failureFunction, language);
  }
}

function submitCodeToServer(){

}

function submitCodeAfterExecution(code, programmingLanguage)   {
  let language = languageToId(programmingLanguage);
  return dispatch => {
    var successFunction = function(response){
      console.log("response from sendCodeForExecution", response);
      dispatch({
        type : constants.CHANGE_ACE_EDITOR_OUTPUT,
        output : response,
        isLoading : false
      });
      submitCodeToServer();
    }
    var failureFunction = function() {
      dispatch({
        type : constants.CHANGE_ACE_EDITOR_OUTPUT,
        output : {
          output:"",
          errors:"",
          time:" .02 "
        } ,
        isLoading : false
      })
    }
    editorApiCall(code, successFunction, failureFunction, language);
  }
}
export const AceEditorActions = {
    setAceEditor,
    changeAceEditorTheme,
    changeAceEditorProgrammingLanguage,
    changeAceEditorCode,
    sendCodeForExecution,
    submitCodeAfterExecution,
    changeAceEditorFontSize,
    changeAceEditorIsLoading
};


