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

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'Woostify_Product_Label' ) ) :

	/**
	 * Woostify Product Label
	 */
	class Woostify_Product_Label {
		/**
		 * Instance Variable
		 *
		 * @var instance
		 */
		private static $instance;

		/**
		 * Extra attribute types
		 *
		 * @var array
		 */
		public $types = array();

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

		/**
		 * Constructor.
		 */
		public function __construct() {
			$this->define_constants();
			$this->types = array(
				'color' => esc_html__( 'Color', 'woostify-pro' ),
				'image' => esc_html__( 'Image', 'woostify-pro' ),
				'label' => esc_html__( 'Label', 'woostify-pro' ),
			);

			$this->includes();
			$this->init_hooks();

			// Save settings.
			$woocommerce_helper = Woostify_Woocommerce_Helper::init();
			add_action( 'wp_ajax_woostify_save_product_label_options', array( $woocommerce_helper, 'save_options' ) );

			// Add Setting url.
			add_action( 'admin_menu', array( $this, 'add_setting_url' ) );
			// Register settings.
			add_action( 'admin_init', array( $this, 'register_settings' ) );

			// Limit variations.
			add_filter( 'woocommerce_ajax_variation_threshold', array( $this, 'limit_variations' ) );

			// Disable Out of Stock Variations.
			add_filter( 'woocommerce_variation_is_active', array( $this, 'disable_variations_out_of_stock' ), 10, 2 );
		}

		/**
		 * Define constant
		 */
		public function define_constants() {
			if ( ! defined( 'WOOSTIFY_PRO_PRODUCT_LABEL' ) ) {
				define( 'WOOSTIFY_PRO_PRODUCT_LABEL', WOOSTIFY_PRO_VERSION );
			}
		}

		/**
		 * Include required core files used in admin and on the frontend.
		 */
		public function includes() {
			require_once WOOSTIFY_PRO_MODULES_PATH . 'woocommerce/product-label/inc/class-woostify-product-label-frontend.php';

			if ( is_admin() ) {
				require_once WOOSTIFY_PRO_MODULES_PATH . 'woocommerce/product-label/inc/class-woostify-product-label-admin.php';
			}
		}

		/**
		 * Initialize hooks
		 */
		public function init_hooks() {
			add_filter( 'product_attributes_type_selector', array( $this, 'add_attribute_types' ) );
			add_action( 'customize_register', array( $this, 'hide_customizer_options' ), 999 );
			add_filter( 'option_woostify_setting', array( $this, 'override_theme_settings' ) );
		}

		/**
		 * Hide customizer options when Product Label module is active
		 *
		 * @param WP_Customize_Manager $wp_customize Customize manager.
		 */
		public function hide_customizer_options( $wp_customize ) {
			$controls = array(
				'shop_page_sale_tag_section',
				'woostify_setting[shop_page_sale_tag_position]',
				'woostify_setting[shop_page_sale_percent]',
				'woostify_setting[shop_page_sale_text]',
				'woostify_setting[shop_page_sale_border_radius]',
				'woostify_setting[shop_page_sale_square]',
				'woostify_setting[shop_page_sale_size]',
				'woostify_setting[shop_page_sale_color]',
				'woostify_setting[shop_page_sale_bg_color]',
				'shop_page_out_of_stock_section',
				'woostify_setting[shop_page_out_of_stock_position]',
				'woostify_setting[shop_page_out_of_stock_text]',
				'woostify_setting[shop_page_out_of_stock_color]',
				'woostify_setting[shop_page_out_of_stock_bg_color]',
				'woostify_setting[shop_page_out_of_stock_border_radius]',
				'woostify_setting[shop_page_out_of_stock_square]',
				'woostify_setting[shop_page_out_of_stock_size]',
			);

			foreach ( $controls as $control_id ) {
				$wp_customize->remove_control( $control_id );
			}
		}

		/**
		 * Override theme settings
		 *
		 * @param array $options Theme options.
		 *
		 * @return array
		 */
		public function override_theme_settings( $options ) {
			if ( is_array( $options ) ) {
				$options['shop_page_sale_percent'] = false;
			}
			return $options;
		}

		/**
		 * Limit variation
		 *
		 * @param int $number The number.
		 */
		public function limit_variations( $number ) {
			$options = $this->get_options();
			if ( empty( $options['limit'] ) ) {
				return $number;
			}

			return intval( $options['limit'] );
		}

		/**
		 * Add extra attribute types
		 * Add color, image and label type
		 *
		 * @param array $types Types.
		 *
		 * @return array
		 */
		public function add_attribute_types( $types ) {
			$types = array_merge( $types, $this->types );

			return $types;
		}

		/**
		 * Add submenu
		 *
		 * @see  add_submenu_page()
		 */
		public function add_setting_url() {
			$position = 7;
			$sub_menu = add_submenu_page( 'woostify-welcome', 'Settings', __( 'Product Label', 'woostify-pro' ), 'manage_options', 'product-label-settings', array( $this, 'add_settings_page' ), $position );
		}

		/**
		 * Register settings
		 */
		public function register_settings() {

			register_setting( 'product-label-settings', 'woostify_product_label_text_color' );
			register_setting( 'product-label-settings', 'woostify_product_label_background' );
		}

		/**
		 * Get options
		 */
		public function get_options() {
			$options                            = array();
			$options['style']                   = get_option( 'woostify_product_label_style', 'round' );
			$options['text_color']              = get_option( 'woostify_product_label_text_color', '#ffffff' );
			$options['label_bg']                = get_option( 'woostify_product_label_background', '#1346af' );
			$options['new_day']                 = get_option( 'woostify_product_label_new_day', '7' );
			$options['new_text_color']          = get_option( 'woostify_product_label_new_text_color', '#ffffff' );
			$options['new_label_bg']            = get_option( 'woostify_product_label_new_background', '#1346af' );
			$options['hot_text_color']          = get_option( 'woostify_product_label_hot_text_color', '#ffffff' );
			$options['hot_label_bg']            = get_option( 'woostify_product_label_hot_background', '#1346af' );
			$options['out_of_stock_text_color'] = get_option( 'woostify_product_label_out_of_stock_text_color', '#ffffff' );
			$options['out_of_stock_label_bg']   = get_option( 'woostify_product_label_out_of_stock_background', '#1346af' );
			$options['attribute_text_color']    = get_option( 'woostify_product_label_attribute_text_color', '#ffffff' );
			$options['attribute_label_bg']      = get_option( 'woostify_product_label_attribute_background', '#1346af' );
			$options['new_enable']              = get_option( 'woostify_product_label_new_enable', '' );
			$options['hot_enable']              = get_option( 'woostify_product_label_hot_enable', '' );
			$options['sale_percentage']         = get_option( 'woostify_product_label_sale_percentage', '' );

			return $options;
		}

		/**
		 * Create Settings page
		 */
		public function add_settings_page() {
			$options = $this->get_options();
			$style   = $options['style'];
			?>
			<div class="woostify-options-wrap woostify-featured-setting woostify-product-label-setting" data-id="product-label" data-nonce="<?php echo esc_attr( wp_create_nonce( 'woostify-product-label-setting-nonce' ) ); ?>">

				<?php Woostify_Admin::get_instance()->woostify_save_option_messages(); ?>

				<?php Woostify_Admin::get_instance()->woostify_welcome_screen_header(); ?>

				<div class="wrap woostify-settings-box">
					<div class="woostify-welcome-container">
						<div class="woostify-notices-wrap">
							<h2 class="notices" style="display:none;"></h2>
						</div>
						<div class="woostify-settings-content">
							<h4 class="woostify-settings-section-title"><?php esc_html_e( 'Product Label', 'woostify-pro' ); ?></h4>

							<div class="woostify-settings-section-content">
								<table class="form-table">

									<tr>
										<th colspan="2" class="table-setting-heading"><?php esc_html_e( 'Label shape', 'woostify-pro' ); ?></th>
									</tr>

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Style', 'woostify-pro' ); ?>:</th>
										<td>
											<div class="woostify-product-label-shape-picker">
												<?php
												$shapes = array(
													'round'         => array(
														'label' => __( 'Round', 'woostify-pro' ),
														'svg'   => '<svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="currentColor" /><text x="50" y="55" font-size="20" font-weight="bold" fill="#fff" text-anchor="middle" alignment-baseline="middle">-20%</text></svg>',
													),
													'rectangular'   => array(
														'label' => __( 'Rectangular', 'woostify-pro' ),
														'svg'   => '<svg viewBox="0 0 100 100"><rect x="10" y="30" width="80" height="40" fill="currentColor" /><text x="50" y="55" font-size="20" font-weight="bold" fill="#fff" text-anchor="middle" alignment-baseline="middle">-20%</text></svg>',
													),
													'rounded-small' => array(
														'label' => __( 'Rounded small', 'woostify-pro' ),
														'svg'   => '<svg viewBox="0 0 100 100"><rect x="10" y="30" width="80" height="40" rx="5" fill="currentColor" /><text x="50" y="55" font-size="20" font-weight="bold" fill="#fff" text-anchor="middle" alignment-baseline="middle">-20%</text></svg>',
													),
													'pill'          => array(
														'label' => __( 'Pill', 'woostify-pro' ),
														'svg'   => '<svg viewBox="0 0 100 100"><rect x="10" y="33" width="80" height="34" rx="17" fill="currentColor" /><text x="50" y="55" font-size="20" font-weight="bold" fill="#fff" text-anchor="middle" alignment-baseline="middle">-20%</text></svg>',
													),
													'leaf'          => array(
														'label' => __( 'Leaf', 'woostify-pro' ),
														'svg'   => '<svg viewBox="0 0 100 100"><path d="M15 30 Q50 20 85 30 Q95 50 85 70 Q50 80 15 70 Q5 50 15 30" fill="currentColor" /><text x="50" y="55" font-size="20" font-weight="bold" fill="#fff" text-anchor="middle" alignment-baseline="middle">-20%</text></svg>',
													),
													'parallelogram' => array(
														'label' => __( 'Parallelogram', 'woostify-pro' ),
														'svg'   => '<svg viewBox="0 0 100 100"><path d="M20 30 H85 L80 70 H15 Z" fill="currentColor" /><text x="50" y="55" font-size="20" font-weight="bold" fill="#fff" text-anchor="middle" alignment-baseline="middle">-20%</text></svg>',
													),
													'teardrop'      => array(
														'label' => __( 'Teardrop', 'woostify-pro' ),
														'svg'   => '<svg viewBox="0 0 100 100"><path d="M50 15 C70 15 85 35 85 55 C85 70 70 85 50 85 C30 85 15 70 15 55 C15 35 30 15 50 15 Z" fill="currentColor" /><text x="50" y="58" font-size="20" font-weight="bold" fill="#fff" text-anchor="middle" alignment-baseline="middle">-20%</text></svg>',
													),
													'ribbon'        => array(
														'label' => __( 'Ribbon', 'woostify-pro' ),
														'svg'   => '<svg viewBox="0 0 100 100"><path d="M15 30 H85 L78 50 L85 70 H15 V30 Z" fill="currentColor" /><text x="45" y="55" font-size="20" font-weight="bold" fill="#fff" text-anchor="middle" alignment-baseline="middle">-20%</text></svg>',
													),
													'tag-left'      => array(
														'label' => __( 'Tag Left', 'woostify-pro' ),
														'svg'   => '<svg viewBox="0 0 100 100"><path d="M10 50 L30 30 H90 V70 H30 Z" fill="currentColor" /><text x="60" y="55" font-size="20" font-weight="bold" fill="#fff" text-anchor="middle" alignment-baseline="middle">-20%</text></svg>',
													),
												);

												foreach ( $shapes as $key => $shape ) :
													?>
													<div class="woostify-shape-option <?php echo ( $style === $key ) ? 'active' : ''; ?>" data-value="<?php echo esc_attr( $key ); ?>" title="<?php echo esc_attr( $shape['label'] ); ?>">
														<?php echo $shape['svg']; // phpcs:ignore ?>
													</div>
													<?php
												endforeach;
												?>
												<input type="hidden" name="woostify_product_label_style" value="<?php echo esc_attr( $style ); ?>">
											</div>
										</td>
									</tr>

									<tr>
										<th colspan="2" class="table-setting-heading"><?php esc_html_e( 'Sale', 'woostify-pro' ); ?></th>
									</tr>

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Background', 'woostify-pro' ); ?>:</th>
										<td>
											<input class="woostify-admin-color-picker" name="woostify_product_label_background" type="text" id="woostify_product_label_background" value="<?php echo esc_attr( $options['label_bg'] ); ?>">
										</td>
									</tr>

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Text Color', 'woostify-pro' ); ?>:</th>
										<td>
											<input class="woostify-admin-color-picker" name="woostify_product_label_text_color" type="text" id="woostify_product_label_text_color" value="<?php echo esc_attr( $options['text_color'] ); ?>">
										</td>
									</tr>

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Sale Percentage', 'woostify-pro' ); ?>:</th>
										<td>
											<label for="woostify_product_label_sale_percentage">
												<input class="woostify-filter-value" name="woostify_product_label_sale_percentage" type="checkbox" id="woostify_product_label_sale_percentage" value="<?php echo esc_attr( '1' === $options['sale_percentage'] ? '1' : '0' ); ?>" <?php checked( $options['sale_percentage'], '1' ); ?>>
											</label>
										</td>
									</tr>

									<tr>
										<th colspan="2" class="table-setting-heading"><?php esc_html_e( 'New', 'woostify-pro' ); ?></th>
									</tr>

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Enable', 'woostify-pro' ); ?>:</th>
										<td>
											<label for="woostify_product_label_new_enable">
												<input class="woostify-filter-value" name="woostify_product_label_new_enable" type="checkbox" id="woostify_product_label_new_enable" value="<?php echo esc_attr( '1' === $options['new_enable'] ? '1' : '0' ); ?>" <?php checked( $options['new_enable'], '1' ); ?>>
											</label>
										</td>
									</tr>

									<tr class="woostify-filter-item woostify-product-label-new-options" style="display: <?php echo '1' === $options['new_enable'] ? 'table-row' : 'none'; ?>;">
										<th scope="row"><?php esc_html_e( 'Day', 'woostify-pro' ); ?>:</th>
										<td>
											<label for="woostify_product_label_new_day">
												<input name="woostify_product_label_new_day" type="number" id="woostify_product_label_new_day" value="<?php echo esc_attr( $options['new_day'] ); ?>">
												<code>day</code>
											</label>
											<p class="woostify-setting-description"><?php esc_html_e( 'Set a number of days to keep your products marked as "New" after creation.', 'woostify-pro' ); ?></p>
										</td>
									</tr>

									<tr class="woostify-filter-item woostify-product-label-new-options" style="display: <?php echo '1' === $options['new_enable'] ? 'table-row' : 'none'; ?>;">
										<th scope="row"><?php esc_html_e( 'Background', 'woostify-pro' ); ?>:</th>
										<td>
											<input class="woostify-admin-color-picker" name="woostify_product_label_new_background" type="text" id="woostify_product_label_new_background" value="<?php echo esc_attr( $options['new_label_bg'] ); ?>">
										</td>
									</tr>

									<tr class="woostify-filter-item woostify-product-label-new-options" style="display: <?php echo '1' === $options['new_enable'] ? 'table-row' : 'none'; ?>;">
										<th scope="row"><?php esc_html_e( 'Text Color', 'woostify-pro' ); ?>:</th>
										<td>
											<input class="woostify-admin-color-picker" name="woostify_product_label_new_text_color" type="text" id="woostify_product_label_new_text_color" value="<?php echo esc_attr( $options['new_text_color'] ); ?>">
										</td>
									</tr>

									<tr>
										<th colspan="2" class="table-setting-heading"><?php esc_html_e( 'Hot', 'woostify-pro' ); ?></th>
									</tr>

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Enable', 'woostify-pro' ); ?>:</th>
										<td>
											<label for="woostify_product_label_hot_enable">
												<input class="woostify-filter-value" name="woostify_product_label_hot_enable" type="checkbox" id="woostify_product_label_hot_enable" value="<?php echo esc_attr( '1' === $options['hot_enable'] ? '1' : '0' ); ?>" <?php checked( $options['hot_enable'], '1' ); ?>>
											</label>
										</td>
									</tr>

									<tr class="woostify-filter-item woostify-product-label-hot-options" style="display: <?php echo '1' === $options['hot_enable'] ? 'table-row' : 'none'; ?>;">
										<th scope="row"><?php esc_html_e( 'Background', 'woostify-pro' ); ?>:</th>
										<td>
											<input class="woostify-admin-color-picker" name="woostify_product_label_hot_background" type="text" id="woostify_product_label_hot_background" value="<?php echo esc_attr( $options['hot_label_bg'] ); ?>">
										</td>
									</tr>

									<tr class="woostify-filter-item woostify-product-label-hot-options" style="display: <?php echo '1' === $options['hot_enable'] ? 'table-row' : 'none'; ?>;">
										<th scope="row"><?php esc_html_e( 'Text Color', 'woostify-pro' ); ?>:</th>
										<td>
											<input class="woostify-admin-color-picker" name="woostify_product_label_hot_text_color" type="text" id="woostify_product_label_hot_text_color" value="<?php echo esc_attr( $options['hot_text_color'] ); ?>">
										</td>
									</tr>

									<tr>
										<th colspan="2" class="table-setting-heading"><?php esc_html_e( 'Out of stock', 'woostify-pro' ); ?></th>
									</tr>

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Background', 'woostify-pro' ); ?>:</th>
										<td>
											<input class="woostify-admin-color-picker" name="woostify_product_label_out_of_stock_background" type="text" id="woostify_product_label_out_of_stock_background" value="<?php echo esc_attr( $options['out_of_stock_label_bg'] ); ?>">
										</td>
									</tr>

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Text Color', 'woostify-pro' ); ?>:</th>
										<td>
											<input class="woostify-admin-color-picker" name="woostify_product_label_out_of_stock_text_color" type="text" id="woostify_product_label_out_of_stock_text_color" value="<?php echo esc_attr( $options['out_of_stock_text_color'] ); ?>">
										</td>
									</tr>

									<tr>
										<th colspan="2" class="table-setting-heading"><?php esc_html_e( 'Attribute', 'woostify-pro' ); ?></th>
									</tr>

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Background', 'woostify-pro' ); ?>:</th>
										<td>
											<input class="woostify-admin-color-picker" name="woostify_product_label_attribute_background" type="text" id="woostify_product_label_attribute_background" value="<?php echo esc_attr( $options['attribute_label_bg'] ); ?>">
										</td>
									</tr>

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Text Color', 'woostify-pro' ); ?>:</th>
										<td>
											<input class="woostify-admin-color-picker" name="woostify_product_label_attribute_text_color" type="text" id="woostify_product_label_attribute_text_color" value="<?php echo esc_attr( $options['attribute_text_color'] ); ?>">
										</td>
									</tr>

								</table>
							</div>

							<div class="woostify-settings-section-footer">
								<span class="save-options button button-primary"><?php esc_html_e( 'Save', 'woostify-pro' ); ?></span>
								<span class="spinner"></span>
							</div>
						</div>
					</div>
				</div>
			</div>
			<style type="text/css">
				.woostify-product-label-shape-picker {
					display: flex;
					flex-wrap: wrap;
					gap: 15px;
				}
				.woostify-shape-option {
					width: 70px;
					height: 70px;
					border: 1px solid #ddd;
					border-radius: 4px;
					display: flex;
					align-items: center;
					justify-content: center;
					cursor: pointer;
					transition: all 0.2s ease;
					padding: 10px;
					box-sizing: border-box;
					color: #1346af;
				}
				.woostify-shape-option:hover {
					border-color: #1346af;
					background-color: #f9f9f9;
				}
				.woostify-shape-option.active {
					border-color: #1346af;
					box-shadow: 0 0 0 1px #1346af;
					background-color: #e6ecf7;
				}
				.woostify-shape-option svg {
					width: 100%;
					height: 100%;
				}
			</style>
			<script type="text/javascript">
				jQuery( document ).ready( function( $ ) {
					// Shape picker selection
					$( '.woostify-shape-option' ).on( 'click', function() {
						$( '.woostify-shape-option' ).removeClass( 'active' );
						$( this ).addClass( 'active' );
						var val = $( this ).data( 'value' );
						$( 'input[name="woostify_product_label_style"]' ).val( val );
					});

					$( '#woostify_product_label_new_enable' ).on( 'change', function() {
						var $options = $( '.woostify-product-label-new-options' );
						if ( $( this ).is( ':checked' ) ) {
							$options.css( 'display', 'table-row' );
						} else {
							$options.hide();
						}
					});
					$( '#woostify_product_label_hot_enable' ).on( 'change', function() {
						var $options = $( '.woostify-product-label-hot-options' );
						if ( $( this ).is( ':checked' ) ) {
							$options.css( 'display', 'table-row' );
						} else {
							$options.hide();
						}
					});
				});
			</script>
			<?php
		}

		/**
		 * Get attribute's properties
		 *
		 * @param string $taxonomy Taxonomy.
		 *
		 * @return object
		 */
		public function get_tax_attribute( $taxonomy ) {
			global $wpdb;

			$attr = substr( $taxonomy, 3 );
			$attr = $wpdb->get_row( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = '$attr'" ); // phpcs:ignore

			return $attr;
		}

		/**
		 * Disable Out of Stock Variations.
		 *
		 * @param boolean $is_active The active.
		 * @param object  $variation The variation.
		 */
		public function disable_variations_out_of_stock( $is_active, $variation ) {
			if ( ! $variation->is_in_stock() ) {
				return false;
			}

			return $is_active;
		}

	}

	Woostify_Product_Label::get_instance();
endif;
