<?php

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

use Gigneo\Messages;

$order_id = $args['p_id'] ?? '';
if ( ! $order_id ) {
	return;
}
$freelancer_profile_id = $args['freelancer_profile_id'] ?? '';
$employer_profile_id   = $args['profile_id'] ?? '';
$date_format           = $args['date_format'] ?? '';
$time_format           = $args['time_format'] ?? '';


$initial_limit  = gigneo_option( '_gigneo_ts_messages_limit', 30 );
$message_offset = ! empty( $args['offset'] ) ? absint( $args['offset'] ) : 0;
$message_limit  = ! empty( $args['limit'] ) ? absint( $args['limit'] ) : $initial_limit - 15;
$last_date      = null;
?>
<div class="gn-order-message-wrapper">
	<div class="gn-head">
		<div class="gn-author-info">
			<div class="gn-avatars">
				<figure class="gn-avatar">
					<img class="gn-user-img"
						src="<?php echo esc_url( gigneo_profile_avatar_url( $freelancer_profile_id, 'freelancer' ) ); ?>"
						alt="<?php echo esc_html__( 'Profile Avatar', 'gigneo' ); ?>">
				</figure>
				<figure class="gn-avatar">
					<img class="gn-user-img"
						src="<?php echo esc_url( gigneo_profile_avatar_url( $employer_profile_id, 'employer' ) ); ?>"
						alt="<?php echo esc_html__( 'Profile Avatar', 'gigneo' ); ?>">
				</figure>
			</div>
			<div class="gn-author-meta">
				<h4 class="gn-name">
					<?php
					echo esc_html( gigneo_profile_name( $freelancer_profile_id ) );
					gigneo_verification_badge( $freelancer_profile_id, true );
					?>
					<span class="separator"> / </span>
					<?php
					echo esc_html( gigneo_profile_name( $employer_profile_id ) );
					gigneo_verification_badge( $employer_profile_id, true );
					?>
				</h4>
			</div>
		</div>
	</div>
	<div id="gn-order-messages-body" class="gn-order-messages-body gn-custom-scroll">
		<div class="gn-messages-wrapper">
			<?php
			$message_list_class = Messages::class;
			$message_list       = $message_list_class::get_messages( $freelancer_profile_id, $employer_profile_id, 'order', $message_limit, $message_offset, false, 'all', $order_id );
			if ( ! empty( $message_list ) && ! empty( $message_list['total'] ) ) {
				foreach ( $message_list['messages'] as $message_data ) {
					$order_id      = $message_data->post_id;
					$message_id    = $message_data->id;
					$sender_id     = (int) $message_data->sender_id;
					$profile_type  = gigneo_profile_type( $sender_id );
					$receiver_id   = $message_data->receiver_id;
					$message       = $message_data->message;
					$reply_message = $message_data->reply;
					$message_date  = gmdate( 'Y-m-d', strtotime( $message_data->sent_at ) );
					if ( $last_date !== $message_date ) {
						$timestamp = strtotime( $message_date );
						echo '<div class="gn-message-time-stemp"><span class="gn-date-time">' . esc_html( wp_date( $date_format, $timestamp ) ) . '</span></div>';
						$last_date = $message_date;
					}
					$datetime_format = $time_format;
					$message_time    = gigneo_datetime( $message_data->sent_at_gmt );
					$message_time    = mysql2date( $datetime_format, $message_time );
					?>
					<div data-gn-message-id="<?php echo esc_attr( $message_data->id ); ?>"
						class="gn-message gn-<?php echo esc_html( gigneo_profile_type( $sender_id ) ); ?>">
							<span class="gn-message-time">
								<span class="name"><?php echo esc_html( gigneo_profile_name( $sender_id ) ); ?> (<?php echo esc_html( 'freelancer' === gigneo_profile_type( $sender_id ) ? gigneo_freelancer_singular() : gigneo_employer_singular() ); ?>)</span>
								<span class="time"><?php echo esc_attr( $message_time ); ?></span>
								</span>
						<div class="gn-message-wrapper">
							<figure class="gn-user-avatar">
								<img class="gn-user-img"
									src="<?php echo esc_url( gigneo_profile_avatar_url( $sender_id, gigneo_profile_type( $sender_id ) ) ); ?>"
									alt="<?php echo esc_html__( 'Profile Avatar', 'gigneo' ); ?>">
							</figure>
							<div class="gn-content">
								<?php
								if ( ! empty( $reply_message ) ) {
									$reply_name = gigneo_profile_name( $reply_message['sender_id'] );
									?>
									<div class="gn-message-reply-shadow">
										<div class="gn-reply-content">
											<p class="gn-info">
												<span class="gn-name"><?php echo esc_html( $reply_name ); ?></span> <span class="gn-date"><?php echo esc_html( $reply_message['formatted_time'] ); ?></span>
											</p>
											<p class="gn-reply-text">
												<?php
												echo esc_html( wp_trim_words( wp_strip_all_tags( $reply_message['content'] ), 5, '..' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
												?>
											</p>
										</div>
									</div>
								<?php } ?>
								<div class="gn-message-text">
									<pre class="gn-pre"><?php echo wp_kses( $message, gigneo_allowed_html_tags() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></pre>
								</div>
							</div>
						</div>
					</div>
					<?php
				}
			} else {
				?>
				<div class="gn-messages gn-no-message">
					<figure class="gn-empty-icon">
						<img class="gn-icon" src="<?php echo esc_url( GIGNEO_DIR_URL . 'assets/img/icon/chat.png' ); ?>" alt="<?php esc_attr__( 'No Responses', 'gigneo' ); ?>">
					</figure>
					<h4 class="gn-order-chat-heading"><?php echo esc_html__( 'No messages exchanged', 'gigneo' ); ?></h4>
					<p class="gn-order-chat-info"><?php echo esc_html__( 'Order chat messages will shown here.', 'gigneo' ); ?></p>
				</div>
			<?php } ?>
		</div>
	</div>
</div>
