<?php

namespace FinchCart\Admin\Settings;

use WP_Query;

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

/**
 * Edith Init Class
 */
class EdithInit {

	/**
	 * Class Constructor.
	 */
	public function __construct() {
		$this->edith_custom_fields();
		add_filter( 'edith_shortcode_builder_enqueue_assets', array( $this, 'adding_edith_scripts' ) );
		add_filter( 'admin_body_class', array( $this, 'adding_edith_admin_body_class' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'edith_add_finch_cart_custom_scripts' ) );
		add_action( 'wp_ajax_fire_fc_product_select_search', array( $this, 'product_select_ajax_search' ) );
	}

	/**
	 * Edith scripts adding condition
	 *
	 * @param boolean $enqueue Condition for adding Edith scripts.
	 * @return boolean
	 */
	public function adding_edith_scripts( $enqueue ) {
		$screen = get_current_screen();
		if ( isset( $screen ) && 'toplevel_page_finch-cart-settings' === $screen->id ) {
			$enqueue = true;
		}
		return $enqueue;
	}

	/**
	 * Edith adding admin body classes for styling
	 *
	 * @param string $classes Space-separated list of CSS classes.
	 * @return string
	 */
	public function adding_edith_admin_body_class( $classes ) {
		$screen = get_current_screen();
		if ( isset( $screen ) && 'toplevel_page_finch-cart-settings' === $screen->id ) {
			$classes .= ' edith-plugin-settings-page edith_shortcode_edit_page ';
		}
		return $classes;
	}

	/**
	 * Edith Custom Fields
	 *
	 * @return void
	 */
	public function edith_custom_fields() {
		include_once __DIR__ . '/../EdithCustomFields/ProductSelect.php';
		include_once __DIR__ . '/../EdithCustomFields/ProNotice.php';
	}

	/**
	 * Add custom scripts for Finch Cart in Edith Framework
	 *
	 * @return void
	 */
	public function edith_add_finch_cart_custom_scripts() {
		wp_register_style( 'finch-cart-edith-custom-style', plugins_url( '/../assets/css/edith-finch-cart-custom.css', __FILE__ ), array(), '1.0' );
		wp_register_script( 'finch-cart-edith-custom-script', plugins_url( '/../assets/js/edith-finch-cart-custom.js', __FILE__ ), array( 'jquery', 'wp-util' ), '1.0', true );

		wp_localize_script(
			'finch-cart-edith-custom-script',
			'Finch_Cart_Vars',
			array(
				'nonce' => wp_create_nonce( 'finch-cart-admin-ajax-nonce' ),
			)
		);
	}

	/**
	 * Ajax Search for Edith custom feild product select
	 *
	 * @return mixed
	 */
	public function product_select_ajax_search() {
		check_ajax_referer( 'finch-cart-admin-ajax-nonce', '_wpnonce' );

		$output          = '';
		$search_query    = isset( $_POST['search_query'] ) ? sanitize_text_field( wp_unslash( $_POST['search_query'] ) ) : '';
		$search_category = isset( $_POST['search_category'] ) ? intval( $_POST['search_category'] ) : '';
		$options_name    = isset( $_POST['options_name'] ) ? sanitize_text_field( wp_unslash( $_POST['options_name'] ) ) : '';
		$current_post    = isset( $_POST['current_post'] ) ? intval( $_POST['current_post'] ) : '';

		$args = array(
			'posts_per_page' => 100,
			'post_type'      => 'product',
			'post__not_in'   => array( $current_post ),
		);

		if ( isset( $search_query ) && '' !== $search_query ) {
			$args['s'] = $search_query;
		}

		if ( isset( $search_category ) && '' !== $search_category && 0 !== $search_category ) {
			$args['tax_query'] = array(
				array(
					'taxonomy' => 'product_cat',
					'field'    => 'id',
					'terms'    => $search_category,
				),
			);
		}

		$the_query = new WP_Query( $args );

		if ( $the_query->have_posts() ) :
			ob_start();
			echo '<ul class="edith-enabled">';
			while ( $the_query->have_posts() ) :
				$the_query->the_post();
				$product       = wc_get_product( get_the_id() );
				$product_title = get_the_title( get_the_id() );
				$product_image = $product->get_image( array( 50, 50 ) );
				echo '<li><input type="hidden" name="' . esc_attr( $options_name ) . esc_attr( '[' . get_the_id() . ']' ) . '" value="' . esc_attr( $product_title ) . '"/><label>' . wp_kses_post( $product_image ) . esc_attr( $product_title ) . '</label></li>';
			endwhile;
			echo '</ul>';
			wp_reset_postdata();
			$output = ob_get_clean();
		endif;

		if ( $output ) {
			wp_send_json_success( $output );
		} else {
			wp_send_json_success( '<div class="edith-notice edith-notice-warning">' . esc_html__( 'Nothing found.', 'finch-cart-product-recommendations' ) . '</div>' );
		}
	}
}
