<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$job_id               = ! empty( $args['job_id'] ) ? $args['job_id'] : 0;
$proposal_id          = ! empty( $args['proposal_id'] ) ? $args['proposal_id'] : 0;
$profile_id           = gigneo_active_profile_id();
$user_type            = gigneo_profile_type( $profile_id );
$pref_location        = gigneo_post_meta( $job_id, '_gigneo_pm_preferred_location' );
$pref_timezone        = gigneo_post_meta( $job_id, '_gigneo_pm_preferred_timezone' );
$pref_language        = gigneo_post_meta( $job_id, '_gigneo_pm_preferred_language' );
$pref_language        = ! empty( $pref_language ) ? get_term( $pref_language, 'gigneo_language' ) : '';
$language_proficiency = gigneo_post_meta( $job_id, '_gigneo_pm_language_proficiency' );

$pref_country  = is_array( $pref_location ) ? ( $pref_location[0] ?? '' ) : $pref_location;
$countries     = gigneo_allowed_countries();
$pref_location = $countries[ $pref_country ] ?? $pref_country;
if ( ! empty( $pref_location ) || ! empty( $pref_timezone ) || ! empty( $pref_language ) ) {
	?>
	<div class="gn-section gn-preferences-section">
		<div class="gn-head">
			<h2 class="gn-title"><?php echo esc_html__( 'Preferences', 'gigneo' ); ?></h2>
		</div>
		<div class="gn-body">
			<ul class="gn-preferences">
				<?php if ( ! empty( $pref_language ) ) { ?>
					<li class="gn-info gn-language">
						<span class="gn-title"><?php echo esc_html__( 'Language', 'gigneo' ); ?></span>
						<span class="gn-value">
							<?php
								echo esc_html( $pref_language->name ?? '' );
							if ( ! empty( $language_proficiency ) ) {
								echo ' (' . esc_html( $language_proficiency ) . ')';
							}
							?>
							<?php
							if ( 'freelancer' === $user_type ) {
								$active_language = 'hi-x-mark';
								if ( has_term( $pref_language->term_id, 'gigneo_language', $profile_id ) ) {
									$active_language = 'hi-check';
									if ( ! empty( $language_proficiency ) ) {
										$profile_languages = gigneo_post_meta( $profile_id, '_gigneo_pm_languages' );
										if ( ! empty( $profile_languages ) && is_array( $profile_languages ) ) {
											foreach ( $profile_languages as $lang ) {
												if ( is_array( $lang ) && isset( $lang['name'] ) && strtolower( $lang['name'] ) === strtolower( $pref_language->name ) && isset( $lang['level'] ) && strtolower( $lang['level'] ) === strtolower( $language_proficiency ) ) {
													$active_language = 'hi-check';
													break;
												} else {
													$active_language = 'hi-x-mark';
												}
											}
										}
									}
								}
								?>
								<i class="hi <?php echo esc_attr( $active_language ); ?>" aria-hidden="true"></i>
							<?php } ?>
						</span>
					</li>
					<?php
				}
				?>
				<?php if ( ! empty( $pref_timezone && in_array( 'timezone', gigneo_active_modules(), true ) ) ) { ?>
					<li class="gn-info gn-timezone">
						<span class="gn-title"><?php echo esc_html__( 'Timezone', 'gigneo' ); ?></span>
						<span class="gn-value">
							<?php echo esc_html( $pref_timezone ?? '' ); ?>
							<?php
							if ( 'freelancer' === $user_type ) {
								$profile_timezone = gigneo_timezone( $profile_id );
								$active_timezone  = $profile_timezone === $pref_timezone ? 'hi-check' : 'hi-x-mark';
								?>
								<i class="hi <?php echo esc_attr( $active_timezone ); ?>" aria-hidden="true"></i>
							<?php } ?>
						</span>
					</li>
				<?php } ?>
				<?php if ( ! empty( $pref_location ) ) { ?>
					<li class="gn-info gn-timezone">
						<span class="gn-title"><?php echo esc_html__( 'Location', 'gigneo' ); ?></span>
						<span class="gn-value">
							<?php echo esc_html( $pref_location ?? '' ); ?>
							<?php
							if ( 'freelancer' === $user_type ) {
								$profile_location = gigneo_post_meta( $profile_id, '_gigneo_pm_country_state' );
								$country_state    = ! empty( $profile_location ) ? explode( ':', (string) $profile_location, 2 ) : array();
								$country_state    = ! empty( $country_state[0] ) ? $country_state[0] : '';
								$active_location  = ! empty( $pref_country ) && $country_state === $pref_country ? 'hi-check' : 'hi-x-mark';
								?>
								<i class="hi <?php echo esc_attr( $active_location ); ?>" aria-hidden="true"></i>
							<?php } ?>
						</span>
					</li>
				<?php } ?>
			</ul>
		</div>
	</div>
	<?php
}