<?php
/**
 * The Template for displaying Matches.
 *
 * This template can be overridden by copying it to yourtheme/anwp-football-leagues/widget-matches.php.
 *
 * @var object $data - Object with widget data.
 *
 * @author        Andrei Strekozov <anwp.pro>
 * @package       AnWP-Football-Leagues/Templates
 * @since         0.4.3
 *
 * @version       0.18.0
 */

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

// Prevent errors with new params
$data = (object) wp_parse_args(
	$data,
	[
		'show_club_logos'       => 1,
		'show_match_datetime'   => true,
		'club_links'            => true,
		'group_by_header_style' => '',
		'layout'                => '',
		'group_by'              => '',
		'link_target'           => '',
		'link_text'             => '',
	]
);

// Get competition matches
$matches = anwp_football_leagues()->competition->tmpl_get_competition_matches_extended( $data );
?>
	<div class="anwp-b-wrap match-list match-list--widget layout--<?php echo esc_attr( $data->layout ); ?>">
		<?php
		$group_current = '';

		// Bulk warm competition rows for the stage_title group_by below.
		anwp_fl()->competition->warm_competitions( array_unique( wp_list_pluck( $matches, 'competition_id' ) ) );

		foreach ( $matches as $m_index => $match ) {

			if ( '' !== $data->group_by ) {

				$group_text = '';

				// Check current group by value
				if ( 'stage' === $data->group_by && $group_current !== $match->competition_id ) {
					$group_text    = anwp_fl()->competition->get_competition_list_row( (int) $match->competition_id )['stage_title'] ?? '';
					$group_current = $match->competition_id;
				} elseif ( 'matchweek' === $data->group_by && $group_current !== $match->match_week && '0' !== $match->match_week ) {
					$group_text    = anwp_football_leagues()->competition->tmpl_get_matchweek_round_text( $match->match_week, $match->competition_id );
					$group_current = $match->match_week;
				} elseif ( 'day' === $data->group_by ) {
					$day_to_compare = date( 'Y-m-d', strtotime( $match->kickoff ) );

					if ( $day_to_compare !== $group_current ) {
						$group_text    = date_i18n( anwp_football_leagues()->get_option_value( 'custom_match_date_format' ) ?: 'j M Y', strtotime( $match->kickoff ) );
						$group_current = $day_to_compare;
					}
				} elseif ( 'month' === $data->group_by ) {
					$month_to_compare = date( 'Y-m', strtotime( $match->kickoff ) );

					if ( $month_to_compare !== $group_current ) {
						$group_text    = date_i18n( 'M Y', strtotime( $match->kickoff ) );
						$group_current = $month_to_compare;
					}
				}

				if ( $group_text ) {

					if ( 'secondary' === $data->group_by_header_style ) {
						anwp_football_leagues()->load_partial(
							[
								'text'  => esc_html( $group_text ),
								'class' => $m_index ? ' mt-4 mb-1' : 'mb-1',
							],
							'general/subheader'
						);
					} else {
						anwp_football_leagues()->load_partial(
							[
								'text'  => esc_html( $group_text ),
								'class' => $m_index ? ' mt-4' : '',
							],
							'general/header'
						);
					}
				}
			}

			$tmpl_data = array_merge( (array) $data, anwp_football_leagues()->match->prepare_match_data_to_render( $match, $data ) );
			anwp_football_leagues()->load_partial( $tmpl_data, 'match/match', 'simple' );
		}
		?>
	</div>
<?php if ( ! empty( $data->link_text ) && ! empty( $data->link_target ) ) : ?>
	<div class="position-relative anwp-fl-btn-outline anwp-text-sm w-100 match-list__link-btn mt-2">
		<?php echo esc_html( $data->link_text ); ?>
		<a href="<?php echo esc_url( get_permalink( (int) $data->link_target ) ); ?>" class="anwp-link-cover anwp-link-without-effects" aria-label="<?php echo esc_attr( $data->link_text ); ?>"></a>
	</div>
	<?php
endif;
