/*
 * chatStatus Javascript
 */

// Initiate Variables
var chatStatus = 'closed';
var chatStatusReason = 'out of service';
var chatTimer = '0';
var proactiveChatDisabled = false;
var chatProactiveAttempts = '0' //Initialise to zero
var chatStatusRefreshTimer = '10000'; // refresh timer set 10 seconds for testing - in production set to '60000'
var chatProactiveRetryAttempts = '1';
var chatStatusUrl = "https://api.chatstatus.mydomain.com/chatStatus.php";
var mybusinessHourId = '5000';
var mychatEntryId = '1003';

getChatStatus(); //on load of page run the (Ajax) Script

function getChatStatus() { //Note - the JSON Response gets auto parsed by jQuery.
	$.ajax({
			url: chatStatusUrl,
			data: {businessHoursId: mybusinessHourId, chatEntryId: mychatEntryId},
			dataType: 'json',
			success: function(chatStatusAPI) {
				chatStatus = chatStatusAPI.chatStatus;
				chatStatusReason = chatStatusAPI.chatStatusReason;
				chatTimer = chatStatusAPI.chatTimer;
				if (chatStatusAPI.debugLevel > 0) {
					console.log(chatStatusAPI);
			}
			setChatStatus(); //on success call this function to update the page
				}
		})				
		.fail(function() {
			chatStatus = 'closed';
			chatStatusReason = 'out of service';
			chatTimer = '0';
			console.log("chatStatus: Ajax Call failed ------");
			setChatStatus(); //on fail call this function to update the page
		})
		
};

function setChatStatus() {

	if (chatStatus == 'closed') {
		$('#mychatOff').show();
		$('#mychatOn').hide();
		$('#mychatBusy').hide();
		$('#mychatPageLoad').hide();
		
		if (chatStatusReason == 'Holiday') { //Case sensensitve = match exactly!
			$('#mychatclosed-holidaymessage').show();
			$('#mychatclosed-standardmessage').hide();
			$('#mychatclosed-emergencymessage').hide();
			$('#mychatclosed-outofservicemessage').hide();
		}
		else if (chatStatusReason == 'Emergency') { //Case sensensitve = match exactly!
			$('#mychatclosed-emergencymessage').show();
			$('#mychatclosed-holidaymessage').hide();
			$('#mychatclosed-standardmessage').hide();
			$('#mychatclosed-outofservicemessage').hide();
		}
		else if (chatStatusReason == 'out of service') { //Case sensensitve = match exactly!
			$('#mychatclosed-outofservicemessage').show();
			$('#mychatclosed-holidaymessage').hide();
			$('#mychatclosed-standardmessage').hide();
			$('#mychatclosed-emergencymessage').hide();
		}
		else { 
			$('#mychatclosed-standardmessage').show();
			$('#mychatclosed-outofservicemessage').hide();
			$('#mychatclosed-holidaymessage').hide();
			$('#mychatclosed-emergencymessage').hide();
		}
	}

	if (chatStatus == 'busy') {
		$('#mychatBusy').show();
		$('#mychatOff').hide();
		$('#mychatOn').hide();
		$('#mychatPageLoad').hide();
	}
	
	if (chatStatus == 'open') {
		$('#mychatOn').show();
		$('#mychatOff').hide();
		$('#mychatBusy').hide();
		$('#mychatPageLoad').hide();

		//Proactive Chat
		if (chatProactiveRetryAttempts > chatProactiveAttempts){
			setTimeout(function() {
				if ((chatTimer != 0) && (proactiveChatDisabled == false) && (chatStatus == 'open') && (chatProactiveRetryAttempts > chatProactiveAttempts)) { //Since open state can change by the timer occurs - need to check again before showing 
					chatProactiveAttempts++
					$('#chatModal').modal();
				}
			}, chatTimer);
		}
	}

	if (chatStatusReason == 'out of service'){
		setTimeout(getChatStatus, 5*chatStatusRefreshTimer); //call the getChatStatus function again in 5 times standard X seconds if chatStatusReason is 'out of service'
	}else{
		setTimeout(getChatStatus, chatStatusRefreshTimer); //call the getChatStatus function again in X seconds, where X is the chatStatusRefreshTimer
	}



}

function disableProactiveChat() {
		proactiveChatDisabled = true;
		console.log('chatStatus: Proactive Chat Disabled!');
}
