<?php

use Gigneo\Modules;

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

$service_id         = $args['service_id'] ?? get_the_ID();
$active_profile_id  = $args['profile_id'] ?? '';
$saved_items        = $args['saved_items'] ?? array();
$country_state_list = $args['country_state_list'] ?? array();

$service_title        = get_the_title( $service_id );
$service_link         = get_permalink( $service_id );
$service_thumbnail_id = get_post_thumbnail_id( $service_id );
$service_status       = get_post_status( $service_id );

$service_price      = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_price' );
$service_type       = gigneo_post_meta( $service_id, '_gigneo_pm_type' );
$service_gallery    = gigneo_post_meta( $service_id, '_gigneo_pm_gallery' );
$service_video_type = gigneo_post_meta( $service_id, '_gigneo_pm_video_type' );

//Video
$service_video_link = '';
if ( 'link' === $service_video_type ) {
	$service_video_link = gigneo_post_meta( $service_id, '_gigneo_pm_video_link' );
} else {
	$service_video      = gigneo_post_meta( $service_id, '_gigneo_pm_video' );
	$service_video_link = wp_get_attachment_url( $service_video ) ?? '';
}

$author_id        = get_post_field( 'post_author', $service_id );
$profile_id       = gigneo_profile_id( $author_id, 'freelancer' );
$author_avatar_id = get_post_thumbnail_id( $profile_id ) ?? 0;
$online_status    = gigneo_online_status( $profile_id );
$is_featured      = gigneo_post_meta( $service_id, '_gigneo_pm_featured' );

$author_profile_average_rating = gigneo_profile_rating( $profile_id, 'average' );
$author_profile_reviews        = gigneo_profile_rating( $profile_id );
$delivery_time                 = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_delivery_time' );
$delivery_time_type            = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_delivery_time_type' );
$delivery_time_label           = gigneo_delivery_time( $delivery_time, $delivery_time_type );

$locations = gigneo_post_meta( $service_id, '_gigneo_pm_locations' );
$locations = is_array( $locations ) ? $locations : array();
$card_lat  = '';
$card_lng  = '';
if ( ! empty( $locations ) && is_array( $locations ) ) {
	$first_loc = $locations[0]['location'][0] ?? $locations[0]['location'] ?? array();
	$card_lat  = $first_loc['lat'] ?? '';
	$card_lng  = $first_loc['lng'] ?? '';
}

// Save state
$active_class = ! empty( $saved_items && in_array( $service_id, $saved_items, true ) ) ? 'gn-saved' : '';
?>
<div class="gn-service-card gn-service-card-layout-1"
	data-item-id="<?php echo esc_attr( $service_id ); ?>" data-lat="<?php echo esc_attr( $card_lat ); ?>"
	data-lng="<?php echo esc_attr( $card_lng ); ?>">
	<div class="gn-head">
		<div class="gn-slick-slider gn-slick-theme gn-service-media-slider">
			<div class="gn-slick-slide">
				<figure class="gn-service-media">
					<?php gigneo_attachment_image_src( $service_thumbnail_id, 'large' ); ?>
					<?php if ( ! empty( $service_video_link ) ) { ?>
						<a href="<?php echo esc_url( $service_video_link ); ?>" data-gn-fancybox
							class="gn-play-video-toggle">
							<i class="hi hi-play" aria-hidden="true"></i>
						</a>
					<?php } ?>
				</figure>
			</div>
			<?php
			if ( ! empty( $service_gallery ) && is_array( $service_gallery ) ) {
				foreach ( $service_gallery as $gallery_item ) {
					if ( intval( $gallery_item ) === intval( $service_thumbnail_id ) ) {
						continue;
					}
					?>
					<div class="gn-slick-slide">
						<figure class="gn-service-media"><?php gigneo_attachment_image_src( $gallery_item, 'large' ); ?>
						</figure>
					</div>
					<?php
				}
			}
			?>
		</div>
		<?php if ( $is_featured ) { ?>
			<div class="gn-tags">
				<span class="gn-status-tag-fill gn-warning"><?php echo esc_html__( 'Featured', 'gigneo' ); ?></span>
			</div>
		<?php } ?>
		<button class="gn-save-btn gn-btn <?php echo esc_attr( $active_class ); ?>"
				data-gn-id="<?php echo intval( $service_id ); ?>"
				data-gn-type="<?php echo esc_attr( 'service' ); ?>"
				aria-label="<?php esc_attr_e( 'Save', 'gigneo' ); ?>">
			<?php gigneo_saved_icon( true ); ?>
		</button>
	</div>
	<div class="gn-body">
		<h2 class="gn-title gn-two-line-ellipsis">
			<a href="<?php echo esc_url( $service_link ); ?>"><?php echo esc_html( $service_title ); ?></a>
		</h2>
		<div class="gn-price">
			<span class="gn-label"><?php echo esc_html__( 'Starting from', 'gigneo' ); ?></span>
			<strong class="gn-value"><?php echo esc_html( gigneo_price_format( $service_price, true ) ); ?></strong>
		</div>
		<div class="gn-meta">
			<div class="gn-delivery">
				<i class="hi hi-clock" aria-hidden="true"></i>
				<?php echo esc_html( $delivery_time_label ); ?>
			</div>
			<div class="gn-type">
				<i class="hi hi-map-pin" aria-hidden="true"></i>
				<?php echo 'onsite' === $service_type ? esc_html__( 'Onsite', 'gigneo' ) : esc_html__( 'Remote', 'gigneo' ); ?>
			</div>
			<?php if ( count( $locations ) === 1 ) { ?>
				<div class="gn-location">
					<i class="hi hi-map" aria-hidden="true"></i>
					<?php
					$loc       = $locations[0] ?? array();
					$city      = get_term_by( 'slug', $loc['city'] ?? '', 'gigneo_city' );
					$city_name = ( $city && ! is_wp_error( $city ) ) ? $city->name : '';

					$country_state_key = $loc['country_state'] ?? '';
					$label             = implode( ', ', array_filter( array( $city_name, $country_state_list[ $country_state_key ] ?? '' ) ) );
					echo esc_html( $label );
					?>
				</div>
				<?php
			} elseif ( count( $locations ) > 1 ) {
				?>
				<div class="gn-location">
					<i class="hi hi-map" aria-hidden="true"></i>
					<?php
					printf(
					// translators: %d is the number of locations.
						esc_html( _n( '%d Location', '%d Locations', count( $locations ), 'gigneo' ) ),
						count( $locations )
					);

					?>
				</div>
			<?php } ?>
		</div>
	</div>
	<div class="gn-footer">
		<div class="gn-profile">
			<figure class="gn-avatar gn-<?php echo esc_attr( $online_status ? 'online' : 'offline' ); ?>">
				<?php gigneo_attachment_image_src( $author_avatar_id, 'thumbnail', 'profile', '', '', $profile_id ); ?>
			</figure>
			<div class="gn-info">
				<div class="gn-name-wrapper">
					<a href="<?php echo esc_url( get_the_permalink( $profile_id ) ); ?>" target="_blank"
						class="gn-name"><?php echo esc_html( gigneo_profile_name( $profile_id ) ); ?>
					</a>
					<?php gigneo_verification_badge( $profile_id, true ); ?>
				</div>
				<p class="gn-ratings">
					<i class="hi hi-star-alt" aria-hidden="true"></i>
					<?php echo esc_html( $author_profile_average_rating ?? 0 ); ?>
					<span class="gn-count">(<?php echo esc_html( $author_profile_reviews ?? 0 ); ?>)</span>
				</p>
			</div>
		</div>
		<div class="gn-actions">
			<?php do_action( 'gigneo_action_buttons', array( 'member_id' => $profile_id, 'profile_id' => $active_profile_id, 'context' => 'grid' ) ); ?>
			<a href="<?php echo esc_url( get_the_permalink( $service_id ) ); ?>" class="gn-btn gn-btn-outline-grey">
				<i class="hi hi-eye" aria-hidden="true"></i>
			</a>
		</div>
	</div>
</div>