'use strict';

/*
 * *******************************************************************************
 *  * Copyright (c) 2018 Edgeworx, Inc.
 *  *
 *  * This program and the accompanying materials are made available under the
 *  * terms of the Eclipse Public License v. 2.0 which is available at
 *  * http://www.eclipse.org/legal/epl-2.0
 *  *
 *  * SPDX-License-Identifier: EPL-2.0
 *  *******************************************************************************
 *
 */

var accTok = apwGetAccessToken();

function Activate_Handlers_For_User_Login() {
	$(document).apwClickPostUp({
		'clickablesSelector': '#user-login-button',
		'postURL': fogControllerUrl + ioAuthoring.urls.login,
		'passToken': 'N',
		'itemsToPost': '{"email" : "user-login-username", "password" : "user-login-password"}',
		'postingResponseHandlerFunction': 'userLoginResponseHandler',
		'postingFailedFunction': 'userLoginFailed'
	});
}

function userLoginFailed() {
	alert("Login failed. Please check your email and password and try again.");
}

function userLoginResponseHandler(response) {
	var result = $.parseJSON(response);
	if (result.status == 'ok') {
		apwSetAccessToken(result.output.token);

		if (result.output.destination == 'changePassword') {
			window.location = ioAuthoring.urls.passwordChange;
		} else {
			window.location = ioAuthoring.urls.build;
		}
	} else {
		Materialize.toast(result.errormessage, 5000);
	}
}

function Activate_Handlers_For_User_Logout() {
	$.ajax({
		type: "POST",
		url: fogControllerUrl + ioAuthoring.urls.logout,
		dataType: "json",
		data: { t: accTok },
		success: function success(res) {
			if (res.status == 'ok') {
				window.location = ioAuthoring.urls.loginPage;
				apwDeleteAccessToken();
			} else {
				userLogoutFailed();
			}
		}
	});
}

function userLogoutFailed() {
	alert("Logout failed.");
}

function Activate_Handlers_For_User_Signup() {
	$(document).apwClickPostUp({
		'clickablesSelector': '#user-signup-button',
		'postURL': fogControllerUrl + ioAuthoring.urls.userSignup,
		'passToken': 'N',
		'itemsToPost': '{"email" : "user-signup-email", "password" : "user-signup-password", "repeatPassword" : "user-signup-repeat-password", "firstName" : "user-signup-first-name", "lastName" : "user-signup-last-name"}',
		'postingResponseHandlerFunction': 'userSignupResponseHandler',
		'postingFailedFunction': 'userSignupFailed'
	});
}

function userSignupFailed() {
	alert("Registration failed due to a technical problem. Please try again in a little while.");
}

function userSignupResponseHandler(response) {
	var result = $.parseJSON(response);

	if (result.status == 'ok') {
		var alert_message = 'Account created.';
		if (result.emailActivation === 'on') {
			alert_message += 'Check your email for activation.';
		}
		alert(alert_message);
		window.location = ioAuthoring.urls.loginPage;
	} else {
		Materialize.toast(result.errormessage, 5000);
	}
}

function Activate_Handlers_For_User_Update_Profile() {
	$(document).apwClickPostUp({
		'clickablesSelector': '#user-update-button',
		'postURL': fogControllerUrl + ioAuthoring.urls.updateUserDetails,
		'passToken': 'Y',
		'itemsToPost': '{"firstName" : "user-update-first-name", "lastName" : "user-update-last-name"}',
		'postingResponseHandlerFunction': 'userUpdateProfileResponseHandler',
		'postingFailedFunction': 'userUpdateProfileFailed'
	});
}

function userUpdateProfileFailed(response) {
	var result = $.parseJSON(response);
	if (result.status != 'ok') {
		Materialize.toast(result.errormessage, 5000);
	}
	//alert("Profile update failed.");
}

function userUpdateProfileResponseHandler(response) {
	var result = $.parseJSON(response);

	if (result.status == 'ok') {
		alert("Profile changes saved.");
	} else {
		userUpdateProfileFailed(response);
	}
}

function Activate_Handlers_For_User_Reset_Password() {
	$(document).apwClickPostUp({
		'clickablesSelector': '#user-forgot-password-button',
		'postURL': fogControllerUrl + ioAuthoring.urls.resetUserPassword,
		'passToken': 'N',
		'itemsToPost': '{"email" : "user-forgot-password-email"}',
		'postingResponseHandlerFunction': 'userResetPasswordResponseHandler',
		'postingFailedFunction': 'userResetPasswordFailed'
	});
}

function userResetPasswordFailed() {
	alert("There was a problem resetting your password.");
}

function userResetPasswordResponseHandler(response) {
	var result = $.parseJSON(response);

	if (result.status == 'ok') {
		alert("A new temporary password has been emailed to you.");
	} else {
		userResetPasswordFailed();
	}
}

function Activate_Handlers_For_User_Change_Password() {
	$(document).apwClickPostUp({
		'clickablesSelector': '#user-change-password-button',
		'postURL': fogControllerUrl + ioAuthoring.urls.updateUserPassword,
		'passToken': 'Y',
		'itemsToPost': '{"oldPassword" : "user-change-password-old", "newPassword" : "user-change-password-new1", "repeatNewPassword" : "user-change-password-new2"}',
		'postingResponseHandlerFunction': 'userChangePasswordResponseHandler',
		'postingFailedFunction': 'userChangePasswordFailed'
	});
}

function userChangePasswordFailed() {
	alert("There was a problem changing your password.");
}

function userChangePasswordResponseHandler(response) {
	var result = $.parseJSON(response);

	if (result.status == 'ok') {
		alert("Your password has been changed.");
	} else {
		Materialize.toast(result.errormessage, 5000);
	}
}

function Activate_Handlers_For_User_Delete_Account() {
	$(document).apwClickAction({
		'clickablesSelector': '#user-delete-account-button',
		'actionFunction': 'handleUserDeleteAccount'
	});
}

function handleUserDeleteAccount() {
	if (confirm("Are you sure you want to delete your account? All of your information will be erased forever.")) {
		$(document).apwManualPostUp({
			'postingResponseHandlerFunction': 'userDeleteAccountResponseHandler',
			'passToken': 'Y',
			'postURL': fogControllerUrl + ioAuthoring.urls.deleteUserAccount,
			'postFailedFunction': 'userDeleteAccountFailed'
		});
	}
}

function userDeleteAccountFailed() {
	alert("There was a problem deleting your account.");
}

function userDeleteAccountResponseHandler(response) {
	var result = $.parseJSON(response);

	if (result.status == 'ok') {
		alert("Your account has been deleted.");
		window.location = ioAuthoring.urls.loginPage;
	} else {
		userDeleteAccountFailed();
	}
}

function Activate_Handlers_For_User_Resend_Activation_Email() {
	$(document).apwClickPostUp({
		'clickablesSelector': '#user-resend-activation-email-button',
		'postURL': fogControllerUrl + ioAuthoring.urls.resendEmailActivationCode,
		'passToken': 'N',
		'itemsToPost': '{"email" : "user-resend-activation-email-email"}',
		'postingResponseHandlerFunction': 'userResendActivationEmailResponseHandler',
		'postingFailedFunction': 'userResendActivationEmailFailed'
	});
}

function userResendActivationEmailFailed() {
	alert("There was a problem sending a new activation email.");
}

function userResendActivationEmailResponseHandler(response) {
	var result = $.parseJSON(response);

	if (result.status == 'ok') {
		alert("A new account activation email has been sent to you.");
	} else {
		userResendActivationEmailFailed();
	}
}