<?php
/**
 * Business Interface WooCommerce section in Customizer.
 *
 * @package     Business Interface
 * @author  WPInterface Team
 * @since   1.0.0
 */

/**
 * Do not allow direct script access.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Business_Interface_Customizer_WooCommerce' ) ) :
	/**
	 * Business Interface WooCommerce section in Customizer.
	 */
	class Business_Interface_Customizer_WooCommerce {

		/**
		 * Primary class constructor.
		 *
		 * @since 1.0.0
		 */
		public function __construct() {

			// Registers our custom options in Customizer.
			add_filter( 'business_interface_customizer_options', array( $this, 'register_options' ), 20 );
			add_action( 'customize_register', array( $this, 'customizer_tweak' ), 20 );

			// Add default values for WooCommerce options.
			add_filter( 'business_interface_default_option_values', array( $this, 'default_customizer_values' ) );

			// Add localized strings to script.
			add_filter( 'business_interface_customizer_localized', array( $this, 'customizer_localized_strings' ) );
		}

		/**
		 * Add defaults for new WooCommerce customizer options.
		 *
		 * @param  array $defaults Array of default values.
		 * @return array           Array of default values.
		 */
		public function default_customizer_values( $defaults ) {

			$defaults['business_interface_wc_product_gallery_lightbox'] = true;
			$defaults['business_interface_wc_product_gallery_zoom']     = true;
			$defaults['business_interface_shop_product_hover']          = 'none';
			$defaults['business_interface_product_sale_badge']          = 'percentage';
			$defaults['business_interface_product_sale_badge_text']     = esc_html__( 'Sale!', 'business-interface' );
			$defaults['business_interface_wc_product_slider_arrows']    = true;
			$defaults['business_interface_wc_product_gallery_style']    = 'default';
			$defaults['business_interface_wc_product_sidebar_position'] = 'no-sidebar';
			$defaults['business_interface_wc_sidebar_position']         = 'no-sidebar';
			$defaults['business_interface_wc_upsell_products']          = true;
			$defaults['business_interface_wc_upsells_columns']          = 4;
			$defaults['business_interface_wc_upsells_rows']             = 1;
			$defaults['business_interface_wc_related_products']         = true;
			$defaults['business_interface_wc_related_columns']          = 4;
			$defaults['business_interface_wc_related_rows']             = 1;
			$defaults['business_interface_wc_cross_sell_products']      = true;
			$defaults['business_interface_wc_cross_sell_rows']          = 1;
			$defaults['business_interface_product_catalog_elements']    = array(
				'category' => true,
				'title'    => true,
				'ratings'  => true,
				'price'    => true,
			);

			return $defaults;
		}

		/**
		 * Tweak Customizer.
		 *
		 * @since 1.0.0
		 * @param WP_Customize_Manager $customizer Instance of WP_Customize_Manager class.
		 */
		public function customizer_tweak( $customizer ) {
			// Move WooCommerce panel.
			$customizer->get_panel( 'woocommerce' )->priority = 10;

			return $customizer;
		}

		/**
		 * Registers our custom options in Customizer.
		 *
		 * @since 1.0.0
		 * @param array $options Array of customizer options.
		 */
		public function register_options( $options ) {

			// Shop image hover effect.
			$options['setting']['business_interface_shop_product_hover'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_select',
				'control'           => array(
					'type'        => 'business-interface-select',
					'section'     => 'woocommerce_product_catalog',
					'label'       => esc_html__( 'Product image hover', 'business-interface' ),
					'description' => esc_html__( 'Effect for product image on hover', 'business-interface' ),
					'choices'     => array(
						'none'       => esc_html__( 'No Effect', 'business-interface' ),
						'image-swap' => esc_html__( 'Image Swap', 'business-interface' ),
					),
				),
			);

			// Sale badge.
			$options['setting']['business_interface_product_sale_badge'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_select',
				'control'           => array(
					'type'        => 'business-interface-select',
					'section'     => 'woocommerce_product_catalog',
					'label'       => esc_html__( 'Product sale badge', 'business-interface' ),
					'description' => esc_html__( 'Choose what to display on the product sale badge.', 'business-interface' ),
					'choices'     => array(
						'hide'       => esc_html__( 'Hide badge', 'business-interface' ),
						'percentage' => esc_html__( 'Show percentage', 'business-interface' ),
						'text'       => esc_html__( 'Show text', 'business-interface' ),
					),
				),
			);

			// Sale badge text.
			$options['setting']['business_interface_product_sale_badge_text'] = array(
				'transport'         => 'postMessage',
				'sanitize_callback' => 'sanitize_text_field',
				'control'           => array(
					'type'        => 'business-interface-text',
					'label'       => esc_html__( 'Sale badge text', 'business-interface' ),
					'description' => esc_html__( 'Add custom text for the product sale badge.', 'business-interface' ),
					'placeholder' => esc_html__( 'Sale!', 'business-interface' ),
					'section'     => 'woocommerce_product_catalog',
					'required'    => array(
						array(
							'control'  => 'business_interface_product_sale_badge',
							'value'    => 'text',
							'operator' => '==',
						),
					),
				),
			);

			// Catalog product elements.
			$options['setting']['business_interface_product_catalog_elements'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_sortable',
				'control'           => array(
					'type'        => 'business-interface-sortable',
					'section'     => 'woocommerce_product_catalog',
					'label'       => esc_html__( 'Product details', 'business-interface' ),
					'description' => esc_html__( 'Set order and visibility for product details.', 'business-interface' ),
					'choices'     => array(
						'title'    => esc_html__( 'Title', 'business-interface' ),
						'ratings'  => esc_html__( 'Ratings', 'business-interface' ),
						'price'    => esc_html__( 'Price', 'business-interface' ),
						'category' => esc_html__( 'Category', 'business-interface' ),
					),
				),
			);

			// Section.
			$options['section']['business_interface_woocommerce_single_product'] = array(
				'title'    => esc_html__( 'Single Product', 'business-interface' ),
				'priority' => 50,
				'panel'    => 'woocommerce',
			);

			// Product Gallery Zoom.
			$options['setting']['business_interface_wc_product_gallery_zoom'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_toggle',
				'control'           => array(
					'type'        => 'business-interface-toggle',
					'label'       => esc_html__( 'Gallery Zoom', 'business-interface' ),
					'description' => esc_html__( 'Enable zoom effect when hovering product gallery.', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'space'       => true,
				),
			);

			// Product Gallery Lightbox.
			$options['setting']['business_interface_wc_product_gallery_lightbox'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_toggle',
				'control'           => array(
					'type'        => 'business-interface-toggle',
					'label'       => esc_html__( 'Gallery Lightbox', 'business-interface' ),
					'description' => esc_html__( 'Open product gallery images in lightbox.', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'space'       => true,
				),
			);

			// Product slider arrows.
			$options['setting']['business_interface_wc_product_slider_arrows'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_toggle',
				'control'           => array(
					'type'        => 'business-interface-toggle',
					'label'       => esc_html__( 'Slider Arrows', 'business-interface' ),
					'description' => esc_html__( 'Enable left and right arrows on product gallery slider.', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'space'       => true,
				),
			);

			// Related Products.
			$options['setting']['business_interface_wc_related_products'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_toggle',
				'control'           => array(
					'type'        => 'business-interface-toggle',
					'label'       => esc_html__( 'Related Products', 'business-interface' ),
					'description' => esc_html__( 'Display related products.', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'space'       => true,
				),
			);

			// Related product column count.
			$options['setting']['business_interface_wc_related_columns'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_range',
				'control'           => array(
					'type'        => 'business-interface-range',
					'label'       => esc_html__( 'Related Products Columns', 'business-interface' ),
					'description' => esc_html__( 'How many related products should be shown per row?', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'min'         => 1,
					'max'         => 6,
					'step'        => 1,
					'required'    => array(
						array(
							'control'  => 'business_interface_wc_related_products',
							'value'    => true,
							'operator' => '==',
						),
					),
				),
			);

			// Related product row count.
			$options['setting']['business_interface_wc_related_rows'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_range',
				'control'           => array(
					'type'        => 'business-interface-range',
					'label'       => esc_html__( 'Related Products Rows', 'business-interface' ),
					'description' => esc_html__( 'How many rows of related products should be shown?', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'min'         => 1,
					'max'         => 5,
					'step'        => 1,
					'required'    => array(
						array(
							'control'  => 'business_interface_wc_related_products',
							'value'    => true,
							'operator' => '==',
						),
					),
				),
			);

			// Up-Sell Products.
			$options['setting']['business_interface_wc_upsell_products'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_toggle',
				'control'           => array(
					'type'        => 'business-interface-toggle',
					'label'       => esc_html__( 'Up-Sell Products', 'business-interface' ),
					'description' => esc_html__( 'Display linked upsell products.', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'space'       => true,
				),
			);

			// Up-Sells column count.
			$options['setting']['business_interface_wc_upsells_columns'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_range',
				'control'           => array(
					'type'        => 'business-interface-range',
					'label'       => esc_html__( 'Up-Sell Products Columns', 'business-interface' ),
					'description' => esc_html__( 'How many up-sell products should be shown per row?', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'min'         => 1,
					'max'         => 6,
					'step'        => 1,
					'required'    => array(
						array(
							'control'  => 'business_interface_wc_upsell_products',
							'value'    => true,
							'operator' => '==',
						),
					),
				),
			);

			// Up-Sells rows count.
			$options['setting']['business_interface_wc_upsells_rows'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_range',
				'control'           => array(
					'type'        => 'business-interface-range',
					'label'       => esc_html__( 'Up-Sell Products Rows', 'business-interface' ),
					'description' => esc_html__( 'How many rows of up-sell products should be shown?', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'min'         => 1,
					'max'         => 6,
					'step'        => 1,
					'required'    => array(
						array(
							'control'  => 'business_interface_wc_upsell_products',
							'value'    => true,
							'operator' => '==',
						),
					),
				),
			);

			// Cross-Sell Products.
			$options['setting']['business_interface_wc_cross_sell_products'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_toggle',
				'control'           => array(
					'type'        => 'business-interface-toggle',
					'label'       => esc_html__( 'Cross-Sell Products', 'business-interface' ),
					'description' => esc_html__( 'Display linked cross-sell products on cart page.', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'space'       => true,
				),
			);

			// Cross-Sells rows count.
			$options['setting']['business_interface_wc_cross_sell_rows'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_range',
				'control'           => array(
					'type'        => 'business-interface-range',
					'label'       => esc_html__( 'Cross-Sell Products Rows', 'business-interface' ),
					'description' => esc_html__( 'How many rows of cross-sell products should be shown?', 'business-interface' ),
					'section'     => 'business_interface_woocommerce_single_product',
					'min'         => 1,
					'max'         => 6,
					'step'        => 1,
					'required'    => array(
						array(
							'control'  => 'business_interface_wc_cross_sells_products',
							'value'    => true,
							'operator' => '==',
						),
					),
				),
			);

			$sidebar_options = array();

			$sidebar_options['business_interface_wc_sidebar_position'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_select',
				'control'           => array(
					'type'        => 'business-interface-select',
					'label'       => esc_html__( 'WooCommerce', 'business-interface' ),
					'description' => esc_html__( 'Choose default sidebar position for cart, checkout and catalog pages. You can change this setting per page via metabox settings.', 'business-interface' ),
					'section'     => 'business_interface_section_sidebar',
					'choices'     => array(
						'default'       => esc_html__( 'Default', 'business-interface' ),
						'no-sidebar'    => esc_html__( 'No Sidebar', 'business-interface' ),
						'left-sidebar'  => esc_html__( 'Left Sidebar', 'business-interface' ),
						'right-sidebar' => esc_html__( 'Right Sidebar', 'business-interface' ),
					),
				),
			);

			$sidebar_options['business_interface_wc_product_sidebar_position'] = array(
				'transport'         => 'refresh',
				'sanitize_callback' => 'business_interface_sanitize_select',
				'control'           => array(
					'type'        => 'business-interface-select',
					'label'       => esc_html__( 'WooCommerce - Single Product', 'business-interface' ),
					'description' => esc_html__( 'Choose default sidebar position layout for product pages. You can change this setting per product via metabox settings.', 'business-interface' ),
					'section'     => 'business_interface_section_sidebar',
					'choices'     => array(
						'default'       => esc_html__( 'Default', 'business-interface' ),
						'no-sidebar'    => esc_html__( 'No Sidebar', 'business-interface' ),
						'left-sidebar'  => esc_html__( 'Left Sidebar', 'business-interface' ),
						'right-sidebar' => esc_html__( 'Right Sidebar', 'business-interface' ),
					),
				),
			);

			$options['setting'] = business_interface_array_insert( $options['setting'], $sidebar_options, 'business_interface_archive_sidebar_position' );

			return $options;
		}

		/**
		 * Add localize strings.
		 *
		 * @param  array $strings Array of strings to be localized.
		 * @return array          Modified string array.
		 */
		public function customizer_localized_strings( $strings ) {

			// Preview a random single product for WooCommerce > Single Product section.
			$products = get_posts(
				array(
					'post_type'      => 'product',
					'posts_per_page' => 1,
					'orderby'        => 'rand',
				)
			);

			if ( count( $products ) ) {
				$strings['preview_url_for_section']['business_interface_woocommerce_single_product'] = get_permalink( $products[0] );
			}

			return $strings;
		}
	}
endif;
new Business_Interface_Customizer_WooCommerce();
