<?php
/**
 * The Template for displaying Competition content.
 * Content only (without title and comments).
 *
 * This template can be overridden by copying it to yourtheme/anwp-football-leagues/content-competition--tabs.php.
 *
 * @author        Andrei Strekozov <anwp.pro>
 * @package       AnWP-Football-Leagues/Templates
 * @since         0.3.0
 * @since         0.4.2 Modified multistage logic
 * @since         0.7.2 Modified logic and structure
 *
 * @version       0.18.1
 */

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

// Prepare data
$competition_post_id = get_the_ID();
$is_multistage       = in_array( anwp_fl()->competition->get_competition_list_row( (int) $competition_post_id )['multistage'] ?? '', [ 'main', 'secondary' ], true );

// Get competition matches ( or all matches from the same league and season - if multistage )
$matches = anwp_football_leagues()->competition->tmpl_get_competition_matches( $competition_post_id, $is_multistage );

// Get competitions
$competitions = anwp_football_leagues()->competition->tmpl_get_prepared_competitions( $competition_post_id, $is_multistage, $matches );

// Bulk warm full competition rows (stage_groups + stage_rounds JSON blobs are needed below).
if ( ! empty( $competitions ) ) {
	anwp_fl()->competition->warm_competitions_full( wp_list_pluck( $competitions, 'ID' ) );
}

/**
 * Filter: anwpfl/tmpl-competition/competitions_prepared
 *
 * @since 0.7.5
 *
 * @param array   $competitions
 * @param integer $competition_post_id - Main Competition ID
 * @param bool    $is_multistage
 * @param array   $matches
 */
$competitions = apply_filters( 'anwpfl/tmpl-competition/competitions_prepared', $competitions, $competition_post_id, $is_multistage, $matches );

/**
 * Hook: anwpfl/tmpl-competition/before_wrapper
 *
 * @since 0.7.5
 *
 * @param int $competition_post_id Post ID
 */
do_action( 'anwpfl/tmpl-competition/before_wrapper', $competition_post_id );
?>
<div class="anwp-b-wrap competition competition-tabs competition__inner competition-<?php echo (int) $competition_post_id; ?> competition-mixed">

	<?php
	/**
	 * Hook: anwpfl/tmpl-competition/before_header
	 *
	 * @since 0.7.5
	 *
	 * @param int $competition_post_id Post ID
	 */
	do_action( 'anwpfl/tmpl-competition/before_header', $competition_post_id );

	/**
	 * Filter: anwpfl/tmpl-competition/render_header
	 *
	 * @since 0.7.5
	 *
	 * @param int $competition_post_id Post ID
	 */
	if ( apply_filters( 'anwpfl/tmpl-competition/render_header', true, $competition_post_id ) ) {

		$competition_shortcode_attr = [
			'id'              => $competition_post_id,
			'season_selector' => 1,
			'title_field'     => anwp_football_leagues()->customizer->get_value( 'competition', 'competition_title_field' ),
		];

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo anwp_football_leagues()->template->shortcode_loader( 'competition_header', $competition_shortcode_attr );
	}

	/**
	 * Hook: anwpfl/tmpl-competition/after_header
	 *
	 * @since 0.7.5
	 *
	 * @param int $competition_post_id Post ID
	 */
	do_action( 'anwpfl/tmpl-competition/after_header', $competition_post_id );

	if ( ! empty( $competitions ) && is_array( $competitions ) ) :
		if ( count( $competitions ) > 1 ) :
			?>
			<div class="anwp-fl-tabs mb-3 competition__tabs d-flex flex-wrap">
				<?php foreach ( $competitions as $ii => $c ) : ?>
					<?php $tab_stage_title = anwp_fl()->competition->get_competition_list_row( (int) $c->ID )['stage_title'] ?? ''; ?>
					<div class="position-relative anwp-text-sm anwp-fl-tabs__item anwp-bg-gray-light anwp-border anwp-border-light anwp-flex-1 d-flex align-items-center justify-content-center"
						data-target="#anwp_c_id_<?php echo (int) $c->ID; ?>" id="anwp_id_<?php echo (int) $c->ID; ?>-tab">
						<span><?php echo esc_html( $tab_stage_title ); ?></span>
						<a class="anwp-link-without-effects anwp-link-cover" href="#" aria-label="<?php echo esc_attr( $tab_stage_title ); ?>"></a>
					</div>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>

		<div class="anwp-fl-tabs-content competition__tabs-content">
			<?php foreach ( $competitions as $ii => $competition ) : ?>
				<div class="anwp-fl-tab__content" id="anwp_c_id_<?php echo (int) $competition->ID; ?>">
					<?php

					/**
					 * Hook: anwpfl/tmpl-competition/before_stage
					 *
					 * @since 0.7.5
					 *
					 * @param WP_Post $competition
					 * @param int     $competition_post_id Post ID
					 */
					do_action( 'anwpfl/tmpl-competition/before_stage', $competition, $competition_post_id );

					$competition_row  = anwp_fl()->competition->get_row( (int) $competition->ID ) ?? [];
					$competition_type = $competition_row['type'] ?? '';

					// Prepare groups
					$groups = json_decode( $competition_row['stage_groups'] ?? '' );

					if ( empty( $groups ) || ! is_array( $groups ) ) {
						continue;
					}

					/*
					|--------------------------------------------------------------------
					| Populate round field
					| Backward compatibility for competitions created before v0.10
					| @since 0.10.0
					|--------------------------------------------------------------------
					*/
					foreach ( $groups as $group_id => $group ) {
						if ( empty( $groups[ $group_id ]->round ) ) {
							$groups[ $group_id ]->round = 1;
						}
					}

					/*
					|--------------------------------------------------------------------
					| Prepare rounds
					| @since 0.10.0
					|--------------------------------------------------------------------
					*/
					$rounds = json_decode( $competition_row['stage_rounds'] ?? '' );

					if ( empty( $rounds ) || ! is_array( $rounds ) ) {
						$rounds = [
							(object) [
								'id'    => 1,
								'title' => '',
							],
						];
					}

					// Check DESC round sorting
					if ( 'desc' === anwp_football_leagues()->customizer->get_value( 'competition', 'competition_rounds_order' ) ) {
						$rounds = wp_list_sort( $rounds, 'id', 'DESC' );
					}

					// Round Tabs
					if ( count( $rounds ) > 1 ) :
						?>
						<div class="anwp-fl-tabs mb-3 d-flex flex-wrap competition__tabs" id="anwp-rounds-tabs-c<?php echo (int) $ii; ?>">
							<?php foreach ( $rounds as $rr => $c ) : ?>
								<div class="position-relative anwp-text-sm anwp-fl-tabs__item anwp-bg-gray-light anwp-border anwp-border-light anwp-flex-1 d-flex align-items-center justify-content-center"
									data-target="#anwp_c_id_<?php echo (int) $competition->ID; ?>_round_id_<?php echo (int) $c->id; ?>">
									<span><?php echo esc_html( $c->title ); ?></span>
									<a class="anwp-link-without-effects anwp-link-cover" href="#" aria-label="<?php echo esc_attr( $c->title ); ?>"></a>
								</div>
							<?php endforeach; ?>
						</div>
						<?php
					endif;
					?>
					<div class="anwp-fl-tabs-content competition-round__tabs-content">
						<?php foreach ( $rounds as $rr => $round ) : ?>
							<div class="anwp-fl-tab__content <?php echo ( count( $rounds ) > 1 ) ? 'd-none' : ''; ?>" id="anwp_c_id_<?php echo (int) $competition->ID; ?>_round_id_<?php echo (int) $round->id; ?>">
								<?php

								/**
								 * Hook: anwpfl/tmpl-competition/before_group
								 *
								 * @since 0.10.0
								 *
								 * @param object  $round
								 * @param object  $competition
								 * @param integer $competition_post_id - Competition ID
								 */
								do_action( 'anwpfl/tmpl-competition/before_round', $round, $competition, $competition_post_id );

								foreach ( $groups as $group ) :

									if ( intval( $group->round ) !== intval( $round->id ) ) {
										continue;
									}

									/**
									 * Hook: anwpfl/tmpl-competition/before_group
									 *
									 * @since 0.7.5
									 *
									 * @param object  $group
									 * @param object  $competition
									 * @param integer $competition_post_id - Main Competition ID
									 */
									do_action( 'anwpfl/tmpl-competition/before_group', $group, $competition, $competition_post_id );
									?>
									<div class="competition__group-wrapper">
										<?php
										if ( 'round-robin' === $competition_type ) :

											/**
											 * Hook: anwpfl/tmpl-competition/before_group_title
											 *
											 * @since 0.7.5
											 *
											 * @param object  $group
											 * @param object  $competition
											 * @param integer $competition_post_id - Main Competition ID
											 */
											do_action( 'anwpfl/tmpl-competition/before_group_title', $group, $competition, $competition_post_id );

											$render_group_title = count( $groups ) > 1 && ! empty( $group->title );

											/**
											 * Filter: anwpfl/tmpl-competition/render_group_title
											 *
											 * @since 0.7.5
											 *
											 * @param bool    $render_group_title
											 * @param object  $group
											 * @param object  $competition
											 * @param integer $competition_post_id - Main Competition ID
											 */
											if ( apply_filters( 'anwpfl/tmpl-competition/render_group_title', $render_group_title, $group, $competition, $competition_post_id ) ) :
												?>
												<div class="competition__group-title">
													<?php
													anwp_football_leagues()->load_partial(
														[
															'text'  => $group->title,
															'class' => 'competition-tabs__group-title',
														],
														'general/header'
													);
													?>
												</div>
												<?php
											endif;

											/**
											 * Hook: anwpfl/tmpl-competition/after_group_title
											 *
											 * @since 0.7.5
											 *
											 * @param object  $group
											 * @param object  $competition
											 * @param integer $competition_post_id - Main Competition ID
											 */
											do_action( 'anwpfl/tmpl-competition/after_group_title', $group, $competition, $competition_post_id );

											/**
											 * Filter: anwpfl/tmpl-competition/render_group_title
											 *
											 * @since 0.7.5
											 *
											 * @param bool
											 * @param object  $group
											 * @param object  $competition
											 * @param integer $competition_post_id - Main Competition ID
											 */
											if ( apply_filters( 'anwpfl/tmpl-competition/render_group_standing', true, $group, $competition, $competition_post_id ) ) :

												$standing = anwp_football_leagues()->competition->tmpl_get_competition_standings( $competition->ID, $group->id );

												if ( ! empty( $standing[0] ) && ! empty( $standing[0]->ID ) ) :

													// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
													echo anwp_football_leagues()->template->shortcode_loader(
														'standing',
														[
															'id'      => $standing[0]->ID,
															'title'   => '',
															'context' => 'competition',
														]
													);
												endif;
											endif;

											/**
											 * Hook: anwpfl/tmpl-competition/after_group_standing
											 *
											 * @since 0.7.5
											 *
											 * @param object  $group
											 * @param object  $competition
											 * @param integer $competition_post_id - Main Competition ID
											 */
											do_action( 'anwpfl/tmpl-competition/after_group_standing', $group, $competition, $competition_post_id );
										endif;

										/**
										 * Filter: anwpfl/tmpl-competition/render_list_of_matches
										 *
										 * @since 0.10.0
										 *
										 * @param bool
										 * @param object  $competition
										 * @param integer $competition_post_id - Main Competition ID
										 */
										if ( apply_filters( 'anwpfl/tmpl-competition/render_list_of_matches', true, $competition, $competition_post_id ) ) :
											$match_week = 0;
											foreach ( $matches as $match ) :

												if ( (int) $match->competition_id !== $competition->ID || (int) $match->group_id !== (int) $group->id ) {
													continue;
												}

												if ( 'round-robin' === $competition_type && $match_week !== (int) $match->match_week && (int) $match->match_week ) :
													?>
													<div class="anwp-section">
														<?php
														anwp_football_leagues()->load_partial(
															[
																'text'  => anwp_football_leagues()->options->get_text_matchweek( $match->match_week ),
																'class' => 'competition__round-title competition-tabs__round-title',
															],
															'general/subheader'
														);
														?>
													</div>
													<?php
													$match_week = (int) $match->match_week;
												endif;

												// Get match data to render
												$data = anwp_football_leagues()->match->prepare_match_data_to_render( $match );

												$data['competition_logo'] = false;
												anwp_football_leagues()->load_partial( $data, 'match/match', 'slim' );

											endforeach;
										endif;

										/**
										 * Hook: anwpfl/tmpl-competition/after_list_of_matches
										 *
										 * @since 0.10.0
										 *
										 * @param object  $group
										 * @param object  $competition
										 * @param integer $competition_post_id - Main Competition ID
										 */
										do_action( 'anwpfl/tmpl-competition/after_list_of_matches', $group, $competition, $competition_post_id );
										?>
									</div>
								<?php endforeach; ?>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			<?php endforeach; ?>
		</div>
	<?php endif; ?>
</div>
<?php
/**
 * Hook: anwpfl/tmpl-competition/after_wrapper
 *
 * @since 0.7.5
 *
 * @param int $competition_post_id Post ID
 */
do_action( 'anwpfl/tmpl-competition/after_wrapper', $competition_post_id );
