<?php
/**
 * Woostify Compare Front End
 *
 * @package  Woostify Pro
 */

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'Woostify_Compare_Frontend' ) ) {

	/**
	 * Class Woostify Compare Front End
	 */
	class Woostify_Compare_Frontend {
		/**
		 * Instance Variable
		 *
		 * @var instance
		 */
		private static $instance;

		/**
		 *  Initiator
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 * Class constructor.
		 */
		public function __construct() {
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
			add_shortcode( 'woostify_compare', array( $this, 'render_compare_page' ) );
			add_shortcode( 'woositfy_compare', array( $this, 'render_compare_page' ) );

			// Hook compare button.
			add_action( 'woostify_product_loop_item_action_item', array( $this, 'add_compare_button_to_loop' ), 40 );
			add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'add_compare_button_with_text_to_loop' ), 86 );
			add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_compare_button_to_single' ), 20 );

			// Hook compare icon in header.
			add_action( 'woostify_site_tool_before_first_item', array( $this, 'add_compare_header_icon' ) );

			// Hook dynamic customizer CSS.
			add_filter( 'woostify_customizer_css', array( $this, 'add_compare_count_colors_to_dynamic_css' ) );

			// Customizer register.
			add_action( 'customize_register', array( $this, 'register_customizer' ), 99 );
		}

		/**
		 * Enqueue scripts and styles
		 */
		public function enqueue_scripts() {
			$options = Woostify_Compare::get_instance()->get_options();
			if ( ! $options['enable'] ) {
				return;
			}

			// Tiny-slider script.
			wp_enqueue_script( 'tiny-slider' );

			wp_enqueue_script(
				'woostify-compare',
				WOOSTIFY_PRO_MODULES_URI . 'woocommerce/compare/js/script' . woostify_suffix() . '.js',
				array( 'jquery' ),
				WOOSTIFY_PRO_VERSION,
				true
			);

			wp_localize_script(
				'woostify-compare',
				'woostify_compare_data',
				array(
					'ajax_url'   => admin_url( 'admin-ajax.php' ),
					'ajax_nonce' => wp_create_nonce( 'woostify_compare' ),
				)
			);

			wp_enqueue_style(
				'woostify-compare',
				WOOSTIFY_PRO_MODULES_URI . 'woocommerce/compare/css/style.css',
				array(),
				WOOSTIFY_PRO_VERSION
			);

			wp_add_inline_style( 'woostify-compare', $this->get_compare_dynamic_css() );
		}

		/**
		 * Add compare button to loop
		 */
		public function add_compare_button_to_loop() {
			$options = Woostify_Compare::get_instance()->get_options();
			if ( ! $options['enable'] ) {
				return;
			}

			$pro_options = Woostify_Pro::get_instance()->woostify_pro_options();
			$defaults    = Woostify_Pro::get_instance()->default_options_value();
			$position    = isset( $pro_options['shop_page_compare_position'] ) ? $pro_options['shop_page_compare_position'] : $defaults['shop_page_compare_position'];

			if ( 'top-right' !== $position ) {
				return;
			}

			global $product;
			if ( ! $product ) {
				return;
			}

			$product_id      = $product->get_id();
			$compare_cookie  = isset( $_COOKIE['woostify_compare_list'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['woostify_compare_list'] ) ) : '';
			$compare_list    = ! empty( $compare_cookie ) ? explode( ',', $compare_cookie ) : array();
			$active_class    = in_array( (string) $product_id, $compare_list, true ) ? 'active' : '';
			$compare_page_id = $options['select_page'];
			$compare_url     = ! empty( $compare_page_id ) ? get_permalink( $compare_page_id ) : '#';

			?>
			<a href="<?php echo esc_url( $compare_url ); ?>" title="<?php esc_attr_e( 'Compare', 'woostify-pro' ); ?>" data-pid="<?php echo esc_attr( $product_id ); ?>" class="product-compare-btn compare-with-icon <?php echo esc_attr( $active_class ); ?>">
				<span class="compare-icon compare-icon-loop"><?php echo woostify_fetch_svg_icon( 'loop' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
				<span class="compare-icon compare-icon-check"><?php echo woostify_fetch_svg_icon( 'check' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
			</a>
			<?php
		}

		/**
		 * Add compare button to single product page
		 */
		public function add_compare_button_to_single() {
			$options = Woostify_Compare::get_instance()->get_options();
			if ( ! $options['enable'] ) {
				return;
			}

			global $product;
			if ( ! $product ) {
				return;
			}

			$product_id      = $product->get_id();
			$compare_cookie  = isset( $_COOKIE['woostify_compare_list'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['woostify_compare_list'] ) ) : '';
			$compare_list    = ! empty( $compare_cookie ) ? explode( ',', $compare_cookie ) : array();
			$active_class    = in_array( (string) $product_id, $compare_list, true ) ? 'active' : '';
			$compare_page_id = $options['select_page'];
			$compare_url     = ! empty( $compare_page_id ) ? get_permalink( $compare_page_id ) : '#';

			?>
			<a href="<?php echo esc_url( $compare_url ); ?>" data-pid="<?php echo esc_attr( $product_id ); ?>" class="woostify-compare-button-single <?php echo esc_attr( $active_class ); ?>">
				<span class="compare-icon compare-icon-loop"><?php echo woostify_fetch_svg_icon( 'loop' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
				<span class="compare-icon compare-icon-check"><?php echo woostify_fetch_svg_icon( 'check' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
				<span class="woostify-compare-button-text">
					<?php esc_html_e( 'Compare', 'woostify-pro' ); ?>
				</span>
			</a>
			<?php
		}

		/**
		 * Render compare page shortcode
		 */
		public function render_compare_page() {
			$options = Woostify_Compare::get_instance()->get_options();
			if ( ! $options['enable'] ) {
				return '';
			}

			ob_start();
			?>
			<div class="woostify-compare-page">
				<?php
				$compare_cookie = isset( $_COOKIE['woostify_compare_list'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['woostify_compare_list'] ) ) : '';
				$product_ids    = ! empty( $compare_cookie ) ? array_map( 'absint', explode( ',', $compare_cookie ) ) : array();

				// Filter out invalid products.
				$valid_product_ids = array();
				if ( ! empty( $product_ids ) ) {
					foreach ( $product_ids as $id ) {
						if ( 'product' === get_post_type( $id ) ) {
							$valid_product_ids[] = $id;
						}
					}
				}

				if ( empty( $valid_product_ids ) ) {
					$this->render_empty_compare_page( $options );
				} else {
					$fields = ! empty( $options['fields'] ) ? explode( ',', $options['fields'] ) : array();
					// Get all product objects.
					$products = array();
					foreach ( $valid_product_ids as $id ) {
						$prod = wc_get_product( $id );
						if ( $prod ) {
							$products[] = $prod;
						}
					}

					if ( empty( $products ) ) {
						$this->render_empty_compare_page( $options );
					} else {
						?>
						<div class="woostify-compare-table-wrapper">
							<table class="woostify-compare-table">
								<thead>
									<tr>
										<th class="compare-row-label-col">&nbsp;</th>
										<?php
										foreach ( $products as $product_item ) :
											$product_id = $product_item->get_id();
											$GLOBALS['product'] = $product_item;
											?>
											<th class="compare-product-header" data-product-id="<?php echo esc_attr( $product_id ); ?>">
												<div class="compare-remove-wrapper">
													<a href="#" class="woostify-remove-compare" data-product-id="<?php echo esc_attr( $product_id ); ?>">
														<span class="remove-icon">&times;</span>
														<span class="remove-text"><?php esc_html_e( 'Remove', 'woostify-pro' ); ?></span>
													</a>
												</div>
												<div class="compare-product-image">
													<a href="<?php echo esc_url( $product_item->get_permalink() ); ?>">
														<?php echo $product_item->get_image( 'woocommerce_thumbnail' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
													</a>
												</div>
												<a class="compare-product-title" href="<?php echo esc_url( $product_item->get_permalink() ); ?>">
													<?php echo esc_html( $product_item->get_name() ); ?>
												</a>
												<div class="compare-product-price">
													<?php echo $product_item->get_price_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
												</div>
												<div class="compare-product-rating">
													<?php echo wc_get_rating_html( $product_item->get_average_rating() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
												</div>
												<div class="compare-product-add-to-cart">
													<?php woocommerce_template_loop_add_to_cart(); ?>
												</div>
											</th>
										<?php endforeach; ?>
									</tr>
								</thead>
								<tbody>
									<?php
									$body_fields = array_diff( $fields, array( 'price', 'rating', 'add_to_cart' ) );
									foreach ( $body_fields as $field ) :
										$label = '';
										if ( 0 === strpos( $field, 'pa_' ) ) {
											$label = wc_attribute_label( $field );
										} else {
											switch ( $field ) {
												case 'description':
													$label = __( 'Description', 'woostify-pro' );
													break;
												case 'sku':
													$label = __( 'SKU', 'woostify-pro' );
													break;
												case 'price':
													$label = __( 'Price', 'woostify-pro' );
													break;
												case 'availability':
													$label = __( 'Availability', 'woostify-pro' );
													break;
												case 'rating':
													$label = __( 'Rating', 'woostify-pro' );
													break;
												case 'weight':
													$label = __( 'Weight', 'woostify-pro' );
													break;
												case 'dimensions':
													$label = __( 'Dimensions', 'woostify-pro' );
													break;
												case 'add_to_cart':
													$label = __( 'Add to cart', 'woostify-pro' );
													break;
											}
										}
										?>
										<tr class="compare-row-<?php echo esc_attr( $field ); ?>">
											<td class="compare-row-label-col"><?php echo esc_html( $label ); ?></td>
											<?php
											foreach ( $products as $product_item ) :
												$product_id = $product_item->get_id();
												$GLOBALS['product'] = $product_item;
												?>
												<td class="compare-val-col" data-product-id="<?php echo esc_attr( $product_id ); ?>">
													<?php
													if ( 0 === strpos( $field, 'pa_' ) ) {
														$attr_val = $product_item->get_attribute( $field );
														echo esc_html( ! empty( $attr_val ) ? $attr_val : '-' );
													} else {
														switch ( $field ) {
															case 'description':
																$desc = $product_item->get_short_description();
																if ( empty( $desc ) ) {
																	$desc = $product_item->get_description();
																}
																echo wp_kses_post( wp_trim_words( $desc, 30 ) );
																break;
															case 'sku':
																$sku = $product_item->get_sku();
																echo esc_html( $sku ? $sku : '-' );
																break;
															case 'price':
																echo $product_item->get_price_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
																break;
															case 'availability':
																if ( $product_item->is_in_stock() ) {
																	echo '<span class="stock in-stock">' . esc_html__( 'In stock', 'woocommerce' ) . '</span>';
																} else {
																	echo '<span class="stock out-of-stock">' . esc_html__( 'Out of stock', 'woocommerce' ) . '</span>';
																}
																break;
															case 'rating':
																echo wc_get_rating_html( $product_item->get_average_rating() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
																break;
															case 'weight':
																$weight = $product_item->get_weight();
																echo esc_html( $weight ? $weight . ' ' . get_option( 'woocommerce_weight_unit' ) : '-' );
																break;
															case 'dimensions':
																$dimensions = wc_format_dimensions( $product_item->get_dimensions( false ) );
																echo esc_html( ! empty( $dimensions ) && '-' !== $dimensions ? $dimensions : '-' );
																break;
															case 'add_to_cart':
																woocommerce_template_loop_add_to_cart();
																break;
														}
													}
													?>
												</td>
											<?php endforeach; ?>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>
						</div>
						<?php
					}
				}
				?>
			</div>
			<?php
			return ob_get_clean();
		}

		/**
		 * Add compare header icon before search icon
		 */
		public function add_compare_header_icon() {
			$options = Woostify_Compare::get_instance()->get_options();
			if ( ! $options['enable'] ) {
				return;
			}

			$compare_page_id = $options['select_page'];
			$compare_url     = ! empty( $compare_page_id ) ? get_permalink( $compare_page_id ) : '#';
			$compare_cookie  = isset( $_COOKIE['woostify_compare_list'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['woostify_compare_list'] ) ) : '';
			$compare_list    = ! empty( $compare_cookie ) ? explode( ',', $compare_cookie ) : array();
			$compare_count   = count( $compare_list );

			$theme_options   = function_exists( 'woostify_options' ) ? woostify_options( false ) : array();

			?>
			<a href="<?php echo esc_url( $compare_url ); ?>" class="tools-icon header-compare-icon" title="<?php esc_attr_e( 'Compare', 'woostify-pro' ); ?>">
				<?php echo woostify_fetch_svg_icon( 'loop' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				<span class="theme-item-count compare-item-count"><?php echo esc_html( $compare_count ); ?></span>
			</a>
			<?php
		}

		/**
		 * Get compare dynamic CSS
		 *
		 * @return string
		 */
		public function get_compare_dynamic_css() {
			$options = Woostify_Compare::get_instance()->get_options();
			if ( ! $options['enable'] ) {
				return '';
			}

			$theme_options = function_exists( 'woostify_options' ) ? woostify_options( false ) : array();
			$badge_bg      = ! empty( $theme_options['header_count_background_color'] ) ? $theme_options['header_count_background_color'] : '#1346af';
			$badge_color   = ! empty( $theme_options['header_count_color'] ) ? $theme_options['header_count_color'] : '#ffffff';
			$transparent_bg = ! empty( $theme_options['header_transparent_count_background'] ) ? $theme_options['header_transparent_count_background'] : '';

			// Button styles from Customizer
			$button_bg          = ! empty( $theme_options['button_background_color'] ) ? $theme_options['button_background_color'] : '#84b93b';
			$button_color       = ! empty( $theme_options['button_text_color'] ) ? $theme_options['button_text_color'] : '#ffffff';
			$button_hover_bg    = ! empty( $theme_options['button_hover_background_color'] ) ? $theme_options['button_hover_background_color'] : '#72a230';
			$button_hover_color = ! empty( $theme_options['button_hover_text_color'] ) ? $theme_options['button_hover_text_color'] : '#ffffff';
			$button_radius      = isset( $theme_options['buttons_border_radius'] ) ? $theme_options['buttons_border_radius'] : '0';

			$pro_options        = Woostify_Pro::get_instance()->woostify_pro_options();
			$defaults           = Woostify_Pro::get_instance()->default_options_value();
			$compare_position   = isset( $pro_options['shop_page_compare_position'] ) ? $pro_options['shop_page_compare_position'] : $defaults['shop_page_compare_position'];
			$compare_radius     = isset( $pro_options['shop_product_compare_radius'] ) ? $pro_options['shop_product_compare_radius'] : $defaults['shop_product_compare_radius'];
			$compare_bg         = isset( $pro_options['shop_product_compare_background'] ) ? $pro_options['shop_product_compare_background'] : $defaults['shop_product_compare_background'];
			$compare_color      = isset( $pro_options['shop_product_compare_color'] ) ? $pro_options['shop_product_compare_color'] : $defaults['shop_product_compare_color'];
			$compare_bg_hover   = isset( $pro_options['shop_product_compare_bg_hover'] ) ? $pro_options['shop_product_compare_bg_hover'] : $defaults['shop_product_compare_bg_hover'];
			$compare_c_hover    = isset( $pro_options['shop_product_compare_c_hover'] ) ? $pro_options['shop_product_compare_c_hover'] : $defaults['shop_product_compare_c_hover'];

			// Shop archive settings from Customizer
			$title_color        = ! empty( $theme_options['shop_page_product_title_color'] ) ? $theme_options['shop_page_product_title_color'] : '#222222';
			$title_font_size    = isset( $theme_options['shop_page_product_title_font_size'] ) ? $theme_options['shop_page_product_title_font_size'] : '15';
			$title_tablet_size  = isset( $theme_options['shop_page_product_title_tablet_font_size'] ) ? $theme_options['shop_page_product_title_tablet_font_size'] : '';
			$title_mobile_size  = isset( $theme_options['shop_page_product_title_mobile_font_size'] ) ? $theme_options['shop_page_product_title_mobile_font_size'] : '';

			$price_color        = ! empty( $theme_options['shop_page_product_price_color'] ) ? $theme_options['shop_page_product_price_color'] : '#7eb338';
			$price_font_size    = isset( $theme_options['shop_page_product_price_font_size'] ) ? $theme_options['shop_page_product_price_font_size'] : '15';
			$price_tablet_size  = isset( $theme_options['shop_page_product_price_tablet_font_size'] ) ? $theme_options['shop_page_product_price_tablet_font_size'] : '';
			$price_mobile_size  = isset( $theme_options['shop_page_product_price_mobile_font_size'] ) ? $theme_options['shop_page_product_price_mobile_font_size'] : '';

			$styles = '
				.site-tools .compare-item-count {
					background-color: ' . esc_attr( $badge_bg ) . ';
					color: ' . esc_attr( $badge_color ) . ';
				}
				.woostify-compare-return-shop-btn,
				.compare-product-add-to-cart .button,
				.compare-product-add-to-cart .added_to_cart {
					background-color: ' . esc_attr( $button_bg ) . ';
					color: ' . esc_attr( $button_color ) . ';
					border-radius: ' . esc_attr( $button_radius ) . 'px;
				}
				.woostify-compare-return-shop-btn:hover,
				.compare-product-add-to-cart .button:hover,
				.compare-product-add-to-cart .added_to_cart:hover {
					background-color: ' . esc_attr( $button_hover_bg ) . ';
					color: ' . esc_attr( $button_hover_color ) . ';
				}
				.compare-product-title {
					color: ' . esc_attr( $title_color ) . ';
					font-size: ' . esc_attr( $title_font_size ) . 'px;
				}
				.compare-product-price,
				.compare-product-price .amount,
				.compare-product-price ins .amount {
					color: ' . esc_attr( $price_color ) . ';
					font-size: ' . esc_attr( $price_font_size ) . 'px;
				}
			';

			if ( 'none' !== $compare_position ) {
				$styles .= '
				.compare-with-text {
					border-radius: ' . esc_attr( $button_radius ) . 'px;
					color: ' . esc_attr( $button_color ) . ';
					background-color: ' . esc_attr( $button_bg ) . ';
				}

				.compare-with-text:hover {
					color: ' . esc_attr( $button_hover_color ) . ';
					background-color: ' . esc_attr( $button_hover_bg ) . ';
				}
				';

				if ( '' !== $compare_radius ) {
					$styles .= '
					.compare-with-text.product-compare-btn,
					.product-loop-action .product-compare-btn {
						border-radius: ' . esc_attr( $compare_radius ) . 'px;
					}
					';
				}

				if ( '' !== $compare_bg ) {
					$styles .= '
					.compare-with-text.product-compare-btn,
					.product-loop-action .product-compare-btn {
						background-color: ' . esc_attr( $compare_bg ) . ';
					}
					';
				}

				if ( '' !== $compare_color ) {
					$styles .= '
					.compare-with-text.product-compare-btn,
					.product-loop-action .product-compare-btn {
						color: ' . esc_attr( $compare_color ) . ';
					}
					';
				}

				if ( '' !== $compare_bg_hover ) {
					$styles .= '
					.compare-with-text.product-compare-btn:hover,
					.product-loop-action .product-compare-btn:hover {
						background-color: ' . esc_attr( $compare_bg_hover ) . ';
					}
					';
				}

				if ( '' !== $compare_c_hover ) {
					$styles .= '
					.compare-with-text.product-compare-btn:hover,
					.product-loop-action .product-compare-btn:hover {
						color: ' . esc_attr( $compare_c_hover ) . ';
					}
					';
				}
			}

			if ( $title_tablet_size || $price_tablet_size ) {
				$styles .= '
					@media (min-width: 321px) and (max-width: 768px) {
				';
				if ( $title_tablet_size ) {
					$styles .= '
						.compare-product-title {
							font-size: ' . esc_attr( $title_tablet_size ) . 'px;
						}
					';
				}
				if ( $price_tablet_size ) {
					$styles .= '
						.compare-product-price,
						.compare-product-price .amount,
						.compare-product-price ins .amount {
							font-size: ' . esc_attr( $price_tablet_size ) . 'px;
						}
					';
				}
				$styles .= '
					}
				';
			}

			if ( $title_mobile_size || $price_mobile_size ) {
				$styles .= '
					@media (max-width: 320px) {
				';
				if ( $title_mobile_size ) {
					$styles .= '
						.compare-product-title {
							font-size: ' . esc_attr( $title_mobile_size ) . 'px;
						}
					';
				}
				if ( $price_mobile_size ) {
					$styles .= '
						.compare-product-price,
						.compare-product-price .amount,
						.compare-product-price ins .amount {
							font-size: ' . esc_attr( $price_mobile_size ) . 'px;
						}
					';
				}
				$styles .= '
					}
				';
			}

			if ( $transparent_bg ) {
				$styles .= '
					.has-header-transparent .compare-item-count {
						background-color: ' . esc_attr( $transparent_bg ) . ';
					}
				';
			}

			return $styles;
		}

		/**
		 * Add compare count colors to dynamic CSS
		 *
		 * @param string $styles Dynamic CSS.
		 * @return string
		 */
		public function add_compare_count_colors_to_dynamic_css( $styles ) {
			$styles .= $this->get_compare_dynamic_css();
			return $styles;
		}

		/**
		 * Render empty compare page layout
		 *
		 * @param array $options Module options.
		 */
		private function render_empty_compare_page( $options ) {
			$shop_url = '#';
			if ( woostify_is_woocommerce_activated() ) {
				$shop_url = get_permalink( wc_get_page_id( 'shop' ) );
			}
			if ( ! $shop_url ) {
				$shop_url = home_url( '/' );
			}
			?>
			<div class="woostify-compare-empty-container">
				<h2 class="woostify-compare-empty-title"><?php esc_html_e( 'Compare list is empty.', 'woostify-pro' ); ?></h2>
				<p class="woostify-compare-empty-message"><?php echo esc_html( $options['empty_text'] ); ?></p>
				<a href="<?php echo esc_url( $shop_url ); ?>" class="woostify-compare-return-shop-btn"><?php esc_html_e( 'RETURN TO SHOP', 'woostify-pro' ); ?></a>
			</div>
			<?php
		}

		/**
		 * Add compare button with text to loop
		 */
		public function add_compare_button_with_text_to_loop() {
			$options = Woostify_Compare::get_instance()->get_options();
			if ( ! $options['enable'] ) {
				return;
			}

			$pro_options = Woostify_Pro::get_instance()->woostify_pro_options();
			$defaults    = Woostify_Pro::get_instance()->default_options_value();
			$position    = isset( $pro_options['shop_page_compare_position'] ) ? $pro_options['shop_page_compare_position'] : $defaults['shop_page_compare_position'];

			if ( in_array( $position, array( 'none', 'top-right' ), true ) ) {
				return;
			}

			global $product;
			if ( ! $product ) {
				return;
			}

			$product_id      = $product->get_id();
			$compare_cookie  = isset( $_COOKIE['woostify_compare_list'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['woostify_compare_list'] ) ) : '';
			$compare_list    = ! empty( $compare_cookie ) ? explode( ',', $compare_cookie ) : array();
			$active_class    = in_array( (string) $product_id, $compare_list, true ) ? 'active' : '';
			$compare_page_id = $options['select_page'];
			$compare_url     = ! empty( $compare_page_id ) ? get_permalink( $compare_page_id ) : '#';
			
			$show_icon       = isset( $pro_options['shop_product_compare_icon'] ) ? $pro_options['shop_product_compare_icon'] : $defaults['shop_product_compare_icon'];
			
			$class[] = 'compare-with-text compare-on-' . $position;
			if ( $active_class ) {
				$class[] = $active_class;
			}
			$class   = implode( ' ', $class );
			?>
			<a href="<?php echo esc_url( $compare_url ); ?>" data-pid="<?php echo esc_attr( $product_id ); ?>" class="product-compare-btn <?php echo esc_attr( $class ); ?>" title="<?php esc_attr_e( 'Compare', 'woostify-pro' ); ?>">
				<?php if ( $show_icon ) : ?>
					<span class="compare-icon compare-icon-loop"><?php echo woostify_fetch_svg_icon( 'loop' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
					<span class="compare-icon compare-icon-check"><?php echo woostify_fetch_svg_icon( 'check' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
				<?php endif; ?>
				<?php esc_html_e( 'Compare', 'woostify-pro' ); ?>
			</a>
			<?php
		}

		/**
		 * Register customizer
		 *
		 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
		 */
		public function register_customizer( $wp_customize ) {
			$defaults = Woostify_Pro::get_instance()->default_options_value();

			// Compare section control group.
			$wp_customize->add_setting(
				'shop_page_compare_section',
				array(
					'sanitize_callback' => 'sanitize_text_field',
				)
			);
			$wp_customize->add_control(
				new Woostify_Section_Control(
					$wp_customize,
					'shop_page_compare_section',
					array(
						'label'      => __( 'Compare Button', 'woostify-pro' ),
						'section'    => 'woostify_shop_page',
						'dependency' => array(
							'woostify_pro_options[shop_page_compare_position]',
							'woostify_pro_options[shop_product_compare_icon]',
							'woostify_pro_options[shop_product_compare_background]',
							'woostify_pro_options[shop_product_compare_bg_hover]',
							'woostify_pro_options[shop_product_compare_color]',
							'woostify_pro_options[shop_product_compare_c_hover]',
							'woostify_pro_options[shop_product_compare_radius]',
						),
					)
				)
			);

			// Position.
			$wp_customize->add_setting(
				'woostify_pro_options[shop_page_compare_position]',
				array(
					'default'           => $defaults['shop_page_compare_position'],
					'sanitize_callback' => 'woostify_sanitize_choices',
					'type'              => 'option',
				)
			);
			$wp_customize->add_control(
				new Woostify_Radio_Image_Control(
					$wp_customize,
					'woostify_pro_options[shop_page_compare_position]',
					array(
						'label'    => __( 'Position', 'woostify-pro' ),
						'section'  => 'woostify_shop_page',
						'settings' => 'woostify_pro_options[shop_page_compare_position]',
						'choices'  => apply_filters(
							'woostify_pro_options_shop_page_compare_position_choices',
							array(
								'none'         => WOOSTIFY_PRO_MODULES_URI . 'woocommerce/compare/images/compare-1.jpg',
								'top-right'    => WOOSTIFY_PRO_MODULES_URI . 'woocommerce/compare/images/compare-2.png',
								'center-image' => WOOSTIFY_PRO_MODULES_URI . 'woocommerce/compare/images/compare-3.png',
								'bottom-image' => WOOSTIFY_PRO_MODULES_URI . 'woocommerce/compare/images/compare-4.png',
							)
						),
					)
				)
			);

			// Icon.
			$wp_customize->add_setting(
				'woostify_pro_options[shop_product_compare_icon]',
				array(
					'type'              => 'option',
					'default'           => $defaults['shop_product_compare_icon'],
					'sanitize_callback' => 'woostify_sanitize_checkbox',
				)
			);
			$wp_customize->add_control(
				new Woostify_Switch_Control(
					$wp_customize,
					'woostify_pro_options[shop_product_compare_icon]',
					array(
						'label'    => __( 'Compare Icon', 'woostify-pro' ),
						'section'  => 'woostify_shop_page',
						'settings' => 'woostify_pro_options[shop_product_compare_icon]',
					)
				)
			);

			// Background.
			$wp_customize->add_setting(
				'woostify_pro_options[shop_product_compare_background]',
				array(
					'default'           => $defaults['shop_product_compare_background'],
					'type'              => 'option',
					'sanitize_callback' => 'woostify_sanitize_rgba_color',
					'transport'         => 'postMessage',
				)
			);
			$wp_customize->add_setting(
				'woostify_pro_options[shop_product_compare_bg_hover]',
				array(
					'default'           => $defaults['shop_product_compare_bg_hover'],
					'type'              => 'option',
					'sanitize_callback' => 'woostify_sanitize_rgba_color',
					'transport'         => 'postMessage',
				)
			);

			if ( class_exists( 'Woostify_Color_Group_Control' ) ) {
				$wp_customize->add_control(
					new Woostify_Color_Group_Control(
						$wp_customize,
						'woostify_pro_options[shop_product_compare_background]',
						array(
							'label'    => __( 'Background', 'woostify-pro' ),
							'settings' => array(
								'woostify_pro_options[shop_product_compare_background]',
								'woostify_pro_options[shop_product_compare_bg_hover]',
							),
							'section'  => 'woostify_shop_page',
							'tooltips' => array(
								'Normal',
								'Hover',
							),
							'prefix'   => 'woostify_pro_options',
						)
					)
				);
			} else {
				$wp_customize->add_control(
					new Woostify_Color_Control(
						$wp_customize,
						'woostify_pro_options[shop_product_compare_background]',
						array(
							'label'    => __( 'Background', 'woostify-pro' ),
							'section'  => 'woostify_shop_page',
							'settings' => 'woostify_pro_options[shop_product_compare_background]',
						)
					)
				);
				$wp_customize->add_control(
					new Woostify_Color_Control(
						$wp_customize,
						'woostify_pro_options[shop_product_compare_bg_hover]',
						array(
							'label'    => __( 'Hover Background', 'woostify-pro' ),
							'section'  => 'woostify_shop_page',
							'settings' => 'woostify_pro_options[shop_product_compare_bg_hover]',
						)
					)
				);
			}

			// Color.
			$wp_customize->add_setting(
				'woostify_pro_options[shop_product_compare_color]',
				array(
					'default'           => $defaults['shop_product_compare_color'],
					'type'              => 'option',
					'sanitize_callback' => 'woostify_sanitize_rgba_color',
					'transport'         => 'postMessage',
				)
			);
			$wp_customize->add_setting(
				'woostify_pro_options[shop_product_compare_c_hover]',
				array(
					'default'           => $defaults['shop_product_compare_c_hover'],
					'type'              => 'option',
					'sanitize_callback' => 'woostify_sanitize_rgba_color',
					'transport'         => 'postMessage',
				)
			);

			if ( class_exists( 'Woostify_Color_Group_Control' ) ) {
				$wp_customize->add_control(
					new Woostify_Color_Group_Control(
						$wp_customize,
						'woostify_pro_options[shop_product_compare_color]',
						array(
							'label'    => __( 'Color', 'woostify-pro' ),
							'settings' => array(
								'woostify_pro_options[shop_product_compare_color]',
								'woostify_pro_options[shop_product_compare_c_hover]',
							),
							'section'  => 'woostify_shop_page',
							'tooltips' => array(
								'Normal',
								'Hover',
							),
							'prefix'   => 'woostify_pro_options',
						)
					)
				);
			} else {
				$wp_customize->add_control(
					new Woostify_Color_Control(
						$wp_customize,
						'woostify_pro_options[shop_product_compare_color]',
						array(
							'label'    => __( 'Color', 'woostify-pro' ),
							'section'  => 'woostify_shop_page',
							'settings' => 'woostify_pro_options[shop_product_compare_color]',
						)
					)
				);
				$wp_customize->add_control(
					new Woostify_Color_Control(
						$wp_customize,
						'woostify_pro_options[shop_product_compare_c_hover]',
						array(
							'label'    => __( 'Hover Color', 'woostify-pro' ),
							'section'  => 'woostify_shop_page',
							'settings' => 'woostify_pro_options[shop_product_compare_c_hover]',
						)
					)
				);
			}

			// Border Radius.
			$wp_customize->add_setting(
				'woostify_pro_options[shop_product_compare_radius]',
				array(
					'default'           => $defaults['shop_product_compare_radius'],
					'type'              => 'option',
					'sanitize_callback' => 'esc_html',
					'transport'         => 'postMessage',
				)
			);
			$wp_customize->add_control(
				new Woostify_Range_Slider_Control(
					$wp_customize,
					'woostify_pro_options[shop_product_compare_radius]',
					array(
						'label'    => __( 'Border Radius', 'woostify-pro' ),
						'section'  => 'woostify_shop_page',
						'settings' => array(
							'desktop' => 'woostify_pro_options[shop_product_compare_radius]',
						),
						'choices'  => array(
							'desktop' => array(
								'min'  => apply_filters( 'woostify_shop_product_compare_radius_min_step', 0 ),
								'max'  => apply_filters( 'woostify_shop_product_compare_radius_max_step', 50 ),
								'step' => 1,
								'edit' => true,
								'unit' => 'px',
							),
						),
					)
				)
			);
		}
	}

	Woostify_Compare_Frontend::get_instance();
}
