var base_url = "https://wm31xpbd3k.execute-api.eu-west-1.amazonaws.com/beta/api?url=";

function login (username, pass, success, failure){
	var data = {
      clientId :'contentportal',
      clientSecret : 'contentportal',
      username :username,
      otp : pass
    };
    var dataRequestObject = JSON.stringify({"data": JSON.stringify(JSON.stringify(data)),"clientKeyDetailsId": 11});
	$.ajax({
	type: "POST",
	url: base_url+'kfeedapp/getAccessTokenAndRefreshTokenAPI/v1/',
	beforeSend: function(xhr) {
		xhr.setRequestHeader('x-api-key','ce1JCgYMj53CLOm9OnpPL53JpsOVStSh3UKtWis2');
		xhr.setRequestHeader("Authorization", "Basic " + btoa('contentportal:contentportal'));
	},
	data:  dataRequestObject,
	contentType:'application/json; charset=UTF-8'
	})
	.done(success)
	.fail(failure);
}
function loggedIn (){
	if (this.getCookie("access_token")){
	 return true; } 
	else { return false; }
}
function deleteCookie (name) {
	document.cookie = name + '=;expires=Thu, 16 Jan 1994 00:00:01 GMT;';
}
function getCookie (name){
    var value = "; " + document.cookie;
    var parts = value.split("; " + name + "=");
    if (parts.length == 2)
        return parts.pop().split(";").shift();
    return '';
}
function setCookie (cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
function logout (){
	console.log("Logout");
	this.deleteCookie('access_token');
}
function getToken (){
	return this.getCookie('access_token');
}
export const auth =  {
	login,
	loggedIn,
	deleteCookie,
	getCookie,
	setCookie,
	logout,
	getToken,
}