<?php
/**
 * Brentwood Page Links — front-end render.
 *
 * Contextual sub-page navigation bar, migrated from brentwood.ca. A skewed
 * "parent" link on the left (desktop only) plus N sibling page tiles (photo +
 * bottom-right label pill). Each link reveals a primary-coloured underline on
 * hover. On mobile the parent panel is hidden and a "More …" highlight bar is
 * shown instead, with the sibling tiles stacked full width.
 *
 * Self-contained — no Tailwind dependency. Colours come from CSS custom props.
 *
 * @var array    $attributes Block attributes.
 * @var string   $content    Inner content (unused — dynamic block).
 * @var WP_Block $block      Block instance.
 */

defined( 'ABSPATH' ) || exit;

/**
 * Accept a CSS colour only if it is a hex, rgb()/rgba() or CSS var() value.
 */
if ( ! function_exists( 'bw_pl_color' ) ) {
	function bw_pl_color( $value, $default = '' ) {
		$value = trim( (string) $value );
		if ( '' === $value ) {
			return $default;
		}
		if ( preg_match( '/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $value )
			|| preg_match( '/^rgba?\(\s*[0-9.,%\s\/]+\)$/i', $value )
			|| preg_match( '/^var\(\s*--[a-z0-9\-]+\s*\)$/i', $value ) ) {
			return $value;
		}
		return $default;
	}
}

// FontAwesome angle-right / angle-left paths (same as brentwood.ca).
if ( ! function_exists( 'bw_pl_chevron' ) ) {
	function bw_pl_chevron( $dir ) {
		$right = 'M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z';
		$left  = 'M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z';
		$d = ( 'left' === $dir ) ? $left : $right;
		return '<svg class="bw-pl__chevron" viewBox="0 0 320 512" role="img" aria-hidden="true" focusable="false">'
			. '<path fill="currentColor" d="' . $d . '"></path></svg>';
	}
}

// ── sanitize attributes ─────────────────────────────────────────────────────

$parent_label = sanitize_text_field( $attributes['parentLabel'] ?? '' );
$parent_url   = esc_url_raw( $attributes['parentUrl'] ?? '' );
$parent_img   = esc_url_raw( $attributes['parentImageUrl'] ?? '' );
$parent_alt   = sanitize_text_field( $attributes['parentImageAlt'] ?? '' );
$more_label   = sanitize_text_field( $attributes['mobileMoreLabel'] ?? '' );
if ( '' === $more_label && '' !== $parent_label ) {
	$more_label = 'More ' . $parent_label;
}

$primary   = bw_pl_color( $attributes['primaryColor']   ?? '', '#cc0000' );
$highlight = bw_pl_color( $attributes['highlightColor'] ?? '', '#f1efe9' );

$items = is_array( $attributes['items'] ?? null ) ? $attributes['items'] : array();

// Nothing to show without at least one sibling tile or a parent link.
if ( empty( $items ) && '' === $parent_label ) {
	return;
}

// ── parent panel (desktop only) ───────────────────────────────────────────────
$parent_html = '';
if ( '' !== $parent_label ) {
	$photo = $parent_img
		? '<span class="bw-pl__parent-photo" style="background-image:url(' . esc_url( $parent_img ) . ')" role="img" aria-label="' . esc_attr( $parent_alt ) . '"></span>'
		: '';

	$parent_inner = $photo
		. '<span class="bw-pl__parent-label">'
		. '<span class="bw-pl__parent-text">' . esc_html( $parent_label ) . '</span>'
		. bw_pl_chevron( 'right' )
		. '</span>'
		. '<span class="bw-pl__line" aria-hidden="true"></span>';

	if ( $parent_url ) {
		$parent_html = '<a class="bw-pl__parent" href="' . esc_url( $parent_url ) . '">' . $parent_inner . '</a>';
	} else {
		$parent_html = '<div class="bw-pl__parent">' . $parent_inner . '</div>';
	}
}

// ── sibling tiles ──────────────────────────────────────────────────────────────
// Pre-filter to valid tiles so we know which one is last: every tile EXCEPT the
// last carries the skewed white divider on its right edge (like the parent).
$valid = array();
foreach ( $items as $item ) {
	$l = sanitize_text_field( $item['label'] ?? '' );
	$i = esc_url_raw( $item['imageUrl'] ?? '' );
	if ( '' === $l && '' === $i ) {
		continue;
	}
	$valid[] = $item;
}
$last_index = count( $valid ) - 1;

$tiles_html = '';
foreach ( $valid as $index => $item ) {
	$label   = sanitize_text_field( $item['label'] ?? '' );
	$url     = esc_url_raw( $item['url'] ?? '' );
	$img     = esc_url_raw( $item['imageUrl'] ?? '' );
	$alt     = sanitize_text_field( $item['imageAlt'] ?? '' );
	$chevron = ( isset( $item['chevron'] ) && in_array( $item['chevron'], array( 'left', 'right', 'none' ), true ) )
		? $item['chevron'] : 'right';

	$img_html = $img
		? '<img class="bw-pl__img" src="' . esc_url( $img ) . '" alt="' . esc_attr( $alt ) . '" loading="lazy">'
		: '<span class="bw-pl__img bw-pl__img--empty" aria-hidden="true"></span>';

	// Pill: chevron sits before the label when pointing left, after it otherwise.
	$pill_parts = '';
	if ( 'left' === $chevron ) {
		$pill_parts .= '<span class="bw-pl__chevron-wrap bw-pl__chevron-wrap--left">' . bw_pl_chevron( 'left' ) . '</span>';
		$pill_parts .= '<span class="bw-pl__pill-label bw-pl__pill-label--left">' . esc_html( $label ) . '</span>';
	} elseif ( 'none' === $chevron ) {
		$pill_parts .= '<span class="bw-pl__pill-label bw-pl__pill-label--left">' . esc_html( $label ) . '</span>';
	} else {
		$pill_parts .= '<span class="bw-pl__pill-label">' . esc_html( $label ) . '</span>';
		$pill_parts .= '<span class="bw-pl__chevron-wrap">' . bw_pl_chevron( 'right' ) . '</span>';
	}

	$overlay = '<span class="bw-pl__overlay"><span class="bw-pl__pill">' . $pill_parts . '</span></span>';
	$line    = '<span class="bw-pl__line" aria-hidden="true"></span>';
	// Image + pill live in a counter-skewed wrapper so they stay upright while
	// the tile itself is skewed. The line spans the (skewed) tile.
	$inner   = '<span class="bw-pl__photo">' . $img_html . $overlay . '</span>' . $line;

	// All tiles but the last get the skewed right divider; descending z-index so
	// each skewed edge sits above the tile to its right.
	$tile_class = 'bw-pl__tile' . ( $index < $last_index ? ' bw-pl__tile--skew' : '' );
	$tile_style = 'z-index:' . ( 6 - $index ) . ';'; // below parent (7) + border (8)

	if ( $url ) {
		$aria = ( '' !== $label ) ? ' aria-label="' . esc_attr( $label ) . '"' : '';
		$tiles_html .= '<a class="' . $tile_class . '" style="' . esc_attr( $tile_style ) . '" href="' . esc_url( $url ) . '"' . $aria . '>' . $inner . '</a>';
	} else {
		$tiles_html .= '<div class="' . $tile_class . '" style="' . esc_attr( $tile_style ) . '">' . $inner . '</div>';
	}
}

// ── mobile "More …" bar ─────────────────────────────────────────────────────────
$more_html = '';
if ( '' !== $more_label ) {
	$more_inner = '<span class="bw-pl__more-text">' . esc_html( $more_label ) . '</span>' . bw_pl_chevron( 'right' );
	if ( $parent_url ) {
		$more_html = '<a class="bw-pl__more" href="' . esc_url( $parent_url ) . '">' . $more_inner . '</a>';
	} else {
		$more_html = '<div class="bw-pl__more">' . $more_inner . '</div>';
	}
}

// ── wrapper ──────────────────────────────────────────────────────────────────
$style = '--bw-pl-primary:' . esc_attr( $primary ) . ';--bw-pl-highlight:' . esc_attr( $highlight ) . ';';

printf(
	'<div %s>'
		. '<div class="bw-pl__bar">'
			. '<span class="bw-pl__border" aria-hidden="true"></span>'
			. '%s%s%s'
		. '</div>'
	. '</div>',
	get_block_wrapper_attributes( array( 'class' => 'bw-pl', 'style' => $style ) ), // phpcs:ignore WordPress.Security.EscapeOutput
	$parent_html, // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above
	$tiles_html,  // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above
	$more_html    // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above
);
