<?php
defined( 'ABSPATH' ) || exit;

/**
 * Get index loop type.
 *
 * @todo rename to wpex_get_archive_loop_type?
 */
function wpex_get_index_loop_type() {
	$loop_type = '';

	if ( wpex_is_blog_query() ) {
		$loop_type = 'blog';
	} elseif ( is_search() ) {
		$search_style = wpex_search_results_style();
		$loop_type = ( 'default' === $search_style ) ? 'search' : $search_style;
	} elseif ( is_tax() ) {
		switch ( get_query_var( 'taxonomy' ) ) {
			case 'portfolio_tag':
			case 'portfolio_category':
				$loop_type = 'portfolio';
				break;
			case 'staff_tag':
			case 'staff_category':
				$loop_type = 'staff';
				break;
			case 'testimonials_tag':
			case 'testimonials_category':
				$loop_type = 'testimonials';
				break;
		}
	}

	elseif ( is_post_type_archive() ) {
		$loop_type = get_query_var( 'post_type' );
	}

	if ( empty( $loop_type ) ) {
		$loop_type = get_post_type(); // fallback
	}

	/**
	 * Filters the current index.php loop type.
	 *
	 * @param string $loop_type
	 */
	$loop_type = apply_filters( 'wpex_get_index_loop_type', $loop_type );

	return $loop_type;
}

/**
 * Returns true if the current Query is a query related to standard blog posts.
 */
function wpex_is_blog_query() {
	$check = false;

	if ( is_search() ) {
		if ( 'blog' === wpex_search_results_style() ) {
			$check = true;
		} else {
			$check = false;
		}
	} elseif (
		is_home()
		|| is_category()
		|| is_tag()
		|| is_date()
		|| is_author()
		|| is_page_template( 'templates/blog.php' )
		|| is_page_template( 'templates/blog-content-above.php' )
		|| ( is_tax( 'post_series' ) && 'post' == get_post_type() )
		|| ( is_tax( 'post_format' ) && 'post' == get_post_type() )
	) {
		$check = true;
	}

	/**
	 * Filters whether the current page is a blog query or not.
	 *
	 * @param bool $check
	 */
	$check = (bool) apply_filters( 'wpex_is_blog_query', $check );

	return $check;
}

/**
 * Return archive grid style.
 */
function wpex_archive_grid_style() {
	$style = 'fit-rows';

	if ( WPEX_PTU_ACTIVE ) {

		$is_cpt_archive = is_post_type_archive();
		$loadmore       = \TotalTheme\Pagination\Load_More::get_data();

		if ( $loadmore ) {
			$taxonomy       = $loadmore['query_vars']['taxonomy'] ?? null;
			$post_type      = $loadmore['query_vars']['post_type'] ?? null;
			$is_cpt_archive = $taxonomy ? false : true;
		}

		if ( $is_cpt_archive ) {
			$post_type = $post_type ?? get_query_var( 'post_type' );
			$ptu_check = wpex_get_ptu_type_mod( $post_type, 'archive_grid_style' );
			if ( $ptu_check ) {
				$style = $ptu_check;
			}
		}

		if ( ! empty( $taxonomy ) || is_tax() ) {
			$taxonomy = $taxonomy ?? get_query_var( 'taxonomy' );
			$ptu_check = wpex_get_ptu_tax_mod( $taxonomy, 'grid_style' );
			if ( $ptu_check ) {
				$style = $ptu_check;
			}
		}
	}

	/**
	 * Filters the archive grid style.
	 *
	 * @param string $style
	 */
	$style = apply_filters( 'wpex_archive_grid_style', $style );

	return $style;
}

/**
 * Return archive grid gap.
 */
function wpex_archive_grid_gap() {
	$gap = '';

	if ( WPEX_PTU_ACTIVE ) {
		if ( is_post_type_archive() ) {
			$ptu_check = wpex_get_ptu_type_mod( get_query_var( 'post_type' ), 'archive_grid_gap' );
			if ( $ptu_check ) {
				$gap = $ptu_check;
			}
		}
		if ( is_tax() ) {
			$ptu_check = wpex_get_ptu_tax_mod( get_query_var( 'taxonomy' ), 'grid_gap' );
			if ( $ptu_check ) {
				$gap = $ptu_check;
			}
		}
	}

	/**
	 * Filters the archive grid gap.
	 *
	 * @param string $gap
	 */
	$gap = apply_filters( 'wpex_archive_grid_gap', $gap );

	return $gap;
}

/**
 * Returns correct classes for archive grid.
 */
function wpex_get_archive_grid_class() {
	$classes = array(
		'archive-grid',
		'entries',
		'wpex-row',
	);

	$grid_style = wpex_archive_grid_style();

	if ( 'masonry' === $grid_style || 'no-margins' === $grid_style ) {
		$classes[] = 'wpex-masonry-grid';
		wpex_enqueue_isotope_scripts(); // This is a good spot to enqueue grid scripts
	}

	if ( $gap = wpex_archive_grid_gap() ) {
		$classes[] = wpex_gap_class( $gap );
	}

	$classes[] = 'wpex-clr';

	$classes = apply_filters( 'wpex_get_archive_grid_class', $classes ); // legacy

	/**
	 * Filters the archive grid class.
	 *
	 * @param array $class
	 */
	$classes = (array) apply_filters( 'wpex_archive_grid_class', $classes ); // @since 5.0

	return implode( ' ', $classes );
}

/**
 * Returns correct grid columns for custom types.
 *
 * @todo rename to wpex_cpt_entry_columns for consistency?
 * @todo cast return value to (int) then locate any (int) wpex_get_grid_entry_columns and remove casting.
 */
function wpex_get_grid_entry_columns() {
	$columms = '1';

	$is_cpt_archive = is_post_type_archive();
	$post_type      = get_post_type();
	$loadmore       = \TotalTheme\Pagination\Load_More::get_data();

	if ( $loadmore ) {
		$taxonomy       = $loadmore['query_vars']['taxonomy'] ?? null;
		$post_type      = $loadmore['query_vars']['post_type'] ?? $post_type;
		$is_cpt_archive = $taxonomy ? false : true;
	}

	if ( $is_cpt_archive ) {
		$columms = get_theme_mod( $post_type . '_grid_entry_columns', $columms );
	}

	if ( WPEX_PTU_ACTIVE ) {
		if ( $is_cpt_archive ) {
			$ptu_check = wpex_get_ptu_type_mod( $post_type, 'archive_grid_columns' );
			if ( $ptu_check ) {
				$columms = $ptu_check;
				return $columms;
			}
		}
		if ( ! empty( $taxonomy ) || is_tax() ) {
			$taxonomy = $taxonomy ?? get_query_var( 'taxonomy' );
			$ptu_check = wpex_get_ptu_tax_mod( $taxonomy, 'grid_columns' );
			if ( $ptu_check ) {
				$columms = $ptu_check;
			}
		}
	}

	/**
	 * Filters the grid entry columns
	 *
	 * @param string $columns
	 */
	$columms = apply_filters( 'wpex_get_grid_entry_columns', $columms );

	return $columms;
}

/**
 * Returns classes for archive grid entries.
 */
function wpex_get_archive_grid_entry_class() {
	$classes = array(
		'cpt-entry',
		'wpex-clr'
	);

	$columns = wpex_get_grid_entry_columns();

	if ( $columns ) {

		$col_class = wpex_row_column_width_class( $columns );

		if ( $col_class ) {

			$classes[] = 'col';

			$classes[] = $col_class;

			$counter = wpex_get_loop_counter();

			if ( $counter ) {
				$classes[] = 'col-' . sanitize_html_class( $counter );
			}

		}

	}

	$grid_style = wpex_archive_grid_style();

	if ( absint( $columns ) > 1 && 'masonry' === $grid_style || 'no-margins' === $grid_style ) {
		$classes[] = 'wpex-masonry-col';
	}

	/**
	 * Filters the archive grid classes.
	 *
	 * @param array $classes
	 */
	$classes = apply_filters( 'wpex_get_archive_grid_entry_class', $classes );

	return $classes;
}

/**
 * Get term description location.
 */
function wpex_term_description_location() {
	if ( is_category() || is_tag() ) {
		$location = get_theme_mod( 'category_description_position' );
	} else {

		if ( wpex_is_woo_tax() ) {
			$location = get_theme_mod( 'woo_category_description_position' );
		}

		if ( WPEX_PTU_ACTIVE ) {
			$ptu_check = wpex_get_ptu_tax_mod( get_query_var( 'taxonomy' ), 'term_description_position' );
			if ( $ptu_check ) {
				$location = $ptu_check;
			}
		}

	}

	if ( empty( $location ) ) {
		$location = wpex_has_page_header() ? 'under_title' : 'above_loop'; // @todo change to page_subheading? and add default mod option.
	}

	/**
	 * Filters the taxonomy archive term description location.
	 *
	 * @param string $location
	 */
	$location = apply_filters( 'wpex_term_description_location', $location );

	return $location;
}

/**
 * Check if term description should display above the loop.
 *
 * By default the term description displays in the subheading in the page header,
 * however, there are some built-in settings to enable the term description above the loop.
 * This function returns true if the term description should display above the loop and not in the header.
 */
function wpex_has_term_description_above_loop() {
	$check = false;

	$location = wpex_term_description_location();

	if ( 'above_loop' === $location ) {
		$check = true;
	}

	/**
	 * Filters whether the term archive description is above the loop.
	 *
	 * @param bool $check
	 */
	$check = (bool) apply_filters( 'wpex_has_term_description_above_loop', $check );

	return $check;
}

/**
 * Check if page header image is enabled for term.
 *
 * This function is only currently used in the Total Theme Core plugin.
 *
 * @todo rename to wpex_has_term_page_header_image
 */
function wpex_term_page_header_image_enabled( $term_id = '' ) {
	if ( ! get_theme_mod( 'term_page_header_image_enable', true ) ) {
		return;
	}

	$check = true;

	// Disable for WooCommerce by default.
	if ( wpex_is_woo_tax() ) {
		$check = get_theme_mod( 'woo_shop_term_page_header_image_enabled', false );
	}

	if ( WPEX_PTU_ACTIVE ) {
		$ptu_check = wpex_get_ptu_tax_mod( get_query_var( 'taxonomy' ), 'term_page_header_image_enabled' );
		if ( isset( $ptu_check ) ) {
			$check = wp_validate_boolean( $ptu_check );
		}
	}

	/**
	 * Filters whether the term archive page header image is enabled.
	 *
	 * @param bool $check
	 */
	$check = (bool) apply_filters( 'wpex_term_page_header_image_enabled', $check );

	// Get term id.
	$term_id = $term_id ?: get_queried_object_id();

	// Term id isn't empty so lets locate the thumbnail.
	if ( $term_id ) {

		$meta_check = get_term_meta( $term_id, 'page_header_bg', true );

		if ( ! isset( $meta_check ) ) {

			// Get data.
			$term_data = get_option( 'wpex_term_data' );
			$term_data = ! empty( $term_data[ $term_id ] ) ? $term_data[ $term_id ] : '';

			// Check setting.
			if ( $term_data && isset( $term_data['page_header_bg'] ) ) {
				$meta_check = $term_data['page_header_bg'];
			}

		}

		// Validate meta.
		if ( is_bool( $meta_check ) ) {
			$check = $meta_check;
		} elseif ( is_string( $meta_check ) && '' !== $meta_check ) {
			$meta_check = strtolower( $meta_check );
			if ( in_array( $meta_check, array( 'false', 'off', 'disabled' ) ) ) {
				$meta_check = false;
			}
			if ( in_array( $meta_check, array( 'true', 'on', 'enabled' ) ) ) {
				$meta_check = true;
			}
			$check = $meta_check;
		}

	}

	return (bool) $check;
}