<?php
/**
 * Pregnancy results.
 *
 * @link       https://belovdigital.agency
 * @since      1.0.0
 *
 * @package    ultimate-pregnancy-ovulation-calculator
 * @subpackage ultimate-pregnancy-ovulation-calculator/public/partials
 *
 * @global array $atts (input data)
 */

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

// Check if this is an IVF pregnancy.
$is_ivf     = isset( $result['isIVF'] ) && $result['isIVF'];
$ivf_method = isset( $result['ivfTransferMethod'] ) ? $result['ivfTransferMethod'] : '';
$embryo_age = isset( $result['embryoAge'] ) ? $result['embryoAge'] : 0;

// Get calculation method.
$method = $is_ivf ? 'ivf_transfer' : ( isset( $result['calculationMethod'] ) ? $result['calculationMethod'] : 'due_date' );

// Get baby size information based on current week and calculation method.
$baby_size = $ulpc->get_baby_size_by_week( $result['currentWeek'], $method );
?>

<div class="ulpc-result__pregnancy">
	<div class="ulpc-result__pregnancy-wrapper">
		<div class="ulpc-result__pregnancy-summary">

			<div class="ulpc-result__pregnancy-image">
				<img src="<?php echo esc_url( $baby_size['image'] ); ?>" alt="<?php echo esc_attr( $baby_size['name'] ); ?>">
			</div>

			<div class="ulpc-result__pregnancy-remaining-days">
				<span class="ulpc-result__pregnancy-remaining-days-label <?php echo $is_custom_font ? 'ulpc-custom-font' : ''; ?>"><?php esc_html_e( 'Days until birth:', 'ultimate-pregnancy-ovulation-calculator' ); ?></span>
				<span class="ulpc-result__pregnancy-remaining-days-value"><?php echo esc_html( $result['pregnancyRemainingDays'] ); ?></span>
			</div>


			<div class="ulpc-result__pregnancy-disclaimer">
				<?php esc_html_e( '* the results of this calculator are estimations only', 'ultimate-pregnancy-ovulation-calculator' ); ?>
			</div>



		</div>
		<div class="ulpc-result__pregnancy-content">

			<div class="ulpc-result__pregnancy-content-text">
				<?php echo esc_html( $baby_size['description'] ); ?>
			</div>

			<div class="ulpc-result__pregnancy-content-size <?php echo $is_custom_font ? 'ulpc-custom-font' : ''; ?>">
				<?php echo esc_html( $baby_size['name'] ); ?>
			</div>

			<div class="ulpc-result__pregnancy-timeline">
				<div class="ulpc-timeline">
					<div class="ulpc-timeline__visualization">
						<?php
						$trimesters = array(
							1 => array(
								'label' => __( 'T1', 'ultimate-pregnancy-ovulation-calculator' ),
								'key'   => 'first',
							),
							2 => array(
								'label' => __( 'T2', 'ultimate-pregnancy-ovulation-calculator' ),
								'key'   => 'second',
							),
							3 => array(
								'label' => __( 'T3', 'ultimate-pregnancy-ovulation-calculator' ),
								'key'   => 'third',
							),
						);

						foreach ( $trimesters as $trimester_number => $trimester_data ) :
							$is_active         = $result['currentTrimester'] === $trimester_number;
							$progress_position = ( $result['currentTrimester'] === $trimester_number )
								? $result['trimesters'][ $trimester_data['key'] ]['progress']
								: ( $result['currentTrimester'] > $trimester_number ? '100' : '0' );
							?>
							<div class="ulpc-timeline__segment ulpc-timeline__segment--<?php echo esc_attr( $trimester_data['key'] ); ?> <?php echo $is_active ? 'active' : ''; ?>">
								<?php if ( $is_active ) : ?>
									<div class="ulpc-timeline__progress-icon"  style="left: calc(<?php echo esc_attr( $progress_position ); ?>% - 14.5px);">
										<div class="ulpc-timeline__progress-icon-image">
											<img src="<?php echo esc_url( ULPC_PLUGIN_URL . 'public/images/baby-icon.png' ); ?>" alt="<?php echo esc_attr( $trimester_data['label'] ); ?>">
										</div>
										<div class="ulpc-timeline__progress-icon-line"></div>
									</div>
								<?php endif; ?>
								<div class="ulpc-timeline__progress-wrapper">
									<div class="ulpc-timeline__progress" style="width: <?php echo esc_attr( $progress_position ); ?>%"></div>
								</div>
								<div class="ulpc-timeline__label"><?php echo esc_html( $trimester_data['label'] ); ?></div>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			</div>

			<div class="ulpc-result__pregnancy-info">
				<div class="ulpc-result__pregnancy-info-start">
					<div class="ulpc-result__pregnancy-info-start-date">
						<?php
						$start_date = '';

						if ( isset( $result['conceptionDate'] ) ) {
							$start_date = $result['conceptionDate'];
						} elseif ( isset( $result['transferDate'] ) ) {
							$start_date = $result['transferDate'];
						}

						if ( ! empty( $start_date ) ) {
							echo esc_html( $start_date );
						}
						?>
					</div>
					<div class="ulpc-result__pregnancy-info-start-text <?php echo $is_custom_font ? 'ulpc-custom-font' : ''; ?>"><?php echo esc_html__( 'Start', 'ultimate-pregnancy-ovulation-calculator' ); ?></div>

				</div>

				<div class="ulpc-result__pregnancy-info-duration">
					<p class="ulpc-result__pregnancy-info-duration-weeks"><?php echo esc_html( $result['pregnantWeeksDays']['weeks'] ); ?> <?php esc_html_e( 'weeks and', 'ultimate-pregnancy-ovulation-calculator' ); ?> <?php echo esc_html( $result['pregnantWeeksDays']['days'] ); ?> <?php esc_html_e( 'days', 'ultimate-pregnancy-ovulation-calculator' ); ?></p>
					<p class="ulpc-result__pregnancy-info-duration-months"><?php echo esc_html( $result['pregnantMonthsDays']['months'] ); ?> <?php esc_html_e( 'months and', 'ultimate-pregnancy-ovulation-calculator' ); ?> <?php echo esc_html( $result['pregnantMonthsDays']['days'] ); ?> <?php esc_html_e( 'days', 'ultimate-pregnancy-ovulation-calculator' ); ?></p>
				</div>

				<div class="ulpc-result__pregnancy-info-end">
					<div class="ulpc-result__pregnancy-info-end-date">
						<?php
						$end_date = $result['dueDate'];
						echo esc_html( $end_date );
						?>
					</div>
					<div class="ulpc-result__pregnancy-info-end-text <?php echo $is_custom_font ? 'ulpc-custom-font' : ''; ?>"><?php echo esc_html__( '🏁🏆🎉 FINISH', 'ultimate-pregnancy-ovulation-calculator' ); ?></div>
				</div>

			</div>

			<div class="ulpc-result__pregnancy-footer">
				<div class="ulpc-result__pregnancy-trimester-dates">
					<p><?php esc_html_e( 'First Trimester:', 'ultimate-pregnancy-ovulation-calculator' ); ?><br> <?php echo esc_html( $result['trimesters']['first']['start'] ); ?> - <?php echo esc_html( $result['trimesters']['first']['end'] ); ?></p>
					<p><?php esc_html_e( 'Second Trimester:', 'ultimate-pregnancy-ovulation-calculator' ); ?><br> <?php echo esc_html( $result['trimesters']['second']['start'] ); ?> - <?php echo esc_html( $result['trimesters']['second']['end'] ); ?></p>
					<p><?php esc_html_e( 'Third Trimester:', 'ultimate-pregnancy-ovulation-calculator' ); ?><br> <?php echo esc_html( $result['trimesters']['third']['start'] ); ?> - <?php echo esc_html( $result['trimesters']['third']['end'] ); ?></p>
				</div>

				<div class="ulpc-result__pregnancy-progress">
					<strong><?php echo esc_html( round( ( $result['currentWeek'] / 40 ) * 100 ) ); ?>%</strong> <?php esc_html_e( 'of pregnancy completed', 'ultimate-pregnancy-ovulation-calculator' ); ?>
				</div>
			</div>

		</div>

	</div>
</div>
