<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Prodigy\Includes\Content\Prodigy_Request_Maker;
use Prodigy\Includes\Frontend\Pages\Prodigy_Page;
use Prodigy\Includes\Helpers\Prodigy_Url_Helper;
use Prodigy\Includes\Models\Prodigy_Brands;
use Prodigy\Includes\Prodigy;
use Prodigy\Includes\Prodigy_Product_Comments;
use Prodigy\Includes\Helpers\Prodigy_Formatting;
use Prodigy\Includes\Helpers\Prodigy_Page as Page_Helper;

/** remove wp_ob_end_flush errors */
remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 );

add_filter( 'template_redirect', 'reset_plugin_template' );
add_filter( 'comment_form_defaults', 'prodigy_isa_comment_reform' );
add_action( 'comment_form', 'prodigy_comment_nonce_to_form' );
add_action( 'widgets_init', 'prodigy_register_shop_sidebar' );
add_action( 'admin_menu', 'hide_the_add_new_menu_item' );
add_filter( 'body_class', 'body_cart_page_classes', 11 );
add_action( 'transition_comment_status', 'my_approve_comment_callback', 10, 3 );


/**
 * Its help for problems with cookie settings
 *
 * @return void
 */
function reset_plugin_template() {
	// Skip for Elementor proxy requests
	// Hide elementor errors
	if ( isset( $_GET['screenshot_proxy'] ) ) {
		return;
	}

	ob_start( null, 0, 0 );
}

/**
 * Change standard review title
 *
 * @param  array $arg
 *
 * @return array
 */
function prodigy_isa_comment_reform( array $arg ): array {

	if ( Page_Helper::prodigy_is_blog() ) {
		$arg['title_reply'] = __( 'Leave a Comment', 'prodigy-commerce' );
	}
	if ( isset( $GLOBALS['prodigy_product'] ) ) {
		$product = $GLOBALS['prodigy_product'];
		if ( $product ) {

			$title = sprintf(
				// translators: %1$s is the product name.
				__( 'My Review for %1$s', 'prodigy-commerce' ),
				$product->get_field( 'post_title' )
			);
			$arg['title_reply'] = $title;
		}
	}
	return $arg;
}

/**
 * Show a shop page content on selected page.
 */
function prodigy_container_shop_block() {

	// Don't display the description on search results page.
	if ( is_search() ) {
		return;
	}

	$info_shop_page = Page_Helper::prodigy_get_page_id( 'shop' );

	/*
	 * if page is not "shop" then don't display the content page
	 */
	if ( empty( $info_shop_page ) ) {
		return;
	}

	$shop_page = false;
	$part      = Prodigy_Url_Helper::prodigy_get_request_uri();

	if ( ! Page_Helper::prodigy_check_shop_page() && ! strpos( $part, 'shop' ) ) {
		$shop_page = get_post( $info_shop_page );
	}

	if ( $shop_page && Prodigy_Url_Helper::is_archive_page_url( $part ) ) {
		$page_content = Prodigy_Formatting::prodigy_format_content( $shop_page->post_content );
		if ( $page_content ) {
			echo '<div class="container prodigy-container-description">' . esc_attr( $page_content ) . '</div>';
		}
	}
}

/**
 * Add sidebar for shop page

 * @return void
 */
function prodigy_register_shop_sidebar() {
	register_sidebar(
		array(
			'id'            => 'prodigy_shop_sidebar',
			'name'          => 'Shop Sidebar',
			'description'   => 'Left sidebar for prodigy widgets',
			'before_widget' => '<div class="prodigy-filter__main">',
			'after_widget'  => '</div>',
			'before_title'  => '<p class="prodigy-filter__title">',
			'after_title'   => '</p>',
		)
	);
}

/**
 * Hide certain item of menu

 * @return void
 */
function hide_the_add_new_menu_item() {
	global $submenu;
	unset( $submenu[ 'edit.php?post_type=' . Prodigy::get_prodigy_product_type() ][10] );
}

/**
 * @param  array  $sidebars_widgets
 *
 * @return array
 */

/**
 * @param  array $classes
 *
 * @return array
 */
function body_cart_page_classes( array $classes ): array {
	$current_page_id = get_queried_object_id();
	$post_type       = get_post_type( $current_page_id );

	$classes = add_class_for_pages( $current_page_id, $post_type, $classes );
	$classes = add_class_for_elementor( $classes );
	$classes = add_class_for_brands( $classes );

	return $classes;
}

/**
 * @param   array $classes
 *
 * @return array
 */
function add_class_for_brands( array $classes ): array {
	$brand_slug = Prodigy_Url_Helper::get_brand_slug();
	$classes[]  = Prodigy_Brands::BRAND_PREFIX . $brand_slug;

	return $classes;
}

/**
 * @param   array $classes
 *
 * @return array
 */
function add_class_for_elementor( array $classes ): array {
	global $wp;

	$pagesIn = array(
		Prodigy_Page::PAGE_TYPE_SHOP,
		Prodigy_Page::PAGE_TYPE_THANK,
	);

	if ( empty( $wp->request ) ) {
		$classes[] = '';
	} elseif ( in_array( $wp->request, $pagesIn, true ) ) {
		$classes[] = '';
	} elseif ( is_plugin_active( 'elementor-pro/elementor-pro.php' ) ) {

		if ( did_action( 'elementor/loaded' ) ) {
			if ( in_array( 'elementor-template-full-width', $classes, true ) ) {
				$classes[] = 'prodigy-elementor-wrapper';
			}
		}
	}

	if ( in_array( 'elementor-template-full-width', $classes, true ) ) {
		$classes[] = 'prodigy-elementor-wrapper';
	}

	return $classes;
}

/**
 * @param   string $current_page_id
 * @param   string $post_type
 * @param   array  $classes
 *
 * @return array
 */
function add_class_for_pages( string $current_page_id, string $post_type, array $classes ): array {
	$prodigy_pages = array(
		Page_Helper::prodigy_get_page_id( 'cart' )  => Prodigy_Page::PRODIGY_CART_PAGE,
		Page_Helper::prodigy_get_page_id( 'thank' ) => Prodigy_Page::PRODIGY_THANK_PAGE,
		Page_Helper::prodigy_get_page_id( 'shop' )  => Prodigy_Page::PRODIGY_SHOP_PAGE,
	);

	if ( Page_Helper::is_prodigy_shop() ) {
		$classes[] = 'tax-prodigy-product-shop';
	}

	if ( isset( $prodigy_pages[ $current_page_id ] ) || $post_type === Prodigy::PRODIGY_POST_TYPE_DEFAULT || Page_Helper::is_prodigy_shop() ) {
		$classes[] = 'prodigy-wrapper-container';
	} else {
		$classes[] = '';
	}

	if ( isset( $prodigy_pages[ $current_page_id ] ) && $prodigy_pages[ $current_page_id ] === Prodigy_Page::PRODIGY_CART_PAGE ) {
		$classes[] = 'prodigy-entry-content-width';
	}

	return $classes;
}

/**
 * @param  string     $new_status  New comment status.
 * @param  string     $old_status  Previous comment status.
 * @param  WP_Comment $comment  Comment object.
 */
function my_approve_comment_callback( string $new_status, string $old_status, WP_Comment $comment ) {
	if ( $old_status !== $new_status ) {
		$average_rating    = Prodigy_Product_Comments::get_rating_average_product( (int) $comment->comment_post_ID );
		$remote_product_id = get_post_meta( $comment->comment_post_ID, Prodigy::PRODIGY_REMOTE_PRODUCT_ID, true );
		Prodigy_Request_Maker::get_instance()->do_update_product_rating_request( (int) $remote_product_id, (int) $average_rating );
	}
}

/**
 * Add nonce for comment form
 *
 * @return void
 */
function prodigy_comment_nonce_to_form() {
	wp_nonce_field( 'comment_nonce' );
}
