<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
/**
 * Render callback for the Snippet Copy Block.
 *
 * @see https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-metadata.md#render
 *
 * @var array    $attributes Block attributes.
 * @var string   $content    Block content (empty for this block).
 * @var WP_Block $block      Block instance.
 */

$bpsc_section_title       = $attributes['sectionTitle'] ?? '';
$bpsc_section_description = $attributes['sectionDescription'] ?? '';
$bpsc_section_alignment   = $attributes['sectionAlignment'] ?? 'left';
$bpsc_theme_style         = $attributes['theme'] ?? 'dark';
$bpsc_button_style        = $attributes['buttonStyle'] ?? 'rounded';
$bpsc_font_size           = absint( $attributes['fontSize'] ?? 14 );
$bpsc_show_line_numbers   = ! empty( $attributes['showLineNumbers'] );
$bpsc_show_traffic_lights = isset( $attributes['showTrafficLights'] ) ? (bool) $attributes['showTrafficLights'] : true;
$bpsc_snippet_max_width   = $attributes['snippetMaxWidth'] ?? '';
$bpsc_snippet_gap         = $attributes['snippetGap'] ?? '1rem';
$bpsc_snippets            = $attributes['snippets'] ?? array();

$bpsc_extra_classes = sprintf( 'theme-%s button-style-%s', esc_attr( $bpsc_theme_style ), esc_attr( $bpsc_button_style ) );

$bpsc_copy_icon_svg = '<span class="snippet-copy-block__copy-icon-wrap"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="snippet-copy-block__copy-icon"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg></span>';

$bpsc_copy_icon_allowed_svg = array(
	'span' => array( 'class' => true ),
	'svg'  => array( 'xmlns' => true, 'viewbox' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, 'class' => true, 'width' => true, 'height' => true ),
	'rect' => array( 'x' => true, 'y' => true, 'width' => true, 'height' => true, 'rx' => true, 'ry' => true ),
	'path' => array( 'd' => true ),
);

$bpsc_wrapper_attributes = get_block_wrapper_attributes(
	array(
		'class' => $bpsc_extra_classes,
	)
);
?>
<div <?php echo wp_kses_post( $bpsc_wrapper_attributes ); ?>>
	<?php
	$bpsc_inline_styles = array();
	if ( $bpsc_snippet_max_width ) {
		$bpsc_inline_styles[] = '.wp-block-bpsc-snippet-copy-section .snippet-copy-block__card { max-width: ' . esc_attr( $bpsc_snippet_max_width ) . '; }';
	}
	if ( $bpsc_snippet_gap ) {
		$bpsc_inline_styles[] = '.wp-block-bpsc-snippet-copy-section .snippet-copy-block__cards { gap: ' . esc_attr( $bpsc_snippet_gap ) . '; }';
	}
	if ( ! empty( $bpsc_inline_styles ) ) :
	?>
		<style><?php echo esc_html( implode( ' ', $bpsc_inline_styles ) ); ?></style>
	<?php endif; ?>
	<?php if ( $bpsc_section_title || $bpsc_section_description ) : ?>
		<div class="snippet-copy-block__header snippet-copy-block__header--align-<?php echo esc_attr( $bpsc_section_alignment ); ?>">
			<?php if ( $bpsc_section_title ) : ?>
				<h3 class="snippet-copy-block__title"><?php echo esc_html( $bpsc_section_title ); ?></h3>
			<?php endif; ?>
			<?php if ( $bpsc_section_description ) : ?>
				<p class="snippet-copy-block__description"><?php echo esc_html( $bpsc_section_description ); ?></p>
			<?php endif; ?>
		</div>
	<?php endif; ?>

	<?php if ( ! empty( $bpsc_snippets ) ) : ?>
		<div class="snippet-copy-block__cards">
			<?php foreach ( $bpsc_snippets as $bpsc_index => $bpsc_snippet ) :
				$bpsc_snippet_title   = $bpsc_snippet['title'] ?? '';
				$bpsc_snippet_lang    = $bpsc_snippet['language'] ?? '';
				$bpsc_snippet_content = $bpsc_snippet['content'] ?? '';

				if ( '' === trim( $bpsc_snippet_content ) && '' === trim( $bpsc_snippet_title ) ) {
					continue;
				}

				$bpsc_content_lines = explode( "\n", $bpsc_snippet_content );
				$bpsc_line_count    = count( $bpsc_content_lines );
			?>
				<div class="snippet-copy-block__card">
					<div class="snippet-copy-block__card-header">
						<?php if ( $bpsc_show_traffic_lights ) : ?>
							<div class="snippet-copy-block__traffic-lights" aria-hidden="true">
								<span class="snippet-copy-block__traffic-dot snippet-copy-block__traffic-dot--red"></span>
								<span class="snippet-copy-block__traffic-dot snippet-copy-block__traffic-dot--yellow"></span>
								<span class="snippet-copy-block__traffic-dot snippet-copy-block__traffic-dot--green"></span>
							</div>
						<?php endif; ?>
						<?php if ( $bpsc_snippet_title ) : ?>
							<span class="snippet-copy-block__card-title"><?php echo esc_html( $bpsc_snippet_title ); ?></span>
						<?php endif; ?>
						<?php if ( $bpsc_snippet_lang ) : ?>
							<span class="snippet-copy-block__language-badge"><?php echo esc_html( strtoupper( $bpsc_snippet_lang ) ); ?></span>
						<?php endif; ?>
						<div class="snippet-copy-block__card-header-right">
							<button
								class="snippet-copy-block__copy-button"
								data-snippet-index="<?php echo esc_attr( (string) $bpsc_index ); ?>"
								type="button"
								aria-label="<?php echo esc_attr(
									$bpsc_snippet_title
										? sprintf(
											/* translators: %s: snippet title */
											__( 'Copy %s to clipboard', 'section-collection' ),
											$bpsc_snippet_title
										)
										: __( 'Copy to clipboard', 'section-collection' )
								); ?>"
							>
								<?php echo wp_kses( $bpsc_copy_icon_svg, $bpsc_copy_icon_allowed_svg ); ?>
								<span class="snippet-copy-block__copy-label"><?php echo esc_html__( 'Copy', 'section-collection' ); ?></span>
							</button>
						</div>
					</div>
					<div class="snippet-copy-block__card-body">
						<?php if ( $bpsc_show_line_numbers ) : ?>
							<div class="snippet-copy-block__line-numbers" aria-hidden="true" style="font-size: <?php echo esc_attr( $bpsc_font_size . 'px' ); ?>">
								<?php for ( $bpsc_i = 1; $bpsc_i <= $bpsc_line_count; $bpsc_i++ ) : ?>
									<span><?php echo esc_html( (string) $bpsc_i ); ?></span>
								<?php endfor; ?>
							</div>
						<?php endif; ?>
						<pre class="snippet-copy-block__code-content" data-snippet-index="<?php echo esc_attr( (string) $bpsc_index ); ?>" style="font-size: <?php echo esc_attr( $bpsc_font_size . 'px' ); ?>"><code><?php echo esc_html( $bpsc_snippet_content ); ?></code></pre>
					</div>
				</div>
			<?php endforeach; ?>
		</div>

		<?php if ( count( $bpsc_snippets ) > 1 ) : ?>
			<div class="snippet-copy-block__copy-all">
				<button
					class="snippet-copy-block__copy-all-button"
					type="button"
					aria-label="<?php echo esc_attr__( 'Copy all snippets to clipboard', 'section-collection' ); ?>"
				>
					<?php echo wp_kses( $bpsc_copy_icon_svg, $bpsc_copy_icon_allowed_svg ); ?>
					<span class="snippet-copy-block__copy-label"><?php echo esc_html__( 'Copy All', 'section-collection' ); ?></span>
				</button>
			</div>
		<?php endif; ?>
	<?php endif; ?>
</div>
