<?php

use Gigneo\Notifications;

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

global $gigneo_args, $current_user;

$home_url             = home_url( '/' );
$profile_id           = gigneo_active_profile_id();
$profile_type         = gigneo_profile_type( $profile_id );
$user_name            = gigneo_profile_name( $profile_id );
$icon_id              = gigneo_option( '_gigneo_ts_main_icon' );
$logo_id              = gigneo_option( '_gigneo_ts_main_logo' );
$dashboard_page_id    = gigneo_option( '_gigneo_ts_template_dashboard' );
$menu_scheme          = gigneo_option( '_gigneo_ts_dashboard_menu_scheme', 'default' );
$layout_color         = gigneo_option( '_gigneo_ts_dashboard_layout_background' );
$dashboard_page_url   = get_permalink( $dashboard_page_id );
$thumbnail_id         = ! empty( $profile_id ) ? get_post_thumbnail_id( $profile_id ) : '';
$unread_notifications = Notifications::get_unread_notification_count();

$source = isset( $gigneo_args['source'] ) ? sanitize_text_field( wp_unslash( $gigneo_args['source'] ) ) : '';
$ref    = isset( $gigneo_args['ref'] ) ? sanitize_text_field( wp_unslash( $gigneo_args['ref'] ) ) : '';
?>
<div class="gn-dashboard-top-menu-wrapper">
	<div class="container">
		<!--Logo-->
		<a href="<?php echo esc_url( $home_url ); ?>" class="gn-dashboard-logo">
			<?php
			$icon_url = wp_get_attachment_image_url( $icon_id, 'full' );
			$icon_url = ! empty( $icon_url ) ? $icon_url : GIGNEO_DIR_URL . 'assets/img/gradient-icon.png';

			$logo_url = wp_get_attachment_image_url( $logo_id, 'full' );
			$logo_url = ! empty( $logo_url ) ? $logo_url : ( 'light' === $menu_scheme ? GIGNEO_DIR_URL . 'assets/img/logo-light.png' : GIGNEO_DIR_URL . 'assets/img/logo-dark.png' );
			?>
			<img class="gn-dash-icon" src="<?php echo esc_url( $icon_url ); ?>" alt="<?php echo esc_attr__( 'icon', 'gigneo' ); ?>" />
			<img class="gn-dash-logo" src="<?php echo esc_url( $logo_url ); ?>" alt="<?php echo esc_attr__( 'logo', 'gigneo' ); ?>" />
		</a>
		<!--Menu-->
		<div class="gn-dashboard-top-menu-list-wrapper">
			<div class="gn-dashboard-top-menu-list-inner">
			<?php
			$menu_list = gigneo_dashboard_menu_list( $profile_type, 'platform' );
			if ( ! empty( $menu_list ) ) {
				?>
				<ul class="gn-dashboard-top-menu">
					<?php
					foreach ( $menu_list as $slug => $menu_item ) {
						$menu_url    = $dashboard_page_url;
						$menu_url    = gigneo_dashboard_url( $menu_item['source'] ?? '', $menu_item['ref'] ?? '' );
						$menu_class  = 'gn-side-menu-' . $slug;
						$active      = ( 'dashboard' === $slug && empty( $source ) ) || $source === $menu_item['source'];
						$menu_class .= $active ? ' gn-active' : '';

						// Check if any submenu
						$has_submenu        = ! empty( $menu_item['submenu'] );
						$has_active_submenu = false;
						$menu_class        .= $has_submenu ? ' gn-has-submenu' : '';

						if ( $has_submenu ) {
							foreach ( $menu_item['submenu'] as $sub_slug => $sub_menu_item ) {
								if ( $source === $sub_menu_item['source'] && $ref === $sub_menu_item['ref'] ) {
									$has_active_submenu = true;
									break;
								}
							}
						}

						?>
						<li class="<?php echo esc_attr( $menu_class ); ?>">
							<a href="<?php echo esc_url( $menu_url ); ?>" target="<?php echo esc_attr( $menu_item['target'] ); ?>">
								<?php if ( ! empty( $menu_item['icon'] ) ) { ?>
									<i class="<?php echo esc_attr( $menu_item['icon'] ); ?>" aria-hidden="true"></i>
								<?php } ?>
								<span class="title"><?php echo esc_html( $menu_item['title'] ); ?></span>
							</a>
							<?php
							if ( $has_submenu ) {
								?>
								<ul class="gn-submenu-list">
									<?php
									foreach ( $menu_item['submenu'] as $sub_slug => $sub_menu_item ) {
										$sub_menu_url = gigneo_dashboard_url( $sub_menu_item['source'] ?? '', $sub_menu_item['ref'] ?? '' );
										$sub_active   = ( $source === $sub_menu_item['source'] && $ref === $sub_menu_item['ref'] );

										$sub_menu_class  = 'gn-submenu-' . $sub_slug;
										$sub_menu_class .= $sub_active ? ' gn-active' : '';
										$menu_class     .= $sub_active ? ' gn-submenu-expand' : '';
										?>
										<li class="<?php echo esc_attr( $sub_menu_class ); ?>">
											<a href="<?php echo esc_url( $sub_menu_url ); ?>" target="<?php echo esc_attr( $sub_menu_item['target'] ); ?>">
												<span class="title"><?php echo esc_html( $sub_menu_item['title'] ); ?></span>
											</a>
										</li>
									<?php } ?>
								</ul>
								<?php
							}
							?>
						</li>
					<?php } ?>
				</ul>
				<?php
			}
			?>
			</div>
		</div>
		<!--Actions-->
		<div class="gn-action-menu-wrapper">
			<div class="gn-responsive-menu-toggle">
				<div class="gn-icon">
					<i class="hi hi-bars-3"></i>
				</div>
			</div>
			<div class="gn-user-notifications">
				<?php do_action( 'gigneo_before_user_notification' ); ?>
				<div class="gn-user-notification">
					<div class="gn-icon">
						<i class="hi hi-bell-alert"></i>
						<span class="gn-count"><?php echo ! empty( $unread_notifications ) ? esc_html( $unread_notifications ) : ''; ?></span>
					</div>
					<?php gigneo_load_template( 'dropdown/notification' ); ?>
				</div>
				<?php do_action( 'gigneo_after_user_notification' ); ?>
			</div>
			<div class="gn-user-action-menu">
				<figure class="gn-user-avatar">
					<?php gigneo_attachment_image_src( $thumbnail_id, 'medium', 'profile' ); ?>
				</figure>
				<?php gigneo_load_template( 'dropdown/user-menu' ); ?>
			</div>
		</div>
	</div>
</div>
