<?php
/**
 * Goodmeet Google meet single Template
 *
 * Description: This template displays google meeting join links for a specific post.
 *
 * @package goodmeet
 */

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

// Get the current gmdate and time.
$goodmeet_is_schedule       = false;
$goodmeet_next_meeting_time = '';

$goodmeet_start_time       = '';
$goodmeet_meeting_duration = get_post_meta( get_the_ID(), 'goodmeet_google_meeting_duration', true );
$goodmeet_join_link        = get_post_meta( get_the_ID(), 'goodmeet_google_join_link', true );
$goodmeet_meeting_id       = get_post_meta( get_the_ID(), 'goodmeet_google_meeting_id', true );
$goodmeet_recurrence_info  = '';
$goodmeet_recurrence_data  = null;

if ( $goodmeet_options['enable_recurring_meeting'] && goodmeet_fs()->can_use_premium_code__premium_only() && function_exists( 'goodmeet_get_next_meeting_occurrence' ) ) {
	$goodmeet_recurrence_data = goodmeet_get_next_meeting_occurrence( $goodmeet_meeting_id, get_the_ID(), $goodmeet_options );
}


if ( $goodmeet_options['enable_recurring_meeting'] && goodmeet_fs()->can_use_premium_code__premium_only() && function_exists( 'goodmeet_get_next_meeting_occurrence' ) ) {
	$goodmeet_recurrence_info = goodmeet_pro_get_recurrence_description( $goodmeet_options );

	if ( ! empty( $goodmeet_recurrence_data ) && ! empty( $goodmeet_recurrence_data['next_occurrence'] ) ) {
		$goodmeet_datetime = new \DateTime( $goodmeet_recurrence_data['next_occurrence'] );
		// Get the timestamp with correct timezone handling.
		$goodmeet_start_time        = $goodmeet_datetime->format( 'Y-m-d\TH:i:s.v\Z' );
		$goodmeet_next_meeting_time = $goodmeet_recurrence_data['next_occurrence'];
		$goodmeet_gmt_offset_val    = $goodmeet_recurrence_data['recurring_gmt_offset_val'];
	} else {
		$goodmeet_start_time = gmdate( 'Y-m-d\TH:i:s.v\Z', strtotime( $goodmeet_options['startDateTime'] ) + $goodmeet_gmt_offset_val );
		if ( ( strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $goodmeet_gmt_offset_val ) - strtotime( $goodmeet_start_time ) <= 0 ) {
			$goodmeet_next_meeting_time = gmdate( 'Y-m-d\TH:i:s.v\Z', strtotime( $goodmeet_start_time ) - $goodmeet_gmt_offset_val );
			$goodmeet_is_schedule       = true;
		} else {
			$goodmeet_next_meeting_time = gmdate( 'Y-m-d\TH:i:s.v\Z', strtotime( $goodmeet_start_time ) );
		}
	}
} else {
	$goodmeet_start_time = gmdate( 'Y-m-d\TH:i:s.v\Z', strtotime( $goodmeet_options['startDateTime'] ) + $goodmeet_gmt_offset_val );

	if ( ( strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $goodmeet_gmt_offset_val ) - strtotime( $goodmeet_start_time ) <= 0 ) {
		$goodmeet_next_meeting_time = gmdate( 'Y-m-d\TH:i:s.v\Z', strtotime( $goodmeet_start_time ) - $goodmeet_gmt_offset_val );
		$goodmeet_is_schedule       = true;
	} else {
		$goodmeet_next_meeting_time = gmdate( 'Y-m-d\TH:i:s.v\Z', strtotime( $goodmeet_start_time ) );
	}
}


if ( goodmeet_fs()->can_use_premium_code__premium_only() ) {
	$goodmeet_attendee_login = 'login_successful' === $goodmeet_login_status ? true : false;
}

$goodmeet_enable_user_zone = isset( $goodmeet_options['display_time_user_zone'] ) ? $goodmeet_options['display_time_user_zone'] : '';

?>
<div class="goodmeet-wrapper goodmeet-wrapper-flex" data-goodmeet-starttime="<?php echo esc_attr( $goodmeet_next_meeting_time ); ?>" data-goodmeet_timezone="<?php echo esc_attr( $goodmeet_options['meeting_timezone'] ); ?>" data-goodmeet-title="<?php echo esc_attr( $goodmeet_meeting_title ); ?>" data-goodmeet-permalilk="<?php echo esc_url( get_permalink( $goodmeet_po_meeting_id ) ); ?>" data-goodmeet-enable-user-zone="<?php echo esc_attr( $goodmeet_enable_user_zone ); ?>" >
	<div class="<?php echo ! $goodmeet_hide_sidebar ? 'goodmeet-meeting-col' : 'goodmeet-col-12'; ?> ">

	<?php if ( 'end' !== $goodmeet_meeting_status ) { ?>
		<?php if ( ( $goodmeet_should_register && $goodmeet_attendee_login ) || current_user_can( 'manage_options' ) || ! $goodmeet_should_register ) { ?>

			<?php
			if ( ! empty( $goodmeet_thumbnail_html ) ) {
				?>
					<div class="meeting-thumbnail">
						<?php echo wp_kses_post( $goodmeet_thumbnail_html ); ?>
					</div>
				<?php
			}
			?>

			<?php
			if ( ! empty( $goodmeet_meeting_description ) ) {
				?>
					<div class="meeting-details">
						<?php echo wp_kses_post( $goodmeet_meeting_description ); ?>
					</div>
				<?php
			}
			?>

		<div class="goodmeet-google-meet-join-links">
			<a class="goodmeet-google-meet-button goodmeet-google-meet-join" target="_blank" href="<?php echo esc_url( $goodmeet_join_link ); ?>" title="JOIN NOW"><?php esc_html_e( 'JOIN NOW', 'goodmeet' ); ?></a>
		</div>

			<?php
		} else {
			require_once GOODMEET_PRO_INCLUDES . '/Templates/goodmeet-register.php';
		}
		?>
	<?php } else { ?>
				<div class="meeting-not-started">
					<?php esc_html_e( 'The meeting has ended by the host.', 'goodmeet' ); ?>
				</div>
			<?php } ?>
	</div>

	<?php
	if ( ! $goodmeet_hide_sidebar ) {
		require_once GOODMEET_INCLUDES . '/Templates/sidebars/goodmeet-google-meet-sidebar.php';
	}
	?>

</div>
