<?php
/**
 * Theme Pagination
 *
 * @package wpstream-theme
 */

if ( ! function_exists( 'wpstream_theme_pagination' ) ) {
	/**
	 * Display pagination links.
	 *
	 * @param int    $pages         Total number of pages.
	 * @param int    $range         Number of pages to show before and after the current page.
	 * @param string $external_paged External page number (optional).
	 */
	function wpstream_shortcode_pagination( $pages = '', $range = 2, $external_paged = '', $uid = '' ) {
		global $wp;
		$showitems = ( $range * 2 ) + 1;

		$query_var_name = 'paged';
		if ( '' !== $uid ) {
			$query_var_name .= '_' . $uid;
		}

		$base_url     = home_url( add_query_arg( null, null ) );
		$current_page = 1;

		if ( isset( $_GET[ $query_var_name ] ) ) {
			$current_page = intval( $_GET[ $query_var_name ] );
		}

		// Default page to one if not provided.
		if ( intval( $external_paged ) !== 0 ) {
			$current_page = $external_paged;
		}

		if ( '' === $pages ) {
			global $wp_query;
			$pages = $wp_query->max_num_pages;

			if ( ! $pages ) {
				$pages = 1;
			}
		}

		if ( 1 !== $pages ) {
			echo '<nav aria-label="Page navigation" class="wpstream-pagination" role="navigation">';
			echo '<span class="sr-only">' . esc_html__( 'Page navigation', 'hello-wpstream' ) . '</span>';
			echo '<ul class="pagination justify-content-center mb-4">';

			$prev_url = add_query_arg( $query_var_name, $current_page - 1, $base_url );

			echo "<li class=\"page-item\">
                    <a class=\"page-link page-item__link-prev\" href='" . esc_url( $prev_url ) . "'>
                        " . wpstream_theme_get_svg_icon( 'pagination_arrow_left.svg' ) . '
                        ' . esc_html( 'Previous' ) . '
                    </a>
                </li>';

			for ( $i = 1; $i <= $pages; $i++ ) {
				if ( 1 !== $pages && ( ! ( $i >= $current_page + $range + 1 || $i <= $current_page - $range - 1 ) || $pages <= $showitems ) ) {
					$current_url = add_query_arg( $query_var_name, $i, $base_url );
					if ( $current_page === $i ) {
						print '<li class="page-item active"><a  class="page-link" href="' . esc_url( $current_url ) . '" >' . esc_html( $i ) . '</a></li>';
					} else {
						print '<li class="page-item "><a  class="page-link" href="' . esc_url( $current_url ) . '" >' . esc_html( $i ) . '</a></li>';
					}
				}
			}

			if ( ( $current_page + 1 ) > $pages ) {
				$next_page = add_query_arg( $query_var_name, $current_page, $base_url );
			} else {
				$next_page = add_query_arg( $query_var_name, $current_page + 1, $base_url );
			}

			echo "<li class=\"page-item\">
                <a class=\"page-link page-item__link-next\" href='" . esc_url( $next_page ) . "'>
                    " . esc_html( 'Next' ) . '
                    ' . wpstream_theme_get_svg_icon( 'pagination_arrow_right.svg' ) . '
                </a>
            </li>';
			echo '</ul>';
			echo '</nav>';
		}
	}
}

if ( ! function_exists( 'wpstream_theme_pagination' ) ) {
	/**
	 * Display pagination links.
	 *
	 * @param int    $pages         Total number of pages.
	 * @param int    $range         Number of pages to show before and after the current page.
	 * @param string $external_paged External page number (optional).
	 */
	function wpstream_theme_pagination( $pages = '', $range = 2, $external_paged = '' ) {
		$showitems    	= 	( $range * 2 ) + 1;
		$current_page 	= 	get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

		// Default page to one if not provided.
		if ( intval( $external_paged ) !== 0 ) {
			$current_page = $external_paged;
		}

		if ( '' === $pages ) {
			global $wp_query;
			$pages = $wp_query->max_num_pages;

			if ( ! $pages ) {
				$pages = 1;
			}
		}
		?>

		<?php if ( 1 !== $pages ) : ?>
			<nav aria-label="Page navigation" class="wpstream-pagination" role="navigation">
				<span class="sr-only"><?php echo esc_html__( 'Page navigation', 'hello-wpstream' ); ?></span>
				<div class="pagination">


				<div class="page-item pagination__link pagination__link--prev">
					<a class="page-link" href="<?php echo esc_url( get_pagenum_link( $current_page - 1 ) ); ?>">
						<?php echo wpstream_theme_get_svg_icon( 'pagination_arrow_left.svg' ); ?>

						<?php esc_html_e( 'Previous', 'hello-wpstream' ); ?>
					</a>
				</div>

			<?php for ( $i = 1; $i <= $pages; $i++ ) : ?>
				<?php if ( 1 !== $pages && ( ! ( $i >= $current_page + $range + 1 || $i <= $current_page - $range - 1 ) || $pages <= $showitems ) ) : ?>
					<div class="pagination__page-items">
						<?php if ( $current_page === $i ) : ?>
							<div class="page-item active">
								<a  class="page-link" href="<?php echo esc_url( get_pagenum_link( $i ) ); ?>" >
									<?php echo esc_html( $i ); ?>
								</a>
							</div>
						<?php else : ?>
							<div class="page-item pagination__page-item">
								<a  class="page-link" href="<?php echo esc_url( get_pagenum_link( $i ) ); ?>" >
									<?php echo esc_html( $i ); ?>
								</a>
							</div>
						<?php endif; ?>
					</div>
				<?php endif; ?>
			<?php endfor; ?>

			<?php
			if ( ( $current_page + 1 ) > $pages ) :
				$next_page = get_pagenum_link( $current_page + 1 );
				?>
				<?php $next_page = get_pagenum_link( $current_page ); ?>
			<?php else : ?>
				<?php $next_page = get_pagenum_link( $current_page + 1 ); ?>
			<?php endif; ?>




					<div class="page-item pagination__link pagination__link--next ">
						<a class="page-link" href="<?php echo esc_url( $next_page ); ?>">
							<?php esc_html_e( 'Next', 'hello-wpstream' ); ?>

							<?php echo wpstream_theme_get_svg_icon( 'pagination_arrow_right.svg' ); ?>
						</a>
					</div>
				</div>
			</nav>
		<?php endif; ?>

		<?php
	}

}


/*
*
* Theme Pagination Ajax
*
*/

if ( ! function_exists( 'wpstream_theme_pagination_ajax' ) ) {
	/**
	 * Display pagination links for AJAX requests.
	 *
	 * @param array  $attributes       Attributes for pagination.
	 * @param int    $pages            Total number of pages.
	 * @param int    $range            Number of pages to show before and after the current page.
	 * @param string $external_paged   External page number (optional).
	 * @return string                  HTML markup for pagination.
	 */
	function wpstream_theme_pagination_ajax( $attributes, $pages = '', $range = 2, $external_paged = '' ) {
		$url = get_permalink( $attributes['current_page'] );

		unset( $attributes['current_page'] );
		unset( $attributes['is_elementor'] );
		unset( $attributes['is_ajax'] );
		unset( $attributes['pagination_type'] );
		unset( $attributes['paged'] );
		unset( $attributes['number'] );
		unset( $attributes['rownumber'] );

		$query_var_name = 'paged';
		if ( isset( $attributes['blog_uid'] ) && $attributes['blog_uid'] !== '' ) {
					$query_var_name .= '_' . $attributes['blog_uid'];
		}
		if ( isset( $attributes['video_uid'] ) && $attributes['video_uid'] !== '' ) {
			$query_var_name .= '_' . $attributes['video_uid'];
		}

		$query_arg_attributes['sort_by'] = $attributes['sort_by'];

		if ( isset( $attributes['type'] ) ) {
			$query_arg_attributes['type'] = $attributes['type'];
		}

		if ( isset( $attributes['category_ids'] ) ) {
			$query_arg_attributes['category_ids'] = $attributes['category_ids'];
		}

		if ( isset( $attributes['wpstream_category_ids'] ) ) {
			$query_arg_attributes['wpstream_category_ids'] = $attributes['wpstream_category_ids'];
		}
		if ( isset( $attributes['movie_ratings_ids'] ) ) {
			$query_arg_attributes['movie_ratings_ids'] = $attributes['movie_ratings_ids'];
		}
		if ( isset( $attributes['actors_ids'] ) ) {
			$query_arg_attributes['actors_ids'] = $attributes['actors_ids'];
		}

		$query_arg_attributes['from_ajax'] = 'yes';

		ob_start();
		$showitems = ( $range * 2 ) + 1;
		// default page to one if not provided.
		if ( intval( $external_paged ) !== 0 ) {
			$paged = $external_paged;
		}

		if ( empty( $paged ) ) {
			$paged = 1;
		}

		if ( '' === $pages ) {
			global $wp_query;
			$pages = $wp_query->max_num_pages;

			if ( ! $pages ) {
				$pages = 1;
			}
		}

		if ( 1 !== $pages ) {

			$prev_url = wpstream_custom_get_pagenum_link( $url, $query_var_name, $paged - 1, $query_arg_attributes );
			echo '<span class="sr-only">' . esc_html__( 'Page navigation', 'hello-wpstream' ) . '</span>';
			echo '<ul class="pagination justify-content-center mb-4">';

			echo "<li class=\"page-item\"><a class=\"page-link\" href='" . esc_url( $prev_url ) . "'><i class=\"fas fa-chevron-left\"></i></a></li>";

			for ( $i = 1; $i <= $pages; $i++ ) {
				if ( 1 !== $pages && ( ! ( $i >= $paged + $range + 1 || $i <= $paged - $range - 1 ) || $pages <= $showitems ) ) {

					$current_url = wpstream_custom_get_pagenum_link( $url, $query_var_name, $i, $query_arg_attributes );
					if ( $paged === $i ) {
						print '<li class="page-item active"><a  class="page-link" href="' . esc_url( $current_url ) . '" >' . esc_html( $i ) . '</a></li>';
					} else {
						print '<li class="page-item "><a  class="page-link" href="' . esc_url( $current_url ) . '" >' . esc_html( $i ) . '</a></li>';
					}
				}
			}

			if ( ( $paged + 1 ) > $pages ) {
								$next_page = wpstream_custom_get_pagenum_link( $url, $query_var_name, $paged, $query_arg_attributes );

			} else {
				$next_page = wpstream_custom_get_pagenum_link( $url, $query_var_name, $paged + 1, $query_arg_attributes );

			}

			echo "<li class=\"page-item\"><a class=\"page-link\" href='" . esc_url( $next_page ) . "'><i class=\"fas fa-chevron-right\"></i></a><li>";
			echo '</ul>';

		}
		$return = ob_get_contents();
		ob_end_clean();
		return $return;
	}
}

/**
 * Generate a paginated URL.
 *
 * @param string $base        The base URL.
 * @param int    $pagenum     The page number.
 * @param array  $query_args  An array of query arguments (optional).
 * @return string             The paginated URL.
 */
function wpstream_custom_get_pagenum_link( $base, $query_var_name, $pagenum, $query_args = array() ) {
	$request = add_query_arg( array( $query_var_name => $pagenum ), $base );

	foreach ( $query_args as $key => $value ) {
		if ( '' !== $value ) {
			$request = add_query_arg( array( $key => $value ), $request );
		}
	}

	return $request;
}

add_filter( 'next_post_link', 'post_link_attributes' );
add_filter( 'previous_post_link', 'post_link_attributes' );

/**
 * Add attributes to post link.
 *
 * @param string $output The post link HTML output.
 * @return string        The modified post link HTML output.
 */
function post_link_attributes( $output ) {
	$code = 'class="page-link"';
	return str_replace( '<a href=', '<a ' . $code . ' href=', $output );
}

function wpstream_theme_pagination_for_taxonmies( $found_posts, $item_post_type ) {
	$big = 999999999; // need an unlikely integer

	// Construct the base URL for pagination, including the item_post_type query var
	$base = esc_url( add_query_arg( 'item_post_type', $item_post_type, get_pagenum_link( $big ) ) );

	$pages = paginate_links(
		array(
			'base'      => str_replace( $big, '%#%', $base ),
			'format'    => '?paged=%#%',
			'current'   => max( 1, get_query_var( 'paged' ) ),
			'total'     => ceil( $found_posts / get_option( 'posts_per_page' ) ),
			'type'      => 'array',
		  'prev_next' => false, 
		)
	);

	if ( is_array( $pages ) ) {
			$current_page = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' );?>

			<nav aria-label="Page navigation" class="wpstream-pagination" role="navigation">
				<span class="sr-only"><?php echo esc_html__( 'Page navigation', 'hello-wpstream' ); ?></span>
				<div class="pagination">


				<div class="page-item pagination__link pagination__link--prev">
					<a class="page-link" href="<?php echo esc_url( get_pagenum_link( $current_page - 1 ) ); ?>">
						<?php echo wpstream_theme_get_svg_icon( 'pagination_arrow_left.svg' ); ?>

						<?php esc_html_e( 'Previous', 'hello-wpstream' ); ?>
					</a>
				</div>
		
				<?php
		
		
				foreach ( $pages as $page ) {
					$page = str_replace( 'page-numbers', 'page-numbers page-link', $page );
					//echo "<li class='page-item'>$page</li>";
					?>
					<div class="page-item pagination__page-item">
						<?php echo wp_kses_post($page) ;?>			
					</div>
				<?php			
				}
				?>
			



				<?php
				if ( ( $current_page + 1 ) > $pages ) :
					$next_page = get_pagenum_link( $current_page + 1 );
					?>
					<?php $next_page = get_pagenum_link( $current_page ); ?>
				<?php else : ?>
					<?php $next_page = get_pagenum_link( $current_page + 1 ); ?>
				<?php endif; ?>


				<div class="page-item pagination__link pagination__link--next ">
						<a class="page-link" href="<?php echo esc_url( $next_page ); ?>">
							<?php esc_html_e( 'Next', 'hello-wpstream' ); ?>

							<?php echo wpstream_theme_get_svg_icon( 'pagination_arrow_right.svg' ); ?>
						</a>
					</div>
				</div>
			</nav>
		
	<?php	
	}
}
