<?php
/**
 * Woostify Product Label Front End
 *
 * @package  Woostify Pro
 */

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'Woostify_Product_Label_Frontend' ) ) {

	/**
	 * Class Woostify Product Label Front End
	 */
	class Woostify_Product_Label_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_filter( 'woostify_customizer_css', array( $this, 'inline_styles' ), 48 );

			// Hooks for displaying tags on product loop and single product
			add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'custom_product_labels' ), 10 );
			add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'render_sale_flash' ), 23 );
			add_action( 'wp', array( $this, 'remove_theme_sale_flash' ), 100 );
			add_action( 'wp', array( $this, 'single_product_hooks' ) );
			add_filter( 'woocommerce_sale_flash', array( $this, 'update_sale_flash' ), 10, 3 );
			add_filter( 'woostify_price_flash', '__return_false' );
		}

		/**
		 * Remove theme sale flash
		 */
		public function remove_theme_sale_flash() {
			remove_action( 'woocommerce_before_shop_loop_item_title', 'woostify_change_sale_flash', 23 );
			remove_action( 'woostify_product_images_box_end', 'woostify_change_sale_flash', 10 );
			remove_action( 'woocommerce_before_single_product_summary', 'woostify_change_sale_flash', 25 );
		}

		/**
		 * Register single product hooks
		 */
		public function single_product_hooks() {
			if ( ! is_product() ) {
				return;
			}
			$gallery = 'theme';
			if ( function_exists( 'woostify_options' ) ) {
				$options = woostify_options( false );
				$gallery = isset( $options['shop_single_product_gallery_layout_select'] ) ? $options['shop_single_product_gallery_layout_select'] : 'theme';
			}
			if ( 'theme' === $gallery ) {
				add_action( 'woostify_product_images_box_end', array( $this, 'custom_product_labels' ), 15 );
				add_action( 'woostify_product_images_box_end', array( $this, 'render_sale_flash' ), 10 );
			} else {
				add_action( 'woocommerce_before_single_product_summary', array( $this, 'custom_product_labels' ), 26 );
				add_action( 'woocommerce_before_single_product_summary', array( $this, 'render_sale_flash' ), 25 );
			}
		}

		/**
		 * Add dynamic style to theme customize styles
		 *
		 * @param string $styles Customize styles.
		 *
		 * @return string
		 */
		public function inline_styles( $styles ) {
			$options = Woostify_Product_Label::get_instance()->get_options();

			$radius    = '3px';
			$round_css = '';
			$space     = 30;

			if ( 'round' === $options['style'] ) {
				$radius    = '50%';
				$space     = 50;
				$round_css = '
					min-width: 40px;
					min-height: 40px;
					display: inline-flex;
					align-items: center;
					justify-content: center;
					padding: 5px;
					text-align: center;
					word-break: break-all;
				';
			} elseif ( 'rectangular' === $options['style'] ) {
				$radius = '0';
			} elseif ( 'pill' === $options['style'] ) {
				$radius = '9999px';
				$round_css = '
					padding: 2px 12px;
				';
			} elseif ( 'leaf' === $options['style'] ) {
				$radius = '10px 0 10px 0';
			} elseif ( 'parallelogram' === $options['style'] ) {
				$radius = '0';
				$round_css = '
					clip-path: polygon(10px 0, 100% 0, calc(100% - 10px) 100%, 0 100%);
					padding-left: 15px;
					padding-right: 15px;
				';
			} elseif ( 'teardrop' === $options['style'] ) {
				$radius = '50% 50% 50% 0';
				$round_css = '
					padding: 4px 8px;
				';
			} elseif ( 'ribbon' === $options['style'] ) {
				$radius = '0';
				$round_css = '
					clip-path: polygon(0 0, 100% 0, calc(100% - 8px) 50%, 100% 100%, 0 100%);
					padding-right: 15px;
				';
			} elseif ( 'tag-left' === $options['style'] ) {
				$radius = '0';
				$round_css = '
					clip-path: polygon(10px 0%, 100% 0, 100% 100%, 10px 100%, 0% 50%);
					padding-left: 15px;
				';
			}

			// Style for the custom attribute label.
			$styles .= '
			/* WOOSTIFY PRODUCT LABEL CUSTOM VARIATION & FEATURED */
				.woostify-product-custom-label,
				.woostify-product-featured-label,
				.woostify-product-new-label,
				.woostify-tag-on-sale {
					border-radius: ' . $radius . ';
					display: inline-block;
					opacity: 1 !important;
					visibility: visible !important;
					padding: 2px 7px;
					font-size: 12px;
					font-weight: 500;
					line-height: normal;
					position: absolute;
					top: 10px;
					left: 10px;
					z-index: 9;
					box-shadow: 0 1px 2px rgba(0,0,0,0.1);
					' . $round_css . '
				}
				.woostify-tag-on-preorder.onsale {
					border-radius: ' . $radius . ';
					display: inline-block;
					opacity: 1 !important;
					visibility: visible !important;
					padding: 2px 7px;
					font-size: 12px;
					font-weight: 500;
					line-height: normal;
					position: absolute;
					z-index: 10;
					box-shadow: 0 1px 2px rgba(0,0,0,0.1);
					' . $round_css . '
				}
				.woostify-product-custom-label {
					background-color: ' . $options['attribute_label_bg'] . ' !important;
					color: ' . $options['attribute_text_color'] . ' !important;
				}
				.woostify-product-featured-label {
					background-color: ' . $options['hot_label_bg'] . ' !important;
					color: ' . $options['hot_text_color'] . ' !important;
				}
				.woostify-tag-on-sale {
					background-color: ' . $options['label_bg'] . ' !important;
					color: ' . $options['text_color'] . ' !important;
					border-radius: ' . $radius . ' !important;
				}
				.woostify-out-of-stock-label {
					background-color: ' . $options['out_of_stock_label_bg'] . ' !important;
					color: ' . $options['out_of_stock_text_color'] . ' !important;
					border-radius: ' . $radius . ' !important;
					' . $round_css . '
				}
				.woostify-tag-new {
					background-color: ' . $options['new_label_bg'] . ' !important;
					color: ' . $options['new_text_color'] . ' !important;
					border-radius: ' . $radius . ' !important;
					' . $round_css . '
				}

				.woostify-tag-on-sale ~ .woostify-pro-label,
				.woostify-tag-on-preorder.onsale ~ .woostify-pro-label,
				.woostify-out-of-stock-label ~ .woostify-pro-label,
				.woostify-pro-label ~ .woostify-pro-label {
					top: ' . ( 10 + $space ) . 'px;
				}
				.woostify-tag-on-sale ~ .woostify-pro-label ~ .woostify-pro-label,
				.woostify-tag-on-preorder.onsale ~ .woostify-pro-label ~ .woostify-pro-label,
				.woostify-out-of-stock-label ~ .woostify-pro-label ~ .woostify-pro-label,
				.woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label {
					top: ' . ( 10 + $space * 2 ) . 'px;
				}
				.woostify-tag-on-sale ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label,
				.woostify-tag-on-preorder.onsale ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label,
				.woostify-out-of-stock-label ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label,
				.woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label {
					top: ' . ( 10 + $space * 3 ) . 'px;
				}
				.woostify-tag-on-sale ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label,
				.woostify-tag-on-preorder.onsale ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label,
				.woostify-out-of-stock-label ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label,
				.woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label ~ .woostify-pro-label {
					top: ' . ( 10 + $space * 4 ) . 'px;
				}
			';

			return $styles;
		}

		/**
		 * Outputs the custom html tag for products with variation of type "Custom"
		 */
		public function custom_product_labels() {
			global $product;

			if ( ! $product ) {
				return;
			}

			$output = '';

			// Check for New product
			$options  = Woostify_Product_Label::get_instance()->get_options();
			$new_days = (int) $options['new_day'];
			if ( '1' === $options['new_enable'] && $new_days > 0 ) {
				$created_date = $product->get_date_created();
				if ( $created_date ) {
					$created_timestamp = $created_date->getOffsetTimestamp();
					if ( ( time() - ( 60 * 60 * 24 * $new_days ) ) < $created_timestamp ) {
						$output .= '<span class="woostify-tag woostify-pro-label woostify-tag-new woostify-product-new-label">' . esc_html__( 'New', 'woostify-pro' ) . '</span>';
					}
				}
			}

			// Check for featured product
			if ( '1' === $options['hot_enable'] && $product->is_featured() ) {
				$output .= '<span class="woostify-tag woostify-pro-label woostify-product-featured-label">' . esc_html__( 'Hot', 'woostify-pro' ) . '</span>';
			}

			// Check for "custom" type variations
			if ( $product->is_type( 'variable' ) ) {
				$attributes = $product->get_attributes();
				if ( $attributes ) {
					foreach ( $attributes as $key => $attribute ) {

						if ( ! $attribute->get_variation() ) {
							continue;
						}

						$attr_id   = $attribute->get_id();
						$attr_type = wc_get_attribute( $attr_id );

						if ( $attr_type && 'custom' === $attr_type->type ) {
							$terms = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );

							if ( empty( $terms ) || is_wp_error( $terms ) ) {
								continue;
							}

							foreach ( $terms as $term ) {
								// Render label tag looking similar to sale tag
								$output .= '<span class="woostify-tag woostify-pro-label woostify-product-custom-label">' . esc_html( $term->name ) . '</span>';
							}
						}
					}
				}
			}

			if ( ! empty( $output ) ) {
				echo wp_kses_post( $output );
			}
		}

		/**
		 * Update sale flash
		 *
		 * @param string $output  Output.
		 * @param object $post    Post.
		 * @param object $product Product.
		 *
		 * @return string
		 */
		public function update_sale_flash( $output, $post, $product ) {
			$options = Woostify_Product_Label::get_instance()->get_options();
			if ( '1' === $options['sale_percentage'] ) {
				if ( $product->is_on_sale() ) {
					$percentage = 0;
					if ( $product->is_type( 'variable' ) ) {
						$percentages = array();
						$prices      = $product->get_variation_prices();
						foreach ( $prices['regular_price'] as $key => $regular_price ) {
							$sale_price = $prices['sale_price'][ $key ];
							if ( $sale_price < $regular_price ) {
								$percentages[] = round( ( ( floatval( $regular_price ) - floatval( $sale_price ) ) / floatval( $regular_price ) ) * 100 );
							}
						}
						$percentage = ! empty( $percentages ) ? max( $percentages ) : 0;
					} else {
						$regular_price = floatval( $product->get_regular_price() );
						$sale_price    = floatval( $product->get_sale_price() );
						if ( $regular_price > 0 && $sale_price > 0 ) {
							$percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
						}
					}

					if ( $percentage > 0 ) {
						return '<span class="woostify-tag woostify-tag-on-sale">-' . $percentage . '%</span>';
					}
				}
			}

			return $output;
		}

		/**
		 * Render sale flash
		 */
		public function render_sale_flash() {
			global $product;
			if ( ! $product || ! is_object( $product ) ) {
				return;
			}

			// Do not show for out of stock if theme hides it
			$out_of_stock = get_post_meta( $product->get_id(), '_stock_status', true );
			if ( 'outofstock' === $out_of_stock ) {
				return;
			}

			if ( $product->is_on_sale() ) {
				$options = Woostify_Product_Label::get_instance()->get_options();
				$output  = $this->update_sale_flash( '', null, $product );
				if ( $output ) {
					echo wp_kses_post( $output );
				} else {
					$sale_text = esc_html__( 'Sale!', 'woostify-pro' );
					if ( function_exists( 'woostify_options' ) ) {
						$options_theme = woostify_options( false );
						$sale_text     = isset( $options_theme['shop_page_sale_text'] ) ? $options_theme['shop_page_sale_text'] : $sale_text;
					}
					echo '<span class="woostify-tag woostify-tag-on-sale">' . esc_html( $sale_text ) . '</span>';
				}
			}
		}
	}

	Woostify_Product_Label_Frontend::get_instance();
}
