<?php
/**
 * Template Functions
 *
 * Helper functions for theme templates.
 *
 * @package ROI_Theme
 * @since 1.0.0
 */

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

/**
 * Prints HTML with meta information for the current post-date/time and author.
 *
 * @since 1.0.0
 */
function roi_posted_on() {
	$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';

	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
		$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
	}

	$time_string = sprintf(
		$time_string,
		esc_attr( get_the_date( DATE_W3C ) ),
		esc_html( get_the_date() ),
		esc_attr( get_the_modified_date( DATE_W3C ) ),
		esc_html( get_the_modified_date() )
	);

	$posted_on = sprintf(
		/* translators: %s: post date. */
		esc_html_x( 'Publicado el %s', 'post date', 'roi' ),
		'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
	);

	echo '<span class="posted-on">' . $posted_on . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
 * Prints HTML with meta information for the current author.
 *
 * @since 1.0.0
 */
function roi_posted_by() {
	$byline = sprintf(
		/* translators: %s: post author. */
		esc_html_x( 'por %s', 'post author', 'roi' ),
		'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
	);

	echo '<span class="byline"> ' . $byline . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
 * Prints HTML with meta information for the categories, tags, and comments.
 *
 * @since 1.0.0
 */
function roi_entry_footer() {
	// Hide category and tag text for pages.
	if ( 'post' === get_post_type() ) {
		/* translators: used between list items, there is a space after the comma */
		$categories_list = get_the_category_list( esc_html__( ', ', 'roi' ) );
		if ( $categories_list ) {
			/* translators: 1: list of categories. */
			printf( '<span class="cat-links">' . esc_html__( 'Categorías: %1$s', 'roi' ) . '</span>', $categories_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}

		/* translators: used between list items, there is a space after the comma */
		$tags_list = get_the_tag_list( '', esc_html_x( ', ', 'list item separator', 'roi' ) );
		if ( $tags_list ) {
			/* translators: 1: list of tags. */
			printf( '<span class="tags-links">' . esc_html__( 'Etiquetas: %1$s', 'roi' ) . '</span>', $tags_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}
	}

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
		comments_popup_link(
			sprintf(
				wp_kses(
					/* translators: %s: post title */
					__( 'Comentar<span class="screen-reader-text"> en %s</span>', 'roi' ),
					array(
						'span' => array(
							'class' => array(),
						),
					)
				),
				wp_kses_post( get_the_title() )
			)
		);
		echo '</span>';
	}

	edit_post_link(
		sprintf(
			wp_kses(
				/* translators: %s: Name of current post. Only visible to screen readers */
				__( 'Editar <span class="screen-reader-text">%s</span>', 'roi' ),
				array(
					'span' => array(
						'class' => array(),
					),
				)
			),
			wp_kses_post( get_the_title() )
		),
		'<span class="edit-link">',
		'</span>'
	);
}

/**
 * Custom excerpt length
 *
 * @since 1.0.0
 * @param int $length Default excerpt length.
 * @return int Modified excerpt length.
 */
function roi_excerpt_length( $length ) {
	if ( is_admin() ) {
		return $length;
	}

	return 40; // Change this to desired excerpt length.
}
add_filter( 'excerpt_length', 'roi_excerpt_length', 999 );

/**
 * Custom excerpt more string
 *
 * @since 1.0.0
 * @param string $more Default more string.
 * @return string Modified more string.
 */
function roi_excerpt_more( $more ) {
	if ( is_admin() ) {
		return $more;
	}

	return '&hellip;';
}
add_filter( 'excerpt_more', 'roi_excerpt_more' );

/**
 * Get custom excerpt by character count
 *
 * @since 1.0.0
 * @param int    $charlength Character length.
 * @param string $more       More string.
 * @return string Custom excerpt.
 */
function roi_get_excerpt( $charlength = 200, $more = '...' ) {
	$excerpt = get_the_excerpt();
	$charlength++;

	if ( mb_strlen( $excerpt ) > $charlength ) {
		$subex   = mb_substr( $excerpt, 0, $charlength - 5 );
		$exwords = explode( ' ', $subex );
		$excut   = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );

		if ( $excut < 0 ) {
			$excerpt = mb_substr( $subex, 0, $excut );
		} else {
			$excerpt = $subex;
		}

		$excerpt .= $more;
	}

	return $excerpt;
}

/**
 * Get post thumbnail URL
 *
 * @since 1.0.0
 * @param string $size Image size.
 * @return string|false Thumbnail URL or false if not found.
 */
function roi_get_post_thumbnail_url( $size = 'full' ) {
	if ( has_post_thumbnail() ) {
		$thumb_id = get_post_thumbnail_id();
		$thumb    = wp_get_attachment_image_src( $thumb_id, $size );

		if ( $thumb ) {
			return $thumb[0];
		}
	}

	return false;
}

/**
 * Display breadcrumbs
 *
 * @since 1.0.0
 * @param array $args Breadcrumb arguments.
 */
function roi_breadcrumbs( $args = array() ) {
	// Default arguments.
	$defaults = array(
		'separator'    => '<span class="separator">/</span>',
		'home_label'   => esc_html__( 'Inicio', 'roi' ),
		'show_home'    => true,
		'show_current' => true,
		'before'       => '<nav class="breadcrumbs" aria-label="' . esc_attr__( 'Breadcrumb', 'roi' ) . '"><ol class="breadcrumb-list">',
		'after'        => '</ol></nav>',
		'link_before'  => '<li class="breadcrumb-item">',
		'link_after'   => '</li>',
	);

	$args = wp_parse_args( $args, $defaults );

	// Don't display on homepage.
	if ( is_front_page() ) {
		return;
	}

	global $post;

	echo $args['before']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

	// Home link.
	if ( $args['show_home'] ) {
		echo $args['link_before'] . '<a href="' . esc_url( home_url( '/' ) ) . '">' . esc_html( $args['home_label'] ) . '</a>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $args['separator']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}

	if ( is_category() ) {
		$category = get_queried_object();
		if ( $category->parent ) {
			$parent_cats = get_category_parents( $category->parent, true, $args['separator'] );
			echo $args['link_before'] . $parent_cats . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}
		echo $args['link_before'] . '<span class="current">' . esc_html( single_cat_title( '', false ) ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

	} elseif ( is_single() && ! is_attachment() ) {
		if ( get_post_type() !== 'post' ) {
			$post_type        = get_post_type_object( get_post_type() );
			$post_type_archive = get_post_type_archive_link( get_post_type() );

			if ( $post_type_archive ) {
				echo $args['link_before'] . '<a href="' . esc_url( $post_type_archive ) . '">' . esc_html( $post_type->labels->name ) . '</a>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				echo $args['separator']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			}

			if ( $args['show_current'] ) {
				echo $args['link_before'] . '<span class="current">' . esc_html( get_the_title() ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			}
		} else {
			$category = get_the_category();
			if ( $category ) {
				$category_values  = array_values( $category );
				$category_last    = end( $category_values );
				$category_parents = get_category_parents( $category_last->term_id, true, $args['separator'] );
				echo $args['link_before'] . $category_parents . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			}

			if ( $args['show_current'] ) {
				echo $args['link_before'] . '<span class="current">' . esc_html( get_the_title() ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			}
		}
	} elseif ( is_page() && ! $post->post_parent ) {
		if ( $args['show_current'] ) {
			echo $args['link_before'] . '<span class="current">' . esc_html( get_the_title() ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}
	} elseif ( is_page() && $post->post_parent ) {
		$parent_id   = $post->post_parent;
		$breadcrumbs = array();

		while ( $parent_id ) {
			$page          = get_post( $parent_id );
			$breadcrumbs[] = '<a href="' . esc_url( get_permalink( $page->ID ) ) . '">' . esc_html( get_the_title( $page->ID ) ) . '</a>';
			$parent_id     = $page->post_parent;
		}

		$breadcrumbs = array_reverse( $breadcrumbs );
		foreach ( $breadcrumbs as $crumb ) {
			echo $args['link_before'] . $crumb . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo $args['separator']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}

		if ( $args['show_current'] ) {
			echo $args['link_before'] . '<span class="current">' . esc_html( get_the_title() ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}
	} elseif ( is_search() ) {
		echo $args['link_before'] . '<span class="current">' . esc_html__( 'Resultados de búsqueda para: ', 'roi' ) . get_search_query() . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

	} elseif ( is_tag() ) {
		echo $args['link_before'] . '<span class="current">' . esc_html__( 'Etiqueta: ', 'roi' ) . esc_html( single_tag_title( '', false ) ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

	} elseif ( is_author() ) {
		$author = get_queried_object();
		echo $args['link_before'] . '<span class="current">' . esc_html__( 'Autor: ', 'roi' ) . esc_html( $author->display_name ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

	} elseif ( is_day() ) {
		echo $args['link_before'] . '<a href="' . esc_url( get_year_link( get_the_time( 'Y' ) ) ) . '">' . esc_html( get_the_time( 'Y' ) ) . '</a>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $args['separator']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $args['link_before'] . '<a href="' . esc_url( get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) ) . '">' . esc_html( get_the_time( 'F' ) ) . '</a>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $args['separator']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $args['link_before'] . '<span class="current">' . esc_html( get_the_time( 'd' ) ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

	} elseif ( is_month() ) {
		echo $args['link_before'] . '<a href="' . esc_url( get_year_link( get_the_time( 'Y' ) ) ) . '">' . esc_html( get_the_time( 'Y' ) ) . '</a>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $args['separator']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $args['link_before'] . '<span class="current">' . esc_html( get_the_time( 'F' ) ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

	} elseif ( is_year() ) {
		echo $args['link_before'] . '<span class="current">' . esc_html( get_the_time( 'Y' ) ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

	} elseif ( is_404() ) {
		echo $args['link_before'] . '<span class="current">' . esc_html__( 'Error 404', 'roi' ) . '</span>' . $args['link_after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}

	echo $args['after']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
 * Add custom body classes
 *
 * @since 1.0.0
 * @param array $classes Existing body classes.
 * @return array Modified body classes.
 */
function roi_body_classes( $classes ) {
	// Add class if sidebar is active.
	if ( is_active_sidebar( 'sidebar-1' ) ) {
		$classes[] = 'has-sidebar';
	} else {
		$classes[] = 'no-sidebar';
	}

	// Add class if it's a singular post/page.
	if ( is_singular() ) {
		$classes[] = 'singular';
	}

	// Add class for specific post formats.
	if ( is_singular() && has_post_format() ) {
		$post_format = get_post_format();
		$classes[]   = 'post-format-' . $post_format;
	}

	return $classes;
}
add_filter( 'body_class', 'roi_body_classes' );

/**
 * Add custom post classes
 *
 * @since 1.0.0
 * @param array $classes Existing post classes.
 * @return array Modified post classes.
 */
function roi_post_classes( $classes ) {
	// Add class if post has thumbnail.
	if ( has_post_thumbnail() ) {
		$classes[] = 'has-post-thumbnail';
	} else {
		$classes[] = 'no-post-thumbnail';
	}

	return $classes;
}
add_filter( 'post_class', 'roi_post_classes' );

/**
 * Sanitize SVG uploads
 *
 * @since 1.0.0
 * @param array $mimes Allowed mime types.
 * @return array Modified mime types.
 */
function roi_add_svg_mime_types( $mimes ) {
	$mimes['svg']  = 'image/svg+xml';
	$mimes['svgz'] = 'image/svg+xml';

	return $mimes;
}
add_filter( 'upload_mimes', 'roi_add_svg_mime_types' );

/**
 * Pagination for archive pages
 *
 * @since 1.0.0
 * @param array $args Pagination arguments.
 */
function roi_pagination( $args = array() ) {
	global $wp_query;

	// Don't print empty markup if there's only one page.
	if ( $wp_query->max_num_pages < 2 ) {
		return;
	}

	$defaults = array(
		'mid_size'           => 2,
		'prev_text'          => esc_html__( '&larr; Anterior', 'roi' ),
		'next_text'          => esc_html__( 'Siguiente &rarr;', 'roi' ),
		'screen_reader_text' => esc_html__( 'Navegación de entradas', 'roi' ),
		'type'               => 'list',
		'current'            => max( 1, get_query_var( 'paged' ) ),
	);

	$args = wp_parse_args( $args, $defaults );

	// Make sure we get a string back. Plain is the next best thing.
	if ( isset( $args['type'] ) && 'array' === $args['type'] ) {
		$args['type'] = 'plain';
	}

	echo '<nav class="pagination" aria-label="' . esc_attr( $args['screen_reader_text'] ) . '">';
	echo wp_kses_post( paginate_links( $args ) );
	echo '</nav>';
}

/**
 * Get footer column Bootstrap class
 *
 * Returns the appropriate Bootstrap grid classes for footer columns
 * based on the column number. Supports configurable widths and
 * responsive breakpoints.
 *
 * @since 1.0.0
 * @param int $column Column number (1-4).
 * @return string Bootstrap column classes.
 */
function roi_get_footer_column_class( $column = 1 ) {
	// Default configuration: Equal width columns (3 columns each on desktop).
	// You can customize these classes per column as needed.
	$column_classes = array(
		1 => 'col-12 col-md-6 col-lg-3',  // Column 1: Full width mobile, half tablet, quarter desktop.
		2 => 'col-12 col-md-6 col-lg-3',  // Column 2: Full width mobile, half tablet, quarter desktop.
		3 => 'col-12 col-md-6 col-lg-3',  // Column 3: Full width mobile, half tablet, quarter desktop.
		4 => 'col-12 col-md-6 col-lg-3',  // Column 4: Full width mobile, half tablet, quarter desktop.
	);

	/**
	 * Filter footer column classes
	 *
	 * Allows customization of footer column widths via filter.
	 *
	 * @since 1.0.0
	 * @param array $column_classes Array of column classes.
	 */
	$column_classes = apply_filters( 'roi_footer_column_classes', $column_classes );

	// Return the class for the specified column, or default to col-12 if not found.
	return isset( $column_classes[ $column ] ) ? $column_classes[ $column ] : 'col-12';
}
