<?php
/**
 * Woodmart Size Guide Module
 *
 * @package woodmart
 */

use XTS\Gutenberg\Blocks_Assets;
use XTS\Gutenberg\Post_CSS;

if ( ! defined( 'WOODMART_THEME_DIR' ) ) {
	exit( 'No direct script access allowed' );
}

/**
 * ------------------------------------------------------------------------------------------------
 * Actions
 * ------------------------------------------------------------------------------------------------
 */
// Save Edit Table Action
add_action( 'save_post_woodmart_size_guide', 'woodmart_sguide_table_save' );
add_action( 'edit_post_woodmart_size_guide', 'woodmart_sguide_table_save' );

add_action( 'save_post_woodmart_size_guide', 'woodmart_sguide_hide_table_save' );
add_action( 'edit_post_woodmart_size_guide', 'woodmart_sguide_hide_table_save' );

// Save Edit Product Action
add_action( 'save_post', 'woodmart_sguide_dropdown_save' );
add_action( 'edit_post', 'woodmart_sguide_dropdown_save' );

// Add size guide to product page
add_action( 'woocommerce_single_product_summary', 'woodmart_sguide_display', 38 );


// Metaboxes template
if ( ! function_exists( 'woodmart_sguide_metaboxes' ) ) {
	/**
	 * Size guide metaboxes template
	 *
	 * @param WP_Post $post Current post object.
	 */
	function woodmart_sguide_metaboxes( $post ) {

		if ( 'add' === get_current_screen()->action ) {
			$tables = array(
				array( 'Size', 'UK', 'US', 'EU', 'Japan' ),
				array( 'XS', '6 - 8', '4', '34', '7' ),
				array( 'S', '8 -10', '6', '36', '9' ),
				array( 'M', '10 - 12', '8', '38', '11' ),
				array( 'L', '12 - 14', '10', '40', '13' ),
				array( 'XL', '14 - 16', '12', '42', '15' ),
				array( 'XXL', '16 - 28', '14', '44', '17' ),
			);
		} else {
			$tables = get_post_meta( $post->ID, 'woodmart_sguide', true );
		}

		if ( ! $tables ) {
			$tables = array(
				array( 'Size' ),
				array( 'XS' ),
			);
		}

		woodmart_sguide_table_template( $tables );
	}
}

/**
 * ------------------------------------------------------------------------------------------------
 * Table
 * ------------------------------------------------------------------------------------------------
 */
// Table template
if ( ! function_exists( 'woodmart_sguide_table_template' ) ) {
	/**
	 * Size guide table template
	 *
	 * @param array $tables Size guide tables.
	 */
	function woodmart_sguide_table_template( $tables ) {
		wp_enqueue_script( 'wd-edit-table', WOODMART_ASSETS . '/js/libs/jquery.edittable.min.js', array(), WOODMART_VERSION, true );
		wp_enqueue_style( 'woodmart-edittable-style', WOODMART_ASSETS . '/css/jquery.edittable.min.css', array(), WOODMART_VERSION );
		wp_enqueue_style( 'wd-admin-page-size-guides', WOODMART_ASSETS . '/css/parts/page-size-guides.min.css', array(), WOODMART_VERSION );
		?>
		<textarea class="woodmart-sguide-table-edit" name="woodmart-sguide-table" style="display:none;">
			<?php echo wp_json_encode( $tables ); ?>
		</textarea>
		<?php
	}
}

// Save table action
if ( ! function_exists( 'woodmart_sguide_table_save' ) ) {
	/**
	 * Size guide table save
	 *
	 * @param int $post_id Current post ID.
	 */
	function woodmart_sguide_table_save( $post_id ) {
		if ( ! isset( $_POST['woodmart-sguide-table'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
			return;
		}

		$size_guide = json_decode( stripslashes( $_POST['woodmart-sguide-table'] ) );  // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

		update_post_meta( $post_id, 'woodmart_sguide', $size_guide );

		// Save product category
		woodmart_sguide_save_category( $post_id );
	}
}

/**
 * ------------------------------------------------------------------------------------------------
 * Dropdown
 * ------------------------------------------------------------------------------------------------
 */
// Dropdown template
if ( ! function_exists( 'woodmart_sguide_dropdown_template' ) ) {
	/**
	 * Size guide dropdown template
	 *
	 * @param WP_Post $post Current post object.
	 */
	function woodmart_sguide_dropdown_template( $post ) {
		$arg = array(
			'post_type'   => 'woodmart_size_guide',
			'numberposts' => -1,
		);

		$sguide_list    = get_posts( $arg );
		$sguide_post_id = get_post_meta( $post->ID, 'woodmart_sguide_select', true );
		?>
			<select name="woodmart_sguide_select">
				<option value="none" <?php selected( 'none', $sguide_post_id ); ?>><?php esc_html_e( '— None —', 'woodmart' ); ?></option>
				
				<?php foreach ( $sguide_list as $sguide_post ) : ?>
					<option value="<?php echo esc_attr( $sguide_post->ID ); ?>" <?php selected( $sguide_post_id, $sguide_post->ID ); ?>>
						<?php echo wp_kses( $sguide_post->post_title, woodmart_get_allowed_html() ); ?>
					</option>
				<?php endforeach; ?>
				
			</select><br><br>
			
			<label>
				<input type="checkbox" name="woodmart_disable_sguide" id="woodmart_disable_sguide" <?php checked( 'disable', $sguide_post_id, true ); ?>> 
				<?php esc_html_e( 'Hide size guide from this product', 'woodmart' ); ?>
			</label>
		<?php
	}
}

// Dropdown Save
if ( ! function_exists( 'woodmart_sguide_dropdown_save' ) ) {
	/**
	 * Size guide dropdown save
	 *
	 * @param int $post_id Current post ID.
	 */
	function woodmart_sguide_dropdown_save( $post_id ) {
		if ( isset( $_POST['woodmart_sguide_select'] ) && $_POST['woodmart_sguide_select'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
			if ( isset( $_POST['woodmart_disable_sguide'] ) && 'on' === $_POST['woodmart_disable_sguide'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
				update_post_meta( $post_id, 'woodmart_sguide_select', 'disable' );
			} else {
				update_post_meta( $post_id, 'woodmart_sguide_select', sanitize_text_field( $_POST['woodmart_sguide_select'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
			}
		}
	}
}

/**
 * ------------------------------------------------------------------------------------------------
 * Display
 * ------------------------------------------------------------------------------------------------
 */

// Size guide display.
if ( ! function_exists( 'woodmart_sguide_display' ) ) {
	/**
	 * Size guide display
	 *
	 * @param int   $post_id Current post ID.
	 * @param array $args Additional arguments.
	 */
	function woodmart_sguide_display( $post_id = false, $args = array() ) {
		$post_id        = ( $post_id ) ? $post_id : get_the_ID();
		$sguide_post_id = get_post_meta( $post_id, 'woodmart_sguide_select', true );

		if ( ! empty( $sguide_post_id ) && 'disable' === $sguide_post_id ) {
			return;
		}

		if ( ! empty( $sguide_post_id ) && 'none' !== $sguide_post_id ) {
			$sguide_post_id = $sguide_post_id;
		} else {
			$sguide_post_id = '';
			$terms          = wp_get_post_terms( $post_id, 'product_cat' );
			if ( $terms ) {
				foreach ( $terms as $term ) {
					if ( get_term_meta( $term->term_id, 'woodmart_chosen_sguide', true ) ) {
						$sguide_post_id = get_term_meta( $term->term_id, 'woodmart_chosen_sguide', true );
					}
				}
			}
		}

		$sguide_post_id = apply_filters( 'wpml_object_id', $sguide_post_id, 'woodmart_size_guide', true );

		if ( $sguide_post_id ) {
			$sguide_post = get_post( $sguide_post_id );
			$size_tables = get_post_meta( $sguide_post_id, 'woodmart_sguide', true );

			woodmart_sguide_display_table_template( $sguide_post, $size_tables, $args );
		}
	}
}

// Size guide display template.
if ( ! function_exists( 'woodmart_sguide_display_table_template' ) ) {
	/**
	 * Render size guide popup markup (content and optional table) for the provided size guide post.
	 *
	 * @param WP_Post $sguide_post  Size guide post object.
	 * @param array   $size_tables  Size guide tables data.
	 * @param array   $args         Additional arguments.
	 *
	 * @return void
	 */
	function woodmart_sguide_display_table_template( $sguide_post, $size_tables, $args = array() ) {
		$is_quick_view = woodmart_loop_prop( 'is_quick_view' );

		if ( ! isset( $args['builder_classes'] ) || empty( $args['builder_classes'] ) ) {
			$args['builder_classes'] = ' wd-style-text';
		}

		if ( ! woodmart_get_opt( 'size_guides' ) || $is_quick_view || ! $size_tables || ! $sguide_post ) {
			return;
		}

		$show_table = get_post_meta( $sguide_post->ID, 'woodmart_sguide_hide_table', true );
		$show_table = $show_table ? $show_table : 'show';

		woodmart_enqueue_js_library( 'magnific' );
		woodmart_enqueue_js_script( 'popup-element' );

		woodmart_enqueue_inline_style( 'mfp-popup' );
		woodmart_enqueue_inline_style( 'size-guide' );
		woodmart_enqueue_inline_style( 'mod-animations-transform' );
		woodmart_enqueue_inline_style( 'mod-transform' );

		if ( woodmart_is_gutenberg_blocks_enabled() && has_blocks( $sguide_post->post_content ) ) {
			echo Blocks_Assets::get_instance()->get_inline_scripts( $sguide_post->ID ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo Post_CSS::get_instance()->get_inline_blocks_css( $sguide_post->ID ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}
		$wpb_shortcodes_custom_css      = get_post_meta( $sguide_post->ID, '_wpb_shortcodes_custom_css', true );
		$woodmart_shortcodes_custom_css = get_post_meta( $sguide_post->ID, 'woodmart_shortcodes_custom_css', true );

		?>
			<?php if ( $wpb_shortcodes_custom_css || $woodmart_shortcodes_custom_css ) : ?>
				<style data-type="vc_shortcodes-custom-css">
					<?php echo $wpb_shortcodes_custom_css; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<?php echo $woodmart_shortcodes_custom_css; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				/* */
				</style>
			<?php endif; ?>
			<div id="wd_sizeguide" data-wrap-class="wd-sizeguide-wrap" class="mfp-hide wd-popup wd-popup-element wd-sizeguide wd-scroll-content">
				<h4 class="wd-sizeguide-title">
					<?php echo esc_html( $sguide_post->post_title ); ?>
				</h4>
				<div class="wd-sizeguide-content">
					<?php if ( has_blocks( $sguide_post->post_content ) ) : ?>
						<?php echo do_shortcode( do_blocks( $sguide_post->post_content ) ); ?>
					<?php else : ?>
						<?php echo do_shortcode( $sguide_post->post_content ); ?>
					<?php endif; ?>
				</div>
				<?php if ( 'show' === $show_table ) : ?>
					<div class="responsive-table">
						<table class="wd-sizeguide-table">
							<?php foreach ( $size_tables as $row ) : ?>
								<tr>
									<?php foreach ( $row as $col ) : ?>
										<td>
											<?php echo esc_html( $col ); ?>
										</td>
									<?php endforeach; ?>
								</tr>
							<?php endforeach; ?>
						</table>
					</div>
				<?php endif; ?>
			</div>

			<div class="wd-sizeguide-btn wd-action-btn wd-sizeguide-icon<?php echo esc_attr( $args['builder_classes'] ); ?>">
				<a class="wd-open-popup" rel="nofollow" href="#wd_sizeguide">
					<span class="wd-action-icon"></span>
					<span class="wd-action-text">
						<?php esc_html_e( 'Size guide', 'woodmart' ); ?>
					</span>
				</a>
			</div>
		<?php
	}
}

/**
 * ------------------------------------------------------------------------------------------------
 * Category
 * ------------------------------------------------------------------------------------------------
 */

// Size guide save category
if ( ! function_exists( 'woodmart_sguide_save_category' ) ) {
	/**
	 * Size guide save category
	 *
	 * @param int $post_id Current post ID.
	 */
	function woodmart_sguide_save_category( $post_id ) {
		if ( isset( $_POST['woodmart_sguide_category'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
			$selected_sguide_category = woodmart_clean( $_POST['woodmart_sguide_category'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
			update_post_meta( $post_id, 'woodmart_chosen_cats', $selected_sguide_category );

			$terms = get_terms( 'product_cat' );
			foreach ( $selected_sguide_category as $selected_sguide_cat ) {
				update_term_meta( $selected_sguide_cat, 'woodmart_chosen_sguide', $post_id );
			}
			foreach ( $terms as $term ) {
				if ( ! in_array( absint( $term->term_id ), array_map( 'absint', $selected_sguide_category ), true ) ) {
					if ( get_term_meta( $term->term_id, 'woodmart_chosen_sguide', true ) === $post_id ) {
						update_term_meta( $term->term_id, 'woodmart_chosen_sguide', '' );
					}
				}
			}
		} else {
			update_post_meta( $post_id, 'woodmart_chosen_cats', '' );
			$terms = get_terms( 'product_cat' );
			foreach ( $terms as $term ) {
				if ( get_term_meta( $term->term_id, 'woodmart_chosen_sguide', true ) === $post_id ) {
					update_term_meta( $term->term_id, 'woodmart_chosen_sguide', '' );
				}
			}
		}
	}
}

// Size guide category template
if ( ! function_exists( 'woodmart_sguide_category_template' ) ) {
	/**
	 * Size guide category template
	 *
	 * @param WP_Post $post Current post object.
	 */
	function woodmart_sguide_category_template( $post ) {
		$arg = array(
			'taxonomy'     => 'product_cat',
			'orderdby'     => 'name',
			'hierarchical' => 1,
			'hide_empty'   => false,
		);

		$chosen_cats     = get_post_meta( $post->ID, 'woodmart_chosen_cats', true );
		$sguide_cat_list = get_categories( $arg );
		?>
		<ul>
			<?php foreach ( $sguide_cat_list as $sguide_cat ) : ?>
				<?php
				$checked = false;

				if ( is_array( $chosen_cats ) && in_array( absint( $sguide_cat->term_id ), array_map( 'absint', $chosen_cats ), true ) ) {
					$checked = 'checked';
				}
				?>
				<li>
					<input type="checkbox" name="woodmart_sguide_category[]" value="<?php echo esc_attr( $sguide_cat->term_id ); ?>" <?php echo esc_attr( $checked ); ?>>
					<?php echo wp_kses( $sguide_cat->name, woodmart_get_allowed_html() ); ?>
				</li>
			<?php endforeach; ?>
		</ul>
		<?php
	}
}

/**
 * ------------------------------------------------------------------------------------------------
 * Hide table
 * ------------------------------------------------------------------------------------------------
 */
// Size guide hide table template
if ( ! function_exists( 'woodmart_sguide_hide_table_template' ) ) {
	/**
	 * Size guide hide table template
	 *
	 * @param WP_Post $post Current post object.
	 */
	function woodmart_sguide_hide_table_template( $post ) {
		$disable_table = get_post_meta( $post->ID, 'woodmart_sguide_hide_table', true );
		$disable_table = $disable_table ? $disable_table : 'show';
		?>
		<label>
			<input type="checkbox" name="woodmart_sguide_hide_table" id="woodmart_sguide_hide_table" <?php checked( 'hide', $disable_table, true ); ?> > 
			<?php esc_html_e( 'Hide size guide table', 'woodmart' ); ?>
		</label>
		<?php
	}
}
// Size guide hide table save
if ( ! function_exists( 'woodmart_sguide_hide_table_save' ) ) {
	/**
	 * Size guide hide table save
	 *
	 * @param int $post_id Current post ID.
	 */
	function woodmart_sguide_hide_table_save( $post_id ) {
		if ( isset( $_POST['woodmart_sguide_hide_table'] ) && 'on' === $_POST['woodmart_sguide_hide_table'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
			update_post_meta( $post_id, 'woodmart_sguide_hide_table', 'hide' );
		} else {
			update_post_meta( $post_id, 'woodmart_sguide_hide_table', 'show' );
		}
	}
}
