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

use Gigneo\Messages;

$order_id             = ! empty( $args['order_id'] ) ? absint( $args['order_id'] ) : 0;
$current_profile_id   = ! empty( $args['profile_id'] ) ? absint( $args['profile_id'] ) : 0;
$initial_limit        = gigneo_option( '_gigneo_ts_messages_limit', 30 );
$offset               = ! empty( $args['offset'] ) ? absint( $args['offset'] ) : 0;
$limit                = ! empty( $args['limit'] ) ? absint( $args['limit'] ) : $initial_limit - 15;
$current_profile_type = gigneo_profile_type();
$employer_id          = gigneo_post_meta( $order_id, '_gigneo_pm_profile_id' );
$freelancer_id        = gigneo_post_meta( $order_id, '_gigneo_pm_freelancer_profile_id' );
$user_profile_id      = 'employer' === $current_profile_type ? $freelancer_id : $employer_id;
$user_profile_type    = gigneo_profile_type( $user_profile_id );
$user_online_status   = gigneo_online_status( $user_profile_id );
$date_format          = get_option( 'date_format' );
$time_format          = get_option( 'time_format' );
$last_date            = null;
$order_status         = get_post_status( $order_id );
?>
<div class="gn-order-message-wrapper">
	<div class="gn-head">
		<div class="gn-author-info">
			<figure class="gn-avatar gn-<?php echo esc_attr( $user_online_status ? 'online' : 'offline' ); ?>">
				<img class="gn-user-img"
					src="<?php echo esc_url( gigneo_profile_avatar_url( $user_profile_id, $user_profile_type ) ); ?>"
					alt="<?php echo esc_html__( 'Profile Avatar', 'gigneo' ); ?>">
			</figure>
			<div class="gn-author-meta">
				<div class="gn-name-wrapper">
					<h4 class="gn-name"><?php echo esc_html( gigneo_profile_name( $user_profile_id ) ); ?></h4>
					<?php gigneo_verification_badge( $user_profile_id, true ); ?>
				</div>
				<span class="gn-status"><?php echo $user_online_status ? esc_html__( 'Online', 'gigneo' ) : esc_html__( 'Offline', 'gigneo' ); ?></span>
			</div>
		</div>
	</div>
	<div id="gn-order-messages-body" class="gn-order-messages-body gn-custom-scroll">
		<div class="gn-messages-wrapper">
			<?php
			$messages = Messages::get_messages( $current_profile_id, $user_profile_id, 'order', $limit, $offset, false, 'all', $order_id );
			if ( $messages ) {
				Messages::mark_as_read( $current_profile_id, $user_profile_id, 'order', $order_id );
				foreach ( $messages['messages'] as $message_data ) {
					$order_id      = $message_data->post_id;
					$message_id    = $message_data->id;
					$sender_id     = (int) $message_data->sender_id;
					$receiver_id   = $message_data->receiver_id;
					$message       = $message_data->filter_message;
					$read_status   = $message_data->status;
					$reply_message = $message_data->reply;
					$is_sender     = $current_profile_id === $sender_id;
					$is_read       = 'read' === $read_status;
					$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( get_option( 'date_format' ), $timestamp ) ) . '</span></div>';
						$last_date = $message_date;
					}
					$message_time = gigneo_datetime( $message_data->sent_at_gmt, $time_format );
					?>
					<div data-gn-message-id="<?php echo esc_attr( $message_data->id ); ?>"
						class="gn-message <?php echo esc_attr( $is_sender ? 'gn-sent' : 'gn-received'); ?>">
							<span class="gn-message-time <?php echo $is_read ? 'gn-read' : 'gn-unread'; ?>">
									<span class="time"><?php echo esc_attr( $message_time ); ?></span>
										<i class="hi hi-double-check"></i>
								</span>
						<div class="gn-message-wrapper">
							<?php if ( ! $is_sender ) { ?>
								<figure class="gn-user-avatar">
									<img class="gn-user-img"
										src="<?php echo esc_url( gigneo_profile_avatar_url( $user_profile_id, $user_profile_type ) ); ?>"
										alt="<?php echo esc_html__( 'Profile Avatar', 'gigneo' ); ?>">
								</figure>
							<?php } ?>
							<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"><?php echo esc_html( $reply_name ); ?> <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>
								<?php if ( ! $is_sender ) { ?>
									<div class="gn-message-reply-edit">
										<div class="gn-message-reply-action">
												<span title="reply" class="gn-message-reply"
														data-gn-reply-message="<?php echo esc_attr( $message_id ); ?>"
														data-gn-message-content="<?php echo esc_html( $message ); ?>"
														data-gn-user-name="<?php echo esc_attr( gigneo_profile_name( $user_profile_id ) ); ?>"
														data-gn-date-time="<?php echo esc_attr( $message_data->sent_at_gmt ); ?>"
												>
													<i class="hi hi-arrow-turn-down-left"></i>
												</span>
										</div>
									</div>
								<?php } ?>
							</div>
						</div>
					</div>
					<?php
				}
			}
			?>
		</div>
	</div>
	<div class="gn-footer">
		<div class="gn-send-message-form">
			<ul class="gn-reply-user-shadow-wrapper" id="gn-reply-user-shadow-wrapper">
			</ul>
			<div class="gn-message-input-wrapper">
				<input type="hidden" name="order_id" id="gn-chat-order-id" value="<?php echo esc_attr( $order_id ); ?>"/>
				<input type="hidden" name="receiver_profile_id"  id="gn-receiver-profile-id" value="<?php echo esc_attr( $user_profile_id ); ?>"/>
				<input type="hidden" name="reply_message_id" value=""/>
				<div class="gn-order-message-input-inner gn-wysiwyg-editor">
					<?php
					wp_editor(
						'',
						'gn-order-message',
						array(
							'textarea_name' => 'gn-order-message-input',
							'textarea_id'   => 'gn-order-message-input',
							'media_buttons' => false,
							'quicktags'     => false,
							'textarea_rows' => 3,
							'resize'        => false,
							'editor_class'  => 'gn-wp-editor',
							'tinymce'       => array(
								'toolbar1'                => 'bold italic bullist numlist link unlink',
								'toolbar2'                => '',
								'plugins'                 => 'lists link',
								'directionality' => is_rtl() ? 'rtl' : 'ltr',
								'forced_root_block'       => false,
								'force_br_newlines'       => true,
								'force_p_newlines'        => false,
								'convert_newlines_to_brs' => true,
								'valid_elements'          => 'strong/b,em/i,ul,ol,li,a[href|target],br,hr',
								'invalid_elements'        => 'p,pre,h1,h2,h3,h4,h5,h6,img',
								'content_style'           => 'body { font-size: 14px; background-color: transparent; font-family: var(--gigneo-primary-font,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji) }',
							),
						)
					);
					?>
				</div>
				<div class="gn-reply-message-actions" >
					<button id="gn-message-wysiwyg" type="button" class="gn-action-btn">
						<i class="hi hi-pencil-edit" aria-hidden="true"></i>
					</button>
					<button id="gn-send-message-btn" type="button" class="gn-send-message-btn">
						<i class="hi hi-paper-airplane" aria-hidden="true"></i>
					</button>
				</div>
			</div>
		</div>
	</div>
</div>
