<?php

/**
 * The main functions of this plugin.
 *
 * @link       https://wpconfiguratorbuilder.com/
 * @since      1.0.0
 *
 * @package    wcb-configurator-builder
 * @subpackage wcb-configurator-builder/public
 */

if ( ! function_exists( 'wpcb_configurator_woocommerce_notices' ) ) {
	/**
	 * Print product configurator WooCommerce notices.
	 *
	 * @return void
	 */
	function wpcb_configurator_woocommerce_notices() {
		if ( ! function_exists( 'wc_print_notices' ) ) {
			return;
		}
		if ( is_product() ) {
			return;
		}
		wc_print_notices();
	}
}

if ( ! function_exists( 'wpcb_template_gallery_wrapper' ) ) {
	/**
	 * Product configurator gallery wrapper
	 *
	 * @return void
	 */
	function wpcb_template_gallery_wrapper() {
		global $wpcb_configuration_id;

		$class = ' visuals-' . get_post_meta( $wpcb_configuration_id, '_template_width', true );
		if ( 'sticky' == get_post_meta( $wpcb_configuration_id, '_gallery_sticky', true ) ) {
			$class .= ' visuals-sticky';
		}

		echo '<div class="wpcb-configurator-visuals' . esc_attr( $class ) . '" data-wpcb-id="' . esc_attr( $wpcb_configuration_id ) . '">';
	}
}

if ( ! function_exists( 'wpcb_get_template_shortcode_class' ) ) {
	/**
	 * Product configurator template class
	 *
	 * @return string $class
	 */
	function wpcb_get_template_shortcode_class() {
		global $wpcb_configuration_id;

		$template_layout = get_post_meta( $wpcb_configuration_id, '_template', true );

		$class = 'wpcb-template-shortcode wpcb-template-shortcode-' . $template_layout;

		if ( 'right' === get_post_meta( $wpcb_configuration_id, '_gallery_position', true ) ) {
			$class .= ' wpcb-configurator-visuals-right';
		}

		if ( wpcb_get_option( 'wpcb_border_style' ) ) {
			$class .= ' wpcb-border-' . wpcb_get_option( 'wpcb_border_style' );
		}

		if ( get_post_meta( $wpcb_configuration_id, '_template_height', true ) ) {
			$class .= ' wpcb-template-height-' . get_post_meta( $wpcb_configuration_id, '_template_height', true );
		}

		if ( get_post_meta( $wpcb_configuration_id, '_gallery_sticky', true ) ) {
			$class .= ' wpcb-template-gallery-' . get_post_meta( $wpcb_configuration_id, '_gallery_sticky', true );
		}

		if ( 'fullwidth' === get_post_meta( $wpcb_configuration_id, '_template_width', true ) ) {
			$class .= ' alignfull';
		}

		if ( is_rtl() ) {
			$class .= ' wpcb-is-rtl';
		}

		return $class;
	}
}

if ( ! function_exists( 'wpcb_template_gallery_actions' ) ) {
	/**
	 * Product configurator gallery actions
	 *
	 * @return void
	 */
	function wpcb_template_gallery_actions() {
		global $wpcb_configuration_id;
		include WPCB_BASE . 'public/blocks/gallery-actions.php';
	}
}

if ( ! function_exists( 'wpcb_template_gallery_main' ) ) {
	/**
	 * Product configurator gallery main
	 *
	 * @return void
	 */
	function wpcb_template_gallery_main() {
		global $wpcb_configuration_id;
		include WPCB_BASE . 'public/blocks/gallery.php'; 
	}
}

if ( ! function_exists( 'wpcb_template_gallery_nav' ) ) {
	/**
	 * Product configurator gallery nav
	 *
	 * @return void
	 */
	function wpcb_template_gallery_nav() {
		global $wpcb_configuration_id;
		include WPCB_BASE . 'public/blocks/gallery-nav.php'; 
	}
}

if ( ! function_exists( 'wpcb_template_gallery_wrapper_close' ) ) {
	/**
	 * Product configurator gallery wrapper close
	 *
	 * @return void
	 */
	function wpcb_template_gallery_wrapper_close() {
		echo '</div>';
	}
}

if ( ! function_exists( 'wpcb_template_form_product_summary' ) ) {
	/**
	 * Product configurator template product details
	 *
	 * @return void
	 */
	function wpcb_template_form_product_summary() {
		global $wpcb_configuration_id;
		include WPCB_BASE . 'public/blocks/product-summary.php';
	}
}

if ( ! function_exists( 'wpcb_template_form_step_list' ) ) {
	/**
	 * Product configurator template form steps
	 *
	 * @return void
	 */
	function wpcb_template_form_step_list() {
		global $wpcb_configuration_id;
		include WPCB_BASE . 'public/blocks/step-list.php';
	}
}

if ( ! function_exists( 'wpcb_template_form_options_list' ) ) {
	/**
	 * Product configurator template form options
	 *
	 * @return void
	 */
	function wpcb_template_form_options_list() {
		global $wpcb_configuration_id;
		include WPCB_BASE . 'public/blocks/form-options.php';
	}
}

if ( ! function_exists( 'wpcb_template_form_step_buttons' ) ) {
	/**
	 * Product configurator template step buttons
	 *
	 * @return void
	 */
	function wpcb_template_form_step_buttons() {
		global $wpcb_configuration_id;
		include WPCB_BASE . 'public/blocks/step-buttons.php';
	}
}

if ( ! function_exists( 'wpcb_template_form_totals' ) ) {
	/**
	 * Product configurator template form totals
	 *
	 * @return void
	 */
	function wpcb_template_form_totals() {
		global $wpcb_configuration_id;
		include WPCB_BASE . 'public/blocks/form-totals.php';
	}
}

if ( ! function_exists( 'wpcb_template_form_actions' ) ) {
	/**
	 * Product configurator template form actions
	 *
	 * @return void
	 */
	function wpcb_template_form_actions() {
		global $wpcb_configuration_id;
		include WPCB_BASE . 'public/blocks/form-actions.php';
	}
}

if ( ! function_exists( 'wpcb_template_form_summary_title' ) ) {
	/**
	 * Product configurator form summary title
	 *
	 * @return void
	 */
	function wpcb_template_form_summary_title() {
		?>
		<div class="wpcb-configurator-summary-title">
			<strong>
				<?php
				$suffix = wpcb_get_i18n_suffix();
				echo esc_attr( wpcb_get_option( 'wpcb_summary_panel_label', $suffix ) ) ?: esc_attr__( 'Your configuration', 'wcb-configurator-builder' );
				?>
			</strong>
		</div>
		<?php
	}
}

if ( ! function_exists( 'wpcb_configurator_popup_button' ) ) {
	/**
	 * Product configurator popup button.
	 *
	 * @return void
	 */
	function wpcb_configurator_popup_button( $shortcode = false ) {
		global $wpcb_configuration_id;

		if ( is_singular('product') && ! $shortcode ) {
			return;
		}

		if ( 'popup' === get_post_meta( $wpcb_configuration_id, '_template_display', true ) ) {
			$suffix = wpcb_get_i18n_suffix();
			$button_text = wpcb_get_option( 'wpcb_button_open_popup_text' . $suffix );
			if ( ! $button_text ) {
				$button_text = __( 'Configure', 'wcb-configurator-builder' );
			}

			echo '<a href="#0" role="button" class="button wpcb-button wpcb_show_configuration_popup" data-wpcb-id="' . esc_attr( $wpcb_configuration_id ) . '">' . esc_attr( $button_text ) . '</a>';
		}
	}
}

if ( ! function_exists( 'wpcb_configurator_form_reset_button' ) ) {
	/**
	 * Product configurator reset button.
	 *
	 * @return void
	 */
	function wpcb_configurator_form_reset_button() {
		$suffix = wpcb_get_i18n_suffix();
		$reset_label = wpcb_get_option( 'wpcb_button_reset_text', $suffix ) ?: __( 'Reset configuration', 'wcb-configurator-builder' );
		?>
		<a href="#" class="wpcb-configurator-form-reset-action">
			<?php echo wp_kses( wpcb_get_icon( 'refresh' ), wpcb_get_icon_kses_args() ); ?>
			<span class="wpcb-configurator-form-action-label"><?php echo esc_attr( $reset_label ); ?></span>
		</a>
		<?php
	}
}
