<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://wpconfiguratorbuilder.com
 * @since      1.0.0
 *
 * @package    wcb-configurator-builder
 * @subpackage wcb-configurator-builder/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    wcb-configurator-builder
 * @subpackage wcb-configurator-builder/includes
 * @author     WPCB <support@wpconfiguratorbuilder.com>
 */
class WPCB_Shortcodes {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'after_setup_theme', array( $this, 'register_shortcodes' ), 99 );
	}

	/**
	 * Register shortcodes for outputting configurator on custom page.
	 *
	 * @since    1.0.0
	 */
	public function register_shortcodes() {
		add_shortcode( 'wpcb_configuration', array( $this, 'output_configurator_template' ) );
		add_shortcode( 'wpcb_configurator', array( $this, 'output_configurator_template' ) );

		add_shortcode( 'wpcb_configuration_wrapper', array( $this, 'output_configurator_wrapper' ) );
		add_shortcode( 'wpcb_configuration_wrapper_close', array( $this, 'output_configurator_wrapper_close' ) );
		add_shortcode( 'wpcb_configuration_result', array( $this, 'output_configurator_result' ) );

		add_shortcode( 'wpcb_popup_button', array( $this, 'output_popup_button' ) );
		add_shortcode( 'wpcb_gallery', array( $this, 'output_gallery' ) );
		add_shortcode( 'wpcb_gallery_nav', array( $this, 'output_gallery_nav' ) );
		add_shortcode( 'wpcb_form_title', array( $this, 'output_form_title' ) );
		add_shortcode( 'wpcb_form', array( $this, 'output_form' ) );
		add_shortcode( 'wpcb_form_totals', array( $this, 'output_form_totals' ) );
		add_shortcode( 'wpcb_form_actions', array( $this, 'output_form_actions' ) );
		add_shortcode( 'wpcb_form_summary', array( $this, 'output_form_summary' ) );
		add_shortcode( 'wpcb_step_buttons', array( $this, 'output_step_buttons' ) );
		add_shortcode( 'wpcb_step_nav', array( $this, 'output_step_nav' ) );
	}

	/**
	 * Display the full configurator template
	 *
	 * @since    1.0.0
	 */
	public function output_configurator_template( $atts ) {
		global $wpcb_configuration_id;

		$wpcb_configuration_id = $this->get_shortcode_configurator_id( $atts );
		if ( ! $wpcb_configuration_id ) {
			return;
		}

		$template_layout = get_post_meta( $wpcb_configuration_id, '_template', true );
		if ( 'custom' === $template_layout ) {
			return;
		}

		$template_path = sprintf(
			'public/templates/%s',
			$template_layout
		);

		return $this->get_shortcode_html( $template_path . '.php', $atts );
	}

	/**
	 * Display the product configurator popup button html
	 *
	 * @since    1.0.0
	 */
	public function output_popup_button() {
		ob_start();
		wpcb_configurator_popup_button(true);
		$button = ob_get_clean();
		return $button;
	}
	
	/**
	 * Display the product gallery html
	 *
	 * @since    1.0.0
	 */
	public function output_gallery( $atts ) {
		global $wpcb_configuration_id;
		if ( isset( $atts['id'] ) && ! $wpcb_configuration_id ) {
			$wpcb_configuration_id = $atts['id'];
		}

		$class = ' visuals-' . get_post_meta( $wpcb_configuration_id, '_template_width', true );

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

		$html .= $this->get_shortcode_html( '/public/blocks/gallery.php', $atts );

		if ( isset( $atts['actions'] ) ) {
			$html .= $this->get_shortcode_html( '/public/blocks/gallery-actions.php', $atts );
		}

		if ( isset( $atts['nav'] ) ) {
			$html .= $this->get_shortcode_html( '/public/blocks/gallery-nav.php', $atts );
		}

		$html .= '</div>';

		return $html;

	}

	/**
	 * Display the product gallery actions html
	 *
	 * @since    1.0.0
	 */
	public function output_gallery_actions( $atts ) {
		
		return $this->get_shortcode_html( '/public/blocks/gallery-actions.php', $atts );

	}

	/**
	 * Display the product gallery nav html
	 *
	 * @since    1.0.0
	 */
	public function output_gallery_nav( $atts ) {

		return $this->get_shortcode_html( '/public/blocks/gallery-nav.php', $atts );

	}

	/**
	 * Display the product summary html
	 *
	 * @since    1.0.0
	 */
	public function output_form_title( $atts ) {

		return $this->get_shortcode_html( '/public/blocks/product-summary.php', $atts );

	}

	/**
	 * Display the configuration form options html
	 *
	 * @since    1.0.0
	 */
	public function output_form( $atts ) {

		$html = $this->get_shortcode_html( '/public/blocks/form-options.php', $atts );

		return $html;
	}

	/**
	 * Display the configuration form totals html
	 *
	 * @since    1.0.0
	 */
	public function output_form_totals( $atts ) {

		$html = $this->get_shortcode_html( '/public/blocks/form-totals.php', $atts );

		return $html;
	}

	/**
	 * Display the configuration form actions html
	 *
	 * @since    1.0.0
	 */
	public function output_form_actions( $atts ) {

		$html = $this->get_shortcode_html( '/public/blocks/form-actions.php', $atts );

		return $html;
	}

	/**
	 * Display the form summary html
	 *
	 * @since    1.0.0
	 */
	public function output_form_summary( $atts ) {

		$html = $this->get_shortcode_html( '/public/blocks/form-summary.php', $atts );

		return $html;
	}

	/**
	 * Display the form step navigation html
	 *
	 * @since    1.0.0
	 */
	public function output_step_nav( $atts ) {

		$html = $this->get_shortcode_html( '/public/blocks/step-list.php', $atts );

		return $html;
	}

	/**
	 * Display the form step buttons html
	 *
	 * @since    1.0.0
	 */
	public function output_step_buttons( $atts ) {

		$html = $this->get_shortcode_html( '/public/blocks/step-buttons.php', $atts );

		return $html;
	}

	/**
	 * Display configurator template wrapper html
	 *
	 * @since    1.0.0
	 */
	public function output_configurator_wrapper() {
		global $wpcb_configuration_id;

		do_action('wpcb_public_output_scripts');

		ob_start();

		$config_name = isset( $_GET['wpcb_result'] ) ? sanitize_text_field( $_GET['wpcb_result'] ) : '';
		if ( '' !== $config_name && function_exists( 'wpcb_configurator_form_review_page' ) ) :
			wpcb_configurator_form_review_page();
		endif;

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

		$html = ob_get_clean();
		return $html;
	}

	/**
	 * Display configurator template wrapper close html
	 *
	 * @since    1.0.0
	 */
	public function output_configurator_wrapper_close() {
		return '</div>';
	}

	/**
	 * Display the product configurator result html
	 *
	 * @since    1.0.0
	 */
	public function output_configurator_result() {
		ob_start();

		$config_name = isset( $_GET['wpcb_result'] ) ? sanitize_text_field( $_GET['wpcb_result'] ) : '';
		if ( '' !== $config_name && function_exists( 'wpcb_configurator_form_review_page' ) ) :
			wpcb_configurator_form_review_page();
		endif;

		$html = ob_get_clean();
		return $html;
	}

	/**
	 * Get shortcode html contents by path
	 *
	 * @since    1.0.0
	 */
	public function get_shortcode_html( $path, $atts = array() ) {
		global $wpcb_configuration_id;

		$wpcb_configuration_id = $this->get_shortcode_configurator_id( $atts );
		if ( ! $wpcb_configuration_id ) {
			return;
		}

		do_action( 'wpcb_public_output_scripts' );

		ob_start();

		require WPCB_BASE . $path;

		$html = ob_get_contents();
		ob_end_clean();

		return $html;
	}

	/**
	 * Get correct configurator ID.
	 *
	 * @since    1.0.0
	 */
	public function get_shortcode_configurator_id( $atts ) {
		$wpcb_configuration_id = esc_attr( get_the_ID() );

		if ( isset( $atts['id'] ) ) {
			$wpcb_configuration_id = esc_attr( $atts['id'] );
		}

		if ( 'product' === get_post_type( $wpcb_configuration_id ) ) {
			$wpcb_configuration_id = wpcb_product_has_configurator( $wpcb_configuration_id );
		}

		if ( 'wpcb_configuration' !== get_post_type( $wpcb_configuration_id ) ) {
			return false;
		}

		return $wpcb_configuration_id;
	}
}
