var pnfpb_onesignal_userid;

const { __ } = wp.i18n;

pnfpb_onesignal_userid = pnfpb_ajax_object_onesignal_push.userid;

var pnfpb_onesignal_userid_os = pnfpb_onesignal_userid;

if (pnfpb_onesignal_userid === '1') {
	
	pnfpb_onesignal_userid_os = '1pnfpbadm';
	
}


var pnfpb_standalone = window.navigator.standalone,
  pnfpb_userAgent = window.navigator.userAgent.toLowerCase(),
  pnfpb_safari = /safari/.test(pnfpb_userAgent),
  pnfpb_ios = /iphone|ipod|ipad/.test(pnfpb_userAgent);
	
var pnfpb_webview = false;

if (pnfpb_ios) {
  if (!pnfpb_standalone && pnfpb_safari) {
    // Safari
    pnfpb_webview = false;
  } else if (!pnfpb_standalone && !pnfpb_safari) {
    // iOS webview
    pnfpb_webview = true;
  	
  };
} else {
  if (pnfpb_userAgent.includes('wv')) {
    // Android webview
    pnfpb_webview = true;
  } else {
    // Chrome
    pnfpb_webview = false;
  }
};

var $j = jQuery.noConflict();

$j(document).ready(function() {
	
	var pnfpbonesignalid = parseInt(pnfpb_onesignal_userid)
	
	if (pnfpb_webview) {
		
		//userid for users who are using webviewgold app
		
        if (pnfpb_userid_to_android_webview) {
               
            pnfpb_userid_to_android_webview.postMessage(pnfpb_onesignal_userid);
               
        }
		
		// For webtonative users
		const { setExternalUserId } = WTN.OneSignal;

		setExternalUserId(pnfpb_onesignal_userid_os);		
		
		const { getPlayerId } = WTN.OneSignal;
		
		getPlayerId().then(function(playerId) {
			
  			if(playerId){
    			// handle for playerId
				if (pnfpb_onesignal_userid) {
					var data = {
						action: 'icpushcallback',
						onesignal_externalid:pnfpbonesignalid,
						nonce: pnfpb_ajax_object_onesignal_push.nonce,
						pushtype: 'onesignal_subscribed_users'
					};	
					$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(responseajax) {
						console.log(responseajax);
					});
				}
 	 		}
			
		});
	}
	else 
	{
		if (typeof OneSignal !== "undefined" && typeof OneSignalDeferred === "undefined") {
			
			OneSignal.push(function() {
			
				if (typeof OneSignal.login === "function" && pnfpb_onesignal_userid_os != '0') { 
				
  					OneSignal.login(pnfpb_onesignal_userid_os);
				
					var data = {
						action: 'icpushcallback',
						onesignal_externalid:pnfpb_onesignal_userid_os,
						nonce: pnfpb_ajax_object_onesignal_push.nonce,						
						pushtype: 'onesignal_subscribed_users'
					};
							
					$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(responseajax) {
						
					});
				
				} else {
					if (typeof OneSignal.setExternalUserId === "function"  && pnfpb_onesignal_userid_os != '0') {
					
						OneSignal.setExternalUserId(pnfpb_onesignal_userid_os);
					
						var data = {
							action: 'icpushcallback',
							onesignal_externalid:pnfpb_onesignal_userid_os,
							nonce: pnfpb_ajax_object_onesignal_push.nonce,							
							pushtype: 'onesignal_subscribed_users'
						};
							
						$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(responseajax) {
						
						});	
					
					}
				}
			});
			
		} else {
			
			if (typeof OneSignalDeferred !== "undefined" && typeof OneSignalDeferred.push === "function" && pnfpb_onesignal_userid_os != '0') {
				
				OneSignalDeferred.push(function(OneSignal) {
  					OneSignal.User.PushSubscription.addEventListener("change", pnfpb_pushSubscriptionChangeListener);
				});				

			}
		}
	}
	
	var currentToken = 'onesignal@N';
	
	$j(".subscribegroupbutton").removeClass( "subscribe-init-display-off" );
	$j(".unsubscribegroupbutton").removeClass( "subscribe-init-display-off" );
	$j(".subscribe-notification-group").removeClass( "subscribe-init-display-off" );
	$j(".unsubscribe-notification-group").removeClass( "subscribe-init-display-off" );	
	
	$j(".group-button").on("click",".join-group", function() {
		var join_group_string_array = $j(this).parent().attr('id').split("-");
		if (join_group_string_array.length > 0) {
			var join_group_id = join_group_string_array[1];
			var join_group_unsubscribebuttonname = '.unsubscribegroupbutton-'+join_group_id;
			var join_group_subscribebuttonname = '.subscribegroupbutton-'+join_group_id;
			var join_group_unsubscribebuttonid = '#unsubscribegroupbutton-'+join_group_id;
			var join_group_subscribebuttonid = '#subscribegroupbutton-'+join_group_id;																										$j(join_group_subscribebuttonname).removeClass( "subscribe-display-off" ).addClass( "subscribe-display-on" );
			$j(join_group_unsubscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
			$j(join_group_subscribebuttonid).removeClass( "subscribe-display-off" ).addClass( "subscribe-display-on" );
			$j(join_group_unsubscribebuttonid).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );										
		}
	});
							
	$j(".group-button").on("click",".leave-group", function() {
		var leave_group_string_array = $j(this).parent().attr('id').split("-");
		if (leave_group_string_array.length > 0) {
			var leave_group_id = leave_group_string_array[1];
			var leave_group_unsubscribebuttonname = '.unsubscribegroupbutton-'+leave_group_id;
			var leave_group_subscribebuttonname = '.subscribegroupbutton-'+leave_group_id;
			var leave_group_unsubscribebuttonid = '#unsubscribegroupbutton-'+leave_group_id;
			var leave_group_subscribebuttonid = '#subscribegroupbutton-'+leave_group_id;
			$j(leave_group_subscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
			$j(leave_group_unsubscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
			$j(leave_group_subscribebuttonid).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
			$j(leave_group_unsubscribebuttonid).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
										
		}
		if (currentToken) {
			
			deviceid = currentToken;
			
			var data = {
				action: 'icpushcallback',
				device_id:deviceid,
				bpgroup_id:leave_group_id,
				nonce: pnfpb_ajax_object_onesignal_push.nonce,				
				pushtype: 'unsubscribe-group-button'
			};
								
			$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(response) {
							        
			   if (response != 'fail')
			   {
    	           if (response != 'duplicate') {
					   
					document.cookie = "pnfpb_group_push_notification_"+leave_group_id + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
					   
					$j(leave_group_subscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
					   
					$j(leave_group_unsubscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
					   
			       }
			       else
			       {
			           
			         console.log(__("Push notification unsubscription failed..try again!!","push-notification-for-post-and-buddypress"));
					   
			        }

			    }

			});	
		}
	});
	
	$j(document).on("click",".join-group", function() {
		
		var join_group_string_array = $j(this).parent().attr('id').split("-");
		
		if (join_group_string_array.length > 0) {
			
			var join_group_id = join_group_string_array[1];
			var join_group_unsubscribebuttonname = '.unsubscribegroupbutton-'+join_group_id;
			var join_group_subscribebuttonname = '.subscribegroupbutton-'+join_group_id;
			var join_group_unsubscribebuttonid = '#unsubscribegroupbutton-'+join_group_id;
			var join_group_subscribebuttonid = '#subscribegroupbutton-'+join_group_id;
			$j(join_group_subscribebuttonname).removeClass( "subscribe-display-off" ).addClass( "subscribe-display-on" );
			$j(join_group_unsubscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );																		$j(join_group_subscribebuttonid).removeClass( "subscribe-display-off" ).addClass( "subscribe-display-on" );
			$j(join_group_unsubscribebuttonid).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
			
		}
	});
							
	$j(document).on("click",".leave-group", function() {
		
		var leave_group_string_array = $j(this).parent().attr('id').split("-");
		
		if (leave_group_string_array.length > 0) {
										
			var leave_group_id = leave_group_string_array[1];
			var leave_group_unsubscribebuttonname = '.unsubscribegroupbutton-'+leave_group_id;
			var leave_group_subscribebuttonname = '.subscribegroupbutton-'+leave_group_id;
			var leave_group_unsubscribebuttonid = '#unsubscribegroupbutton-'+leave_group_id;
			var leave_group_subscribebuttonid = '#subscribegroupbutton-'+leave_group_id;
			$j(leave_group_subscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
			$j(leave_group_unsubscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
			$j(leave_group_subscribebuttonid).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
			$j(leave_group_unsubscribebuttonid).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
										
		}
		
		if (currentToken) {
			
			var deviceid = currentToken;
			
			var data = {
				action: 'icpushcallback',
				device_id:deviceid,
				bpgroup_id:leave_group_id,
				nonce: pnfpb_ajax_object_onesignal_push.nonce,				
				pushtype: 'unsubscribe-group-button'
			};
								
			$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(response) {
									        
			     if (response != 'fail')
			     {
    	             if (response != 'duplicate') {
						 
						document.cookie = "pnfpb_group_push_notification_"+leave_group_id + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
    	                                                        
						$j(leave_group_subscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
						$j(leave_group_unsubscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
						 
			          }
			          else
			          {
			           
			              console.log(__("Push notification unsubscription failed..try again!!","push-notification-for-post-and-buddypress"));
			           }

			     }

							
			});	
		}
	});
	
	var pnfpb_subscribe_notification_group_element =  document.getElementsByClassName("subscribe-notification-group");
	
	var pnfpb_group_users_subscribe_dialog_confirm =  document.getElementById("pnfpb_group_users_subscribe_dialog_confirm");
	
	if( typeof(pnfpb_subscribe_notification_group_element) != 'undefined' && pnfpb_subscribe_notification_group_element != null ) {
				
		$j(document).on( "click",".subscribe-notification-group", function(e) {
			
			e.preventDefault();
			var groupId = $j(this).attr("data-group-id");
			var unsubscribebuttonname = '.unsubscribegroupbutton-'+groupId;
			var subscribebuttonname = '.subscribegroupbutton-'+groupId;
			var unsubscribebuttonid = '#unsubscribegroupbutton-'+groupId;
			var subscribebuttonid = '#subscribegroupbutton-'+groupId;
			
			if( typeof(pnfpb_group_users_subscribe_dialog_confirm) === 'undefined' || pnfpb_group_users_subscribe_dialog_confirm === null ) {

				$j(this).append( '<div id="pnfpb_group_users_subscribe_dialog_confirm" class="pnfpb_group_users_subscribe_dialog_confirm" title="Subscribe" style="display:none;"><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>'+pnfpb_ajax_object_onesignal_push.group_subscribe_dialog_text+'</div><div id="pnfpb-group-unsubscribe-dialog" title="Confirmation"><div id="pnfpb-group-unsubscribe-alert-msg" class="pnfpb-group-unsubscribe-alert-msg"></div></div>' );
				
			}
			
 			var subscribe_button_text = pnfpb_ajax_object_onesignal_push.subscribe_button_text;
			
			var deviceid = currentToken;
			
			$j( "#pnfpb_group_users_subscribe_dialog_confirm" ).dialog({  
				resizable: false,
				height: "auto",
				closeText : '',
			   	modal: true,
			   	buttons: [{
      				text: subscribe_button_text,
					open: function() {
						$j(this).attr('style','font-weight:bold;color:'+pnfpb_ajax_object_onesignal_push.subscribe_button_text_color+';background-color:'+pnfpb_ajax_object_onesignal_push.subscribe_button_color+';border:0px');
					},
      				click: function() {
						const d = new Date();
						d.setTime(d.getTime() + (365*24*60*60*1000));
						let expires = "expires="+ d.toUTCString();
						var cookievalue = "pnfpb_group_push_notification_"+groupId + "=" + "expiretime" + ";" + expires + ";path=/";
						document.cookie = cookievalue;
									
						if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.subscribeGroupid) {
       						window.webkit.messageHandlers.subscribeGroupid.postMessage({
                     			"message": groupId
       						});
						}									
									
						var data = {
							action: 'icpushcallback',
							device_id:deviceid,
							bpgroup_id:groupId,
							nonce: pnfpb_ajax_object_onesignal_push.nonce,
							pushtype: 'subscribe-group-button'
						};
								
						$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(response) {
							
							$j(".pnfpb-group-unsubscribe-alert-msg").html("<p>"+pnfpb_ajax_object_onesignal_push.group_subscribe_dialog_text_confirm+"</p>");
			       			$j( "#pnfpb-group-unsubscribe-dialog" ).dialog();
							$j(subscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
							$j(unsubscribebuttonname).removeClass( "subscribe-display-off" ).addClass( "subscribe-display-on" );
							$j(subscribebuttonid).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
							$j(unsubscribebuttonid).removeClass( "subscribe-display-off" ).addClass( "subscribe-display-on" );	
							$j( "#pnfpb_group_users_subscribe_dialog_confirm" ).dialog("close");
							
						})

					},
					Cancel: function() {
						$j( "#pnfpb_group_users_subscribe_dialog_confirm" ).dialog("close");
					}
				}]
    		})

		});
	}
	
	var pnfpb_unsubscribe_notification_group_element =  document.getElementsByClassName("unsubscribe-notification-group");
	
	var pnfpb_group_users_unsubscribe_dialog_confirm =  document.getElementById("pnfpb_group_users_unsubscribe_dialog_confirm");
				
	if( typeof(pnfpb_unsubscribe_notification_group_element) != 'undefined' && pnfpb_unsubscribe_notification_group_element != null ) {
					
		$j(document).on( "click",".unsubscribe-notification-group", function(e) {
			
			e.preventDefault();
			var groupId = $j(this).attr("data-group-id");
			document.cookie = "pnfpb_group_push_notification_"+groupId + "; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"				
			var unsubscribebuttonname = '.unsubscribegroupbutton-'+groupId;
			var subscribebuttonname = '.subscribegroupbutton-'+groupId;
			var unsubscribebuttonid = '#unsubscribegroupbutton-'+groupId;
			var subscribebuttonid = '#subscribegroupbutton-'+groupId;
			
			if(typeof(pnfpb_group_users_unsubscribe_dialog_confirm) === 'undefined' || pnfpb_group_users_unsubscribe_dialog_confirm === null ) {

				$j(this).append( '<div id="pnfpb_group_users_unsubscribe_dialog_confirm" class="pnfpb_group_users_unsubscribe_dialog_confirm" title="Unsubscribe" style="display:none;"><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>'+pnfpb_ajax_object_onesignal_push.group_unsubscribe_dialog_text+'</div><div id="pnfpb-group-unsubscribe-dialog" title="Confirmation"><div id="pnfpb-group-unsubscribe-alert-msg" class="pnfpb-group-unsubscribe-alert-msg"></div></div>' );
				
			}
			
			var unsubscribe_button_text = pnfpb_ajax_object_onesignal_push.unsubscribe_button_text;
			
			var deviceid = currentToken;
			
			$j( "#pnfpb_group_users_unsubscribe_dialog_confirm" ).dialog({  
				resizable: false,
				height: "auto",
				closeText : '',
				modal: true,
				buttons: [{
      				text: unsubscribe_button_text,
					open: function() {
						$j(this).attr('style','font-weight:bold;color:'+pnfpb_ajax_object_onesignal_push.subscribe_button_text_color+';background-color:'+pnfpb_ajax_object_onesignal_push.subscribe_button_color+';border:0px');
					},
      				click: function() {
								
						if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unsubscribeGroupid) {
       						window.webkit.messageHandlers.unsubscribeGroupid.postMessage({
                     			"message": groupId
       						});
						}								
								
						if (currentToken) {
							   
							deviceid = currentToken;
							
							var data = {
								action: 'icpushcallback',
								device_id:deviceid,
								bpgroup_id:groupId,
								nonce: pnfpb_ajax_object_onesignal_push.nonce,								
								pushtype: 'unsubscribe-group-button'
							};
								
							$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(response) {
										            
			                	if (response != 'fail')
			                    {
    	                           if (response != 'duplicate'){
									   
									   document.cookie = "pnfpb_group_push_notification_"+ groupId + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
    	                                                        
										$j(unsubscribebuttonname).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
										$j(subscribebuttonname).removeClass( "subscribe-display-off" ).addClass( "subscribe-display-on" );
										$j(unsubscribebuttonid).removeClass( "subscribe-display-on" ).addClass( "subscribe-display-off" );
										$j(subscribebuttonid).removeClass( "subscribe-display-off" ).addClass( "subscribe-display-on" );
																
										$j(".pnfpb-group-unsubscribe-alert-msg").html("<p>"+pnfpb_ajax_object_onesignal_push.group_unsubscribe_dialog_text_confirm+"</p>");
																
										$j( "#pnfpb-group-unsubscribe-dialog" ).dialog();
																
			                        }
			                        else
			                        {
			                             console.log(__("Push notification unsubscription failed..try again!!","push-notification-for-post-and-buddypress"));
			                         }

			                       }
			                       else
			                       {           
                                        $j(".pnfpb-group-unsubscribe-alert-msg").html(__("Push notification unsubscription failed..try again!!","push-notification-for-post-and-buddypress"));
                                                            
                                         $j( "#pnfpb-group-unsubscribe-dialog" ).dialog();

			                             console.log(__("device update failed","push-notification-for-post-and-buddypress"));
			                         }
							
							});
							
						}
						$j("#pnfpb_group_users_unsubscribe_dialog_confirm").dialog("close");
					
					},
					Cancel: function() {
						$j( "#pnfpb_group_users_unsubscribe_dialog_confirm" ).dialog( "close" );
					}
				}]
			})
		})				
	}	

	var pnfpb_push_notification_frontend_settings_element =  document.getElementsByClassName("pnfpb_push_notification_frontend_settings_submit");
	
	if (pnfpb_ajax_object_onesignal_push.userid > 0 && typeof(pnfpb_push_notification_frontend_settings_element) != 'undefined' && pnfpb_push_notification_frontend_settings_element != null) {
			
		
		var subscriptionoptions = '00000000001';
		var subscribeallshortcode = '0';
		var subscribepostactivitiesshortcode = '0';
		var subscribeallcommentsshortcode = '0';
		var subscribemypostshortcode = '0';
		var subscribeprivatemessagesshortcode = '0';
		var subscribenewmembershortcode = '0';
		var subscribefriendshiprequestshortcode = '0';
		var subscribefriendshipacceptshortcode = '0';
		var subscribeuseravatarshortcode = '0';
		var subscribecoverimageshortcode = '0';
		var subscribegroupinviteshortcode = '0';
		var subscribegroupdetailsshortcode = '0';
		var unsubscribeallshortcode = '0';
		var subscribeactivitiesshortcode = '0';
		var subscribemarkfavouriteshortcode = '0';
		var pnfpb_custom_post_types = JSON.parse(pnfpb_ajax_object_onesignal_push.pnfpb_show_custom_post_types);
		
		var data = {
			action: 'icpushcallback',
			nonce: pnfpb_ajax_object_onesignal_push.nonce,
			onesignal_get_subscriptionoptions_id:pnfpbonesignalid,
			pushtype: 'onesignal_get_frontend_subscriptions'
		};
							
		$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(responseajax) {
					
					var response = JSON.parse(responseajax);
					console.log(responseajax);						
					var subscriptionoptions = response.subscriptionoptions;
			
					if (subscriptionoptions === null) {
						subscriptionoptions = '1000000000000';
					}
					
					var subscriptionoptionsarray = subscriptionoptions.split('');
					
					if (subscriptionoptionsarray.length >= 4)
					{
						if (subscriptionoptionsarray[1] === '1'  || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_post_enable').prop('checked', true);
						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_post_enable').prop('checked', false);
						}
									
						if ((subscriptionoptionsarray[11] && subscriptionoptionsarray[11] === '1') || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_bactivity_enable').prop('checked', true);

						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_bactivity_enable').prop('checked', false);
						}									
							
						if (subscriptionoptionsarray[2] === '1' || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_bcomment_enable').prop('checked', true);
						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_bcomment_enable').prop('checked', false);
						}
								
						if (subscriptionoptionsarray[3] === '1' || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_mybcomment_enable').prop('checked', true);
						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_mybcomment_enable').prop('checked', false);
						}								
							
						if ((subscriptionoptionsarray[4] && subscriptionoptionsarray[4] === '1') || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_bprivatemessage_enable').prop('checked', true);

						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_bprivatemessage_enable').prop('checked', false);
						}
															
															
						if ((subscriptionoptionsarray[5] && subscriptionoptionsarray[5] === '1') || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_new_member_enable').prop('checked', true);

						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_new_member_enable').prop('checked', false);
						}
															
						if ((subscriptionoptionsarray[6] && subscriptionoptionsarray[6] === '1') || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_friendship_request_enable').prop('checked', true);

						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_friendship_request_enable').prop('checked', false);
						}
															
						if ((subscriptionoptionsarray[7] && subscriptionoptionsarray[7] === '1') || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_friendship_accept_enable').prop('checked', true);

						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_friendship_accept_enable').prop('checked', false);
						}
															
						if ((subscriptionoptionsarray[8] && subscriptionoptionsarray[8] === '1') || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_avatar_change_enable').prop('checked', true);

						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_avatar_change_enable').prop('checked', false);
						}
															
						if ((subscriptionoptionsarray[9] && subscriptionoptionsarray[9] === '1') || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_cover_image_change_enable').prop('checked', true);

						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_cover_image_change_enable').prop('checked', false);
						}
											
						if ((subscriptionoptionsarray[12] && subscriptionoptionsarray[12] === '1') || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_group_details_update_enable').prop('checked', true);

						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_group_details_update_enable').prop('checked', false);
						}
											
						if ((subscriptionoptionsarray[13] && subscriptionoptionsarray[13] === '1') || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_group_invite_enable').prop('checked', true);
						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_group_invite_enable').prop('checked', false);
						}
						
						if ((subscriptionoptionsarray[24] && subscriptionoptionsarray[24] === '1') || subscriptionoptionsarray[0] === '1')
						{
  							$j('#pnfpb_ic_fcm_front_favourite_enable').prop('checked', true);
						}
						else 
						{
							$j('#pnfpb_ic_fcm_front_favourite_enable').prop('checked', false);
						}
						
						if (subscriptionoptionsarray.length >= 14) {

							var subscription_options_element = 14;

							for (var pnfpb_frontend_post_type_element = 0; pnfpb_frontend_post_type_element < pnfpb_custom_post_types.length; pnfpb_frontend_post_type_element++) {

								if (subscriptionoptionsarray[subscription_options_element] === '1'  || subscriptionoptionsarray[0] === '1')
								{
									$j('.pnfpb_ic_fcm_front_subscription_'+pnfpb_custom_post_types[pnfpb_frontend_post_type_element]+'_enable').prop('checked', true);

								}
								else 
								{
									$j('.pnfpb_ic_fcm_front_subscription_'+pnfpb_custom_post_types[pnfpb_frontend_post_type_element]+'_enable').prop('checked', false);

								}

								subscription_options_element++;

							}

						}							
					}				
					
					$j(".pnfpb_push_notification_frontend_settings_submit").on( "click", function(event) {
			
					event.preventDefault();
			
					$j('.pnfpb_ic_front_push_notification_settings_messages').removeClass('success');
					$j('.pnfpb_ic_front_push_notification_settings_messages').removeClass('error');
					$j('.pnfpb_ic_front_push_notification_settings_messages').addClass('info');
					$j('.pnfpb_ic_front_push_notification_settings_text').html(__('Processing...',"push-notification-for-post-and-buddypress"));
					$j('.pnfpb_ic_front_push_notification_settings_messages').attr('style','display: flex !important');										
			

					if ($j('#pnfpb_ic_fcm_front_post_enable').is(":checked"))
					{
						subscribepostactivitiesshortcode = '1';
					}
					else 
					{
						subscribepostactivitiesshortcode = '0';
					}
						
					if ($j('#pnfpb_ic_fcm_front_bactivity_enable').is(":checked"))
					{
						subscribeactivitiesshortcode = '1';
					}
					else 
					{
						subscribeactivitiesshortcode = '0';
					}							
						
					if ($j('#pnfpb_ic_fcm_front_bcomment_enable').is(":checked"))
					{
						subscribeallcommentsshortcode = '1';
						subscribemypostshortcode = '0';
					}
					else 
					{
						subscribeallcommentsshortcode = '0';
					}
			
					if ($j('#pnfpb_ic_fcm_front_mybcomment_enable').is(":checked"))
					{
						subscribemypostshortcode = '1';
						subscribeallcommentsshortcode = '0';
					}
					else 
					{
						subscribemypostshortcode = '0';
					}				
						
					if ($j('#pnfpb_ic_fcm_front_bprivatemessage_enable').is(":checked"))
					{
						subscribeprivatemessagesshortcode = '1';
						subscribeallshortcode = '0'; 
						unsubscribeallshortcode = '0';			
								
					}
					else 
					{
						subscribeprivatemessagesshortcode = '0';
					}
						
					if ($j('#pnfpb_ic_fcm_front_new_member_enable').is(":checked"))
					{
						subscribenewmembershortcode = '1';
						subscribeallshortcode = '0'; 
						unsubscribeallshortcode = '0';
					}
					else 
					{

						subscribenewmembershortcode = '0';
					}
						
					if ($j('#pnfpb_ic_fcm_front_friendship_request_enable').is(":checked"))
					{
						subscribefriendshiprequestshortcode = '1';
						subscribeallshortcode = '0'; 
						unsubscribeallshortcode = '0';
					}
					else 
					{
						subscribefriendshiprequestshortcode = '0';
					}
						
					if ($j('#pnfpb_ic_fcm_front_friendship_accept_enable').is(":checked"))
					{
						subscribefriendshipacceptshortcode = '1';
						subscribeallshortcode = '0'; 
						unsubscribeallshortcode = '0';
					}
					else 
					{
						subscribefriendshipacceptshortcode = '0';
					}
						
					if ($j('#pnfpb_ic_fcm_front_avatar_change_enable').is(":checked"))
					{
						subscribeuseravatarshortcode = '1';
						subscribeallshortcode = '0'; 
						unsubscribeallshortcode = '0';
					}
					else 
					{
						subscribeuseravatarshortcode = '0';
					}
						
					if ($j('#pnfpb_ic_fcm_front_cover_image_change_enable').is(":checked"))
					{
						subscribecoverimageshortcode = '1';
						subscribeallshortcode = '0'; 
						unsubscribeallshortcode = '0';
					}
					else 
					{
						subscribecoverimageshortcode = '0';
					}
			
					if ($j('#pnfpb_ic_fcm_front_group_invite_enable').is(":checked"))
					{
						subscribegroupinviteshortcode = '1';
						subscribeallshortcode = '0'; 
						unsubscribeallshortcode = '0';
					}
					else 
					{
						subscribegroupinviteshortcode = '0';
					}
			
					if ($j('#pnfpb_ic_fcm_front_group_details_update_enable').is(":checked"))
					{
						subscribegroupdetailsshortcode = '1';
						subscribeallshortcode = '0'; 
						unsubscribeallshortcode = '0';
					}
					else 
					{
						subscribegroupdetailsshortcode = '0';
					}
						
					if ($j('.pnfpb_ic_fcm_front_favourite_enable').is(":checked"))
					{
						subscribemarkfavouriteshortcode = '1';
						subscribeallshortcode = '0'; 
						unsubscribeallshortcode = '0';
					}
					else 
					{
						subscribemarkfavouriteshortcode = '0';
					}					
						
					var pnfpb_custom_post_types_subscriptions = [];
						


					var frontend_custom_post_options = '';

					if (pnfpb_custom_post_types.length > 0) {

						for (var pnfpb_post_type_element = 0; pnfpb_post_type_element < pnfpb_custom_post_types.length; pnfpb_post_type_element++) {

							if ($j('.pnfpb_ic_fcm_front_subscription_'+pnfpb_custom_post_types[pnfpb_post_type_element]+'_enable').is(":checked"))
							{
								pnfpb_custom_post_types_subscriptions[pnfpb_post_type_element] = '1';
								subscribeallshortcode = '0'; 
								unsubscribeallshortcode = '0';
							}
							else
							{
								pnfpb_custom_post_types_subscriptions[pnfpb_post_type_element] = '0';
							}								
						}
					}

					if (pnfpb_custom_post_types_subscriptions.length > 0) {

						for (var pnfpb_post_type_element = 0; pnfpb_post_type_element <= 9; pnfpb_post_type_element++) {

							if (pnfpb_post_type_element < pnfpb_custom_post_types.length && pnfpb_custom_post_types_subscriptions[pnfpb_post_type_element]) {

								frontend_custom_post_options  = frontend_custom_post_options + pnfpb_custom_post_types_subscriptions[pnfpb_post_type_element];

							} else  {

								frontend_custom_post_options  = frontend_custom_post_options + '0';
							}
								
						}

					} else {
						frontend_custom_post_options  = frontend_custom_post_options + '0000000000';
					}
			
					subscriptionoptions = subscribeallshortcode + subscribepostactivitiesshortcode + subscribeallcommentsshortcode + subscribemypostshortcode + subscribeprivatemessagesshortcode + subscribenewmembershortcode + subscribefriendshiprequestshortcode + subscribefriendshipacceptshortcode + subscribeuseravatarshortcode + subscribecoverimageshortcode + unsubscribeallshortcode + subscribeactivitiesshortcode + subscribegroupdetailsshortcode + subscribegroupinviteshortcode + frontend_custom_post_options + subscribemarkfavouriteshortcode;
						
					var data = {
						action: 'icpushcallback',
						nonce: pnfpb_ajax_object_onesignal_push.nonce,	
						onesignal_subscriptionoptions:subscriptionoptions,
						pushtype: 'onesignal_frontend_subscriptions'
					};
							
					$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(responseajax) {
					
						var response = JSON.parse(responseajax);
												
						subscriptionoptions = response.subscriptionoptions;

						var subscriptionoptionsarray = [];
												
						if (subscriptionoptions) {
							subscriptionoptionsarray = subscriptionoptions.split('');
						}								

						if (subscriptionoptionsarray.length >= 11)
						{
							$j('.pnfpb_ic_front_push_notification_settings_messages').removeClass('error');
							$j('.pnfpb_ic_front_push_notification_settings_messages').removeClass('info');
							$j('.pnfpb_ic_front_push_notification_settings_messages').addClass('success');
							$j('.pnfpb_ic_front_push_notification_settings_messages').attr('style','display: flex !important');
							$j('.pnfpb_ic_front_push_notification_settings_text').html(__('Your notification settings have been saved',"push-notification-for-post-and-buddypress"));
					
							if (subscriptionoptionsarray[1] === '1')
							{
								$j('#pnfpb_ic_fcm_front_post_enable').prop('checked', true);
							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_post_enable').prop('checked', false);
							}
								
							if (subscriptionoptionsarray[11] === '1')
							{
								$j('#pnfpb_ic_fcm_front_bactivity_enable').prop('checked', true);

							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_bactivity_enable').prop('checked', false);
							}									
						
							if (subscriptionoptionsarray[2] === '1')
							{
								$j('#pnfpb_ic_fcm_front_bcomment_enable').prop('checked', true);
							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_bcomment_enable').prop('checked', false);
							}
								
							if (subscriptionoptionsarray[3] === '1')
							{
								$j('#pnfpb_ic_fcm_front_mybcomment_enable').prop('checked', true);
							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_mybcomment_enable').prop('checked', false);
							}									
						
							if (subscriptionoptionsarray[4] === '1')
							{
								$j('#pnfpb_ic_fcm_front_bprivatemessage_enable').prop('checked', true);

							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_bprivatemessage_enable').prop('checked', false);
							}
														
														
							if (subscriptionoptionsarray[5] === '1')
							{
								$j('#pnfpb_ic_fcm_front_new_member_enable').prop('checked', true);

							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_new_member_enable').prop('checked', false);
							}
														
							if (subscriptionoptionsarray[6] === '1')
							{
								$j('#pnfpb_ic_fcm_front_friendship_request_enable').prop('checked', true);

							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_friendship_request_enable').prop('checked', false);
							}
														
							if (subscriptionoptionsarray[7] === '1')
							{
								$j('#pnfpb_ic_fcm_front_friendship_accept_enable').prop('checked', true);

							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_friendship_accept_enable').prop('checked', false);
							}
														
							if (subscriptionoptionsarray[8] === '1')
							{
								$j('#pnfpb_ic_fcm_front_avatar_change_enable').prop('checked', true);

							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_avatar_change_enable').prop('checked', false);
							}
														
							if (subscriptionoptionsarray[9] === '1')
							{
								$j('#pnfpb_ic_fcm_front_cover_image_change_enable').prop('checked', true);

							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_cover_image_change_enable').prop('checked', false);
							}
								
							if (subscriptionoptionsarray[13] === '1')
							{
								$j('#pnfpb_ic_fcm_front_group_invite_enable').prop('checked', true);

							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_group_invite_enable').prop('checked', false);
							}
								
							if (subscriptionoptionsarray[12] === '1')
							{
								$j('#pnfpb_ic_fcm_front_group_details_update_enable').prop('checked', true);

							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_group_details_update_enable').prop('checked', false);
							}
							
							if ((subscriptionoptionsarray[24] && subscriptionoptionsarray[24] === '1') || subscriptionoptionsarray[0] === '1')
							{
								$j('#pnfpb_ic_fcm_front_favourite_enable').prop('checked', true);

							}
							else 
							{
								$j('#pnfpb_ic_fcm_front_favourite_enable').prop('checked', false);
							}							
							
							if (subscriptionoptionsarray.length >= 14) {

								var subscription_options_element = 14;

								for (var pnfpb_frontend_post_type_element = 0; pnfpb_frontend_post_type_element < pnfpb_custom_post_types.length; pnfpb_frontend_post_type_element++) {

									if (subscriptionoptionsarray[subscription_options_element] === '1'  || subscriptionoptionsarray[0] === '1')
									{
										$j('.pnfpb_ic_fcm_front_subscription_'+pnfpb_custom_post_types[pnfpb_frontend_post_type_element]+'_enable').prop('checked', true);

									}
									else 
									{
										$j('.pnfpb_ic_fcm_front_subscription_'+pnfpb_custom_post_types[pnfpb_frontend_post_type_element]+'_enable').prop('checked', false);

									}

									subscription_options_element++;

								}

							}							

						}
						else 
						{
							$j('.pnfpb_ic_front_push_notification_settings_messages').removeClass('success');
							$j('.pnfpb_ic_front_push_notification_settings_messages').addClass('error');
							$j('.pnfpb_ic_front_push_notification_settings_text').html(__('Error in saving notification settings',"push-notification-for-post-and-buddypress"));
							$j('.pnfpb_ic_front_push_notification_settings_messages').attr('style','display: flex !important');
						}
					})
				})
			})

    }	

	if (pnfpb_onesignal_userid) {
		var data = {
			action: 'icpushcallback',
			onesignal_externalid:pnfpbonesignalid,
			nonce: pnfpb_ajax_object_onesignal_push.nonce,			
			pushtype: 'onesignal_subscribed_users'
		};
								
		$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(responseajax) {

		
		})
	}
	
	async function pnfpb_pushSubscriptionChangeListener(event) {
  		if (event.current.token) {
    		console.log(`The push subscription has received a token!`);
    		//this is a good place to call OneSignal.login and pass in your user ID
   			await OneSignal.login(pnfpb_onesignal_userid_os);
					
			var data = {
				action: 'icpushcallback',
				onesignal_externalid:pnfpb_onesignal_userid_os,
				nonce: pnfpb_ajax_object_onesignal_push.nonce,				
				pushtype: 'onesignal_subscribed_users'
			};
							
			$j.post(pnfpb_ajax_object_onesignal_push.ajax_url, data, function(responseajax) {
						
			});	    		
  		}
	}

})
