<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $current_user;

$verification_email = isset($campoal_options['conikal_verification_email_field']) ? $campoal_options['conikal_verification_email_field'] : 0;
$user_account_page = isset($campoal_options['conikal_page_user_account_field']) ? $campoal_options['conikal_page_user_account_field'] : 0;
$verify_notification_position = isset($campoal_options['conikal_verify_notification_position_field']) ? $campoal_options['conikal_verify_notification_position_field'] : 'bottom';
$verify_notification_closable = isset($campoal_options['conikal_verify_notification_closable_field']) ? $campoal_options['conikal_verify_notification_closable_field'] : 1;
$verify_notification_style = isset($campoal_options['conikal_verify_notification_style_field']) ? $campoal_options['conikal_verify_notification_style_field'] : 'toast';
$toast_update_profile = isset($campoal_options['conikal_toast_update_profile_field']) ? $campoal_options['conikal_toast_update_profile_field'] : 0;
$toast_set_password = isset($campoal_options['conikal_toast_set_password_field']) ? $campoal_options['conikal_toast_set_password_field'] : 0;
$toast_welcome = isset($campoal_options['conikal_toast_welcome_field']) ? $campoal_options['conikal_toast_welcome_field'] : 0;
$toast_welcome_title = isset($campoal_options['conikal_toast_welcome_title_field']) ? $campoal_options['conikal_toast_welcome_title_field'] : 0;
$toast_welcome_message = isset($campoal_options['conikal_toast_welcome_message_field']) ? $campoal_options['conikal_toast_welcome_message_field'] : 0;
$toast_welcome_button_url = isset($campoal_options['conikal_toast_welcome_button_url_field']) ? $campoal_options['conikal_toast_welcome_button_url_field'] : 0;
$toast_welcome_button_text = isset($campoal_options['conikal_toast_welcome_button_text_field']) ? $campoal_options['conikal_toast_welcome_button_text_field'] : 0;

// get user data from verify code if user not logged in
if (isset($_GET['verify'])) {
	$user_id    = sanitize_text_field($_GET['id']);
    $user_code  = sanitize_text_field($_GET['code']);
	if (!is_user_logged_in()) {
		$current_user = get_user_by('ID', $user_id);
	}
}

$user_account_page = ($user_account_page ? get_permalink($user_account_page) : get_site_url());
$account_activation = get_user_meta($current_user->ID, 'is_activated', true);

$temporary_pass = get_user_meta($current_user->ID, 'user_temporary_pass', true);
$profile_progress = get_user_meta($current_user->ID, 'user_profile_progress', true);
$welcome_seen = get_user_meta($current_user->ID, 'user_welcome_seen', true);

// cookie remind toast
$remind_email_verify = isset($_COOKIE['conikal_toast_remind_email_verify']) ? $_COOKIE['conikal_toast_remind_email_verify'] : 0;
$remind_update_profile = isset($_COOKIE['conikal_toast_remind_update_profile']) ? $_COOKIE['conikal_toast_remind_update_profile'] : 0;
$remind_set_password = isset($_COOKIE['conikal_toast_remind_set_password']) ? $_COOKIE['conikal_toast_remind_set_password'] : 0;
?>

<?php if ($remind_email_verify != 1 && $verify_notification_style == 'toast' && $verification_email != 0 && $account_activation != 1 || isset($_GET['verify']) ) : ?>
	<div class="ui text <?php echo esc_attr((!empty($account_activation) || isset($_GET['verify'])) ? 'success' : 'primary') ?> toast" id="toast-email-verify">
		<?php if (!empty($account_activation) || isset($_GET['verify'])) { ?>
		    <div class="content">
				<div class="ui small header">
					<i class="check circle icon"></i>
					<div class="content">
						<?php esc_html_e('Your email has been verified!', 'campoal'); ?>
						<div class="sub header">
							<?php echo sprintf(esc_html__('Now you can use your account %1$s for all the feature on %2$s without limits.', 'campoal'), $current_user->user_login, get_bloginfo('name')); ?>
						</div>
					</div>
				</div>
			</div>
			<?php } else { ?>
			<div class="content">
		    	<div class="ui small header">
		    		<i class="mail icon"></i>
		    		<div class="content">
			    		<?php esc_html_e('Your account is unverified!', 'campoal'); ?>
			    		<div class="sub header">
			    			<?php echo sprintf(esc_html__('Check your inbox and confirm %1$s for access to all things on %2$s.', 'campoal'), $current_user->user_email, get_bloginfo('name')); ?>
			    		</div>
			    	</div>
		    	</div>
	    		<div class="left basic actions">
		          	<button class="ui positive basic tiny button" id="resend-email"><?php esc_html_e('Resend', 'campoal') ?></button>
		          	<?php if ($verify_notification_closable) : ?>
		          	<button class="ui negative basic tiny button"><?php esc_html_e('Later', 'campoal') ?></button>
		          	<?php endif; ?>
		          	<a href="<?php echo esc_url($user_account_page) ?>" class="ui tertiary tiny basic button"><?php esc_html_e('Change email', 'campoal') ?></a>
		      	</div>
		    </div>
		   	<?php wp_nonce_field('resend_ajax_nonce', 'securityResendVerification', true); ?>
		   	<i class="close icon"></i>
		<?php } ?>
	</div>

	<?php if (!empty($account_activation) || isset($_GET['verify'])) {
		// add temporary to really signatures
		$signature_temporary = conikal_temporary_to_really_signature($current_user->ID);

		if ($signature_temporary) {
			$temporary_title 	= '';
			$temporary_link 	='';
			foreach ($signature_temporary as $id => $signature) {
				$temporary_title 	= get_the_title($id);
				$temporary_link 	= get_permalink($id);
			}
	?>
	<div class="ui text <?php echo esc_attr((!empty($account_activation) || isset($_GET['verify'])) ? 'success' : 'primary') ?> toast" id="toast-petition-signed">
		<div class="content">
				<div class="ui small header">
					<i class="check circle icon"></i>
					<div class="content">
						<?php esc_html_e('A petition has been signed!', 'campoal'); ?>
						<div class="sub header">
							<?php echo sprintf(esc_html__('A previously signed petition queue "%1$s" has been confirmed on %2$s.', 'campoal'), $temporary_title, get_bloginfo('name')); ?>
						</div>
					</div>
				</div>
				<?php if ($temporary_link) : ?>
				<div class="left basic actions">
					<a href="<?php echo esc_url($temporary_link) ?>" class="ui positive basic tiny button"><?php esc_html_e('Open Petition', 'campoal') ?></a>
				</div>
				<?php endif; ?> 
			</div>
	</div>
	<?php } 
	} ?>

	<div class="ui success toast" id="toast-resend-email-success">
		<div class="content">
			<div class="ui small header">
	    		<i class="check circle icon"></i>
	    		<div class="content" id="resend-email-content">
		    		<?php esc_html_e('Sent success!', 'campoal'); ?>
		    		<div class="sub header">
		    			<?php esc_html_e('A new verification email was sent.', 'campoal') ?>
		    		</div>
		    	</div>
	    	</div>
		</div>
	</div>

	<div class="ui failed toast" id="toast-resend-email-failed">
		<div class="content">
			<div class="ui small header">
	    		<i class="times circle icon"></i>
	    		<div class="content" id="resend-email-content">
		    		<?php esc_html_e('Sent failed!', 'campoal'); ?>
		    		<div class="sub header">
		    			<?php esc_html_e('A new verification email was failed.', 'campoal') ?>
		    		</div>
		    	</div>
	    	</div>
		</div>
	</div>
<?php endif; ?>

<?php if ($remind_update_profile != 1 && $toast_update_profile && is_array($profile_progress)) : ?>
	<?php if (!is_page_template('user-account.php')) : ?>
		<div class="ui primary toast" id="toast-update-profile">
			<div class="content">
				<div class="ui small header">
		    		<i class="address card icon"></i>
		    		<div class="content" id="resend-email-content">
			    		<?php esc_html_e('Profile information is missing!', 'campoal'); ?>
			    		<div class="sub header">
			    			<?php echo sprintf(esc_html__('Go to your profile and complete the remaining %1$d of %2$d information.', 'campoal'), (int)$profile_progress['goal'] - (int)$profile_progress['completed'], $profile_progress['goal']) ?>
			    		</div>
			    	</div>
		    	</div>
			</div>
			<div class="left basic actions">
				<a href="<?php echo esc_url($user_account_page) ?>" class="ui positive basic tiny button"><?php esc_html_e('Update profile', 'campoal') ?></a>
		      	<button class="ui negative basic tiny button"><?php esc_html_e('Later', 'campoal') ?></button>
		  	</div>
		  	<i class="close icon"></i>
		</div>
	<?php else : ?>
		<div class="ui success toast" id="toast-update-profile-success">
			<div class="content">
				<div class="ui small header">
		    		<i class="check circle icon"></i>
		    		<div class="content">
			    		<?php esc_html_e('Your profile is completed!', 'campoal'); ?>
			    		<div class="sub header">
			    			<?php esc_html_e('You have completed the profile, now start signing the petitions.', 'campoal') ?>
			    		</div>
			    	</div>
		    	</div>
			</div>
		</div>
	<?php endif; ?>
<?php endif; ?>

<?php if (is_user_logged_in() && $remind_set_password != 1 && $toast_set_password && $temporary_pass == true) : ?>
	<div class="ui primary toast" id="toast-set-password">
		<div class="content">
			<div class="ui small header">
	    		<i class="key icon"></i>
	    		<div class="content" id="resend-email-content">
		    		<?php esc_html_e('Password has not been set!', 'campoal'); ?>
		    		<div class="sub header">
		    			<?php esc_html_e('Password is randomly generated, retrieve your password for next login.', 'campoal') ?>
		    		</div>
		    	</div>
	    	</div>
		</div>
		<div class="left basic actions">
			<button class="ui positive basic tiny button" id="set-password-btn"><?php esc_html_e('Set password', 'campoal') ?></button>
	      	<button class="ui negative basic tiny button"><?php esc_html_e('Later', 'campoal') ?></button>
	  	</div>
	  	<i class="close icon"></i>
	</div>
	<div class="ui success toast" id="toast-reset-pass-success">
		<div class="content">
			<div class="ui small header">
	    		<i class="check circle icon"></i>
	    		<div class="content">
		    		<?php esc_html_e('Password has been set!', 'campoal'); ?>
		    		<div class="sub header">
		    			<?php esc_html_e('Your new password has been set successfully!', 'campoal') ?>
		    		</div>
		    	</div>
	    	</div>
		</div>
	</div>
	<?php get_template_part('templates/forgot-password'); ?>
<?php endif; ?>

<?php if (is_user_logged_in() && !empty($account_activation) && !is_array($profile_progress) && !$temporary_pass) : ?>
	<?php if (!$welcome_seen && $toast_welcome) : ?>
		<div class="ui success toast" id="toast-welcome">
			<div class="content">
				<div class="ui small header">
		    		<i class="smile icon"></i>
		    		<div class="content" id="resend-email-content">
			    		<?php echo sprintf(esc_html($toast_welcome_title), $current_user->display_name, get_bloginfo('name')); ?>
			    		<div class="sub header">
			    			<?php echo sprintf(esc_html($toast_welcome_message), $current_user->display_name, get_bloginfo('name')); ?>
			    		</div>
			    	</div>
		    	</div>
			</div>
			<div class="left basic actions">
				<a href="<?php echo esc_url($toast_welcome_button_url) ?>" class="ui positive basic tiny button"><?php echo esc_html($toast_welcome_button_text) ?></a>
		      	<button class="ui negative basic tiny button"><?php esc_html_e('Got it!', 'campoal') ?></button>
		  	</div>
		  	<i class="close icon"></i>
		</div>
	<?php endif; ?>
	<?php wp_nonce_field('notificaiton_seen_ajax_nonce', 'securityNotificationSeen', true); ?>
<?php endif; ?>