<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

use Gigneo\Notifications;

$profile_id   = gigneo_active_profile_id();
$profile_type = gigneo_profile_type( $profile_id );

$notification_data = Notifications::get_notifications();
$notifications     = $notification_data['notifications'] ?? array();
?>
<div class="gn-notification-dropdown">
	<div class="gn-head">
		<h6 class="gn-title"><?php echo esc_html__( 'Notifications', 'gigneo' ); ?></h6>
		<p class="gn-description"><?php echo esc_html__( 'View recent notifications', 'gigneo' ); ?></p>
	</div>
	<div class="gn-body">
		<?php
		if ( ! empty( $notifications ) ) {
			?>
			<div class="gn-notifications gn-custom-scroll">
				<?php
				foreach ( $notifications as $notification ) {
					$notification_id         = $notification['id'] ?? '';
					$notification_title      = $notification['title'] ?? '';
					$notification_content    = $notification['content'] ?? '';
					$notification_category   = $notification['category'] ?? '';
					$notification_type       = $notification['type'] ?? '';
					$notification_status     = $notification['status'] ?? '';
					$notification_meta       = $notification['meta'] ?? '';
					$notification_profile_id = $notification['profile_id'] ?? '';
					$notification_post_id    = $notification['post_id'] ?? '';
					$notification_created_at = $notification['created_at_gmt'] ?? '';

					?>
					<div id="gn-notification-in-line-<?php echo esc_attr( $notification['id'] ); ?>" class="gn-notification-item gn-type-<?php echo esc_attr( $notification_type ); ?> gn-<?php echo esc_attr( $notification_status ); ?>">
						<?php
						//Avatar
						$participant_profile_thumbnail_id = Notifications::avatar( $notification_category, $notification_type, $notification_profile_id, $notification_post_id );
						if ( false !== $participant_profile_thumbnail_id ) {
							$is_online = gigneo_online_status( $notification_profile_id );
							?>
							<figure class="gn-avatar gn-<?php echo esc_attr( $is_online ? 'online' : 'offline' ); ?>">
								<?php gigneo_attachment_image_src( $participant_profile_thumbnail_id ?? '', 'thumbnail', 'profile' ); ?>
							</figure>
							<?php
						} else {
							$icon_data  = Notifications::icon( $notification_type );
							$icon       = $icon_data['icon'] ?? 'hi hi-exclamation-circle';
							$icon_class = $icon_data['status'] ?? 'general';
							?>
							<span class="gn-icon gn-status-<?php echo esc_attr( $icon_class ); ?>">
								<i class="<?php echo esc_html( $icon ); ?>" aria-hidden="true"></i>
							</span>
						<?php } ?>
						<div class="gn-info">
							<div class="gn-head">
								<h6 class="gn-title"><?php echo esc_html( $notification_title ); ?></h6>
								<div class="gn-meta">
									<?php
									$notification_button = Notifications::link( $notification_category, $notification_type, $notification_profile_id, $notification_post_id );
									if ( ! empty( $notification_button['link'] ) ) {
										?>
										<a href="<?php echo esc_url( $notification_button['link'] ); ?>"
											target="<?php echo esc_attr( $notification_button['target'] ?? '_self' ); ?>"
											class="gn-btn gn-notification-link">
											<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
										</a>
									<?php } ?>
									<button type="button"
											class="gn-btn gn-btn-outline-grey gn-dropdown-notification-mark-read"
											data-gn-status="<?php echo esc_attr( 'read' === $notification_status ? 'unread' : 'read' ); ?>"
											data-gn-id="<?php echo esc_attr( $notification['id'] ); ?>">
										<i class="hi hi-envelope" aria-hidden="true"></i>
									</button>
								</div>
							</div>
							<div class="gn-body">
								<p class="gn-description"><?php echo esc_html( wp_html_excerpt( $notification_content, 100, '...' ) ); ?></p>
							</div>
							<div class="gn-footer">
								<?php
								$notification_date = Notifications::date( $notification_created_at, $notification_profile_id );
								if ( ! empty( $notification_date ) ) {
									?>
									<span class="gn-date"><?php echo esc_html( $notification_date ); ?></span>
									<?php
								}
								?>
							</div>
						</div>
					</div>
				<?php } ?>
			</div>
			<div class="gn-actions">
				<a href="<?php echo esc_url( gigneo_dashboard_url( 'notification', 'all' ) ); ?>" class="gn-btn gn-btn-outline-light gn-btn-sm gn-btn-block">
					<i class="hi hi-eye" aria-hidden="true"></i>
					<?php echo esc_html__( 'View all notifications', 'gigneo' ); ?>
				</a>
			</div>
			<?php
		} else {
			$not_found_args = array(
				'title'       => esc_html__( 'No notifications', 'gigneo' ),
				'description' => esc_html__( 'You’re all caught up! Check back later for new notifications', 'gigneo' ),
			);
			gigneo_not_found( 'notification_dropdown', $not_found_args );
		}
		?>
	</div>
</div>
