<?php
/**
 * Handled all hooks related to WooCommerce for Revio
 *
 * @package RVIOR
 */

namespace RVIOR;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit( 1 );


if ( ! class_exists( __NAMESPACE__ . '\WooCommerce' ) ) {

	/**
	 * Handled all hooks related to WooCommerce for Revio
	 *
	 * @package RVIOR
	 */
	class WooCommerce extends \RVIOR\Base\Controller {

		// Use Utilities trait.
		use \RVIOR\Traits\Utilities;

		/**
		 * Register the hooks
		 *
		 * @return void
		 */
		public function register_hooks() {
			$this->init_extension();
			$this->add_actions();
			$this->add_filters();
		}

		/**
		 * Initialize the extension
		 *
		 * @return mixed
		 */
		public function init_extension() {
			require_once __DIR__ . '/class-woocommerce-extension.php';

			// Initialize the extension.
			\RVIOR\Extensions\WooCommerce::init();
		}

		/**
		 * Add action hooks
		 *
		 * @return mixed
		 */
		public function add_actions() {
			// Check if the user has premium access.
			// Modifying this section will cause the plugin to break.
			if ( ! $this->client()->has_premium_access() ) {
				return false;
			}

			//add_action('woocommerce_email_footer', [ $this, 'add_recorder_to_woocommerce_email_footer' ], 0);
			add_action('woocommerce_order_item_meta_start', [ $this, 'add_review_link' ], 12, 3);
		}

		/**
		 * Add filter hooks
		 *
		 * @return mixed
		 */
		public function add_filters() {
			// Check if the user has premium access.
			// Modifying this section will cause the plugin to break.
			// if ( ! $this->client()->has_premium_access() ) {
			//  return false;
			// }
			add_filter('woocommerce_product_tabs', [ $this, 'add_custom_product_tab' ], 99);
		}



		/**
		 * Adds custom tab to WooCommerce Single Product Page
		 *
		 * @param array $tabs WooCommerce Tabs.
		 * @return array
		 */
		public function add_custom_product_tab( $tabs ) {
			$woo_settings = (array) $this->option()->get('woocommerce');

			if ( ! $woo_settings ) {
				return $tabs;
			}

			// Adds a custom tab.
			if ( $woo_settings['single_tab'] ) {
				$tabs['rvior_tab'] = [
					'title'    => wp_sprintf( '%s', $woo_settings['single_tab_label'] ),
					'priority' => 50,
					'callback' => [ $this, 'custom_single_product_tab_callback' ],
				];
			}

			// Hides the default reviews tab.
			if ( true === wp_validate_boolean( $woo_settings['hide_default_reviews_tab'] ) ) {
				unset($tabs['reviews']);
			}

			return $tabs;
		}

		/**
		 * Adds product review link in oder details table
		 *
		 * @return mixed
		 */
		public function add_review_link( $item_id, $item, $order ) {
			$woo = (array) $this->option()->get('woocommerce');
			if ( ! isset( $woo['order_complete_button'] ) || '1' !== $woo['order_complete_button'] ) {
				return false;
			}
			$porduct_url = get_permalink($item->get_product_id()) . '?review=rvior';
			?>
				<?php if ( is_checkout() && ! empty( is_wc_endpoint_url('order-received') ) ) : ?>
					<span class="rvior-single-product-reveiw">
						<a href="<?php echo esc_url($porduct_url); ?>" target="_blank">Leave a Review</a>
					</span>
				<?php else : ?>
					<div class="rvior-eamil-review">
						<span class="rvior-single-product-reveiw">
							<a href="<?php echo esc_url($porduct_url); ?>" target="_blank">Leave a Review</a>
						</span>
					</div>
				<?php endif; ?>
			<?php
		}


		/**
		 * Adds custom tab to WooCommerce Single Product Page
		 *
		 * @return mixed
		 */
		public function custom_single_product_tab_callback() {
			global $product;

			$product_name = str_replace(' ','-',$product->get_name());

			// if ( get_option('rvior_onbarding_on_update', false) ) {
			//  $folder = wp_sprintf('product-%s', $product->get_id());
			// } else {
				$folder = wp_sprintf('product-%s-%s', $product_name, $product->get_id());
			//}

			$woo_settings = (array) $this->option()->get('woocommerce');

			if ( ! $woo_settings ) {
				return false;
			}

			echo ( '<div class="woocommerce">' );

			if ( $woo_settings['single_tab_showcase'] ) {
				$shortcode = wp_sprintf('[reviews cols="3" order="%s" orderBy="%s" folder="%s"][/reviews]',
					esc_attr( $woo_settings['single_order'] ),
					esc_attr( $woo_settings['single_order_by'] ),
					esc_attr( $folder )
				);

				echo wp_kses_post( do_shortcode( $shortcode ) );
			}

			if ( $woo_settings['single_button'] ) {
				echo '<div style="margin-top: 20px;">';
				$shortcode = wp_sprintf('[recorder folder=%s review_source="button"][/recorder]', esc_attr( $folder ));

				echo wp_kses_post( do_shortcode( $shortcode ) );
				echo '</div>';
			}

			echo '</div>';
		}

		/**
		 * Adds recorder to WooCommerce email footer
		 *
		 * @return mixed
		 */
		// public function add_recorder_to_woocommerce_email_footer() {
		//  $woo = (array) $this->option()->get('woocommerce');
		//  if ( ! $woo['order_complete_email'] ) {
		//      return false;
		//  }

		//  echo wp_kses_post($this->globals()->get_email_append_message());
		// }
	}

	// Initialize the class.
	WooCommerce::init();
}
