import {auth} from 'utils/auth';

// const BASE_URL = 'http://beta-web.elasticbeanstalk.com/vlabs/';
const BASE_URL = 'https://wm31xpbd3k.execute-api.eu-west-1.amazonaws.com/beta/api?url=';
const LC_BASE_URL = 'http://beta-web.elasticbeanstalk.com/';

export function genericWebAPICall (url, requestObject, onSuccess, onFailure, type='POST') {
    // console.log("type", type)
    if(type == 'POST'){
      const formattedrequestObject = JSON.stringify(JSON.stringify(requestObject));
      const dataRequestObject = JSON.stringify({'data':formattedrequestObject, 'clientKeyDetailsId':1});
      console.log('dataRequestObject', dataRequestObject);
      $.ajax({
        type: 'POST',
        url: BASE_URL + url,
        beforeSend: function (xhr) {
          xhr.setRequestHeader('x-api-key', 'ce1JCgYMj53CLOm9OnpPL53JpsOVStSh3UKtWis2');
          xhr.setRequestHeader('Authorization', 'Bearer '+auth.getCookie('access_token')); 
          // xhr.setRequestHeader('Authorization', 'Bearer LBkdafTjIQTWmOfnUeuTmPY6dbMuTe')
        },
        data: dataRequestObject,
        contentType:'application/json; charset=UTF-8'
      })
        .done(onSuccess)
        .fail(onFailure);
    } else if(type == 'GET'){
      $.ajax({
        type: 'GET',
        url: BASE_URL + url,
        beforeSend: function (xhr) {
          xhr.setRequestHeader('x-api-key', 'ce1JCgYMj53CLOm9OnpPL53JpsOVStSh3UKtWis2');
          xhr.setRequestHeader('Authorization', 'Bearer '+auth.getCookie('access_token')); 
        },
        contentType:'application/json; charset=UTF-8'
      })
        .done(onSuccess)
        .fail(onFailure);
    }
}

export function networkCall (url, requestObject, onSuccess, onFailure, type='POST') {
    // console.log("type", type)
    if(type == 'POST'){
      const formattedrequestObject = JSON.stringify(JSON.stringify(requestObject));
      const dataRequestObject = JSON.stringify({'data':formattedrequestObject, 'clientKeyDetailsId':1});
      // console.log('dataRequestObject >>> ', dataRequestObject);
      $.ajax({
        type: 'POST',
        url: LC_BASE_URL + url,
        beforeSend: function (xhr) {
          // xhr.setRequestHeader('x-api-key', 'grXZBrshN94CwPSpd5DX07adDG0sPkDo9RGULgBX');
           xhr.setRequestHeader('Authorization', 'Bearer '+auth.getCookie('access_token'));
        },
        data: dataRequestObject,
        contentType:'application/json; charset=UTF-8'
      })
        .done(onSuccess)
        .fail(onFailure);
    } else if(type == 'GET'){
      $.ajax({
        type: 'GET',
        url: LC_BASE_URL + url,
        beforeSend: function (xhr) {
          // xhr.setRequestHeader('x-api-key', 'grXZBrshN94CwPSpd5DX07adDG0sPkDo9RGULgBX');
          xhr.setRequestHeader('Authorization', 'Bearer '+auth.getCookie('access_token')); 
        },
        contentType:'application/json; charset=UTF-8'
      })
        .done(onSuccess)
        .fail(onFailure);
    }
}

export default {
  genericWebAPICall,
  networkCall,
};
