<?php
/**
 * Script Class.
 * Frontend stylesheeets and scripts load if page has required shortcode.
 * Dashboard stylesheets and scripts in booking calendar page.
 *
 * @since 1.0.0
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
if ( ! class_exists( 'SBUY_Script' ) ) {
	class SBUY_Script {
		/**
		 * Constructor
		 *
		 * @since 1.0.0
		 */
		public function __construct() {
			add_action( 'wp_enqueue_scripts', array( $this, 'stylesheets' ) );
			add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
			add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
		}

		/**
		 * Frontend stylesheets
		 *
		 * @since 1.0.0
		 */
		public function stylesheets() {

			global $post;

			if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'simply-buy-cart' ) ) {
				wp_enqueue_style( 'select2-style', SBUY_CORE_PLUGIN_URL . 'assets/style/select2.min.css', array(), SBUY_PLUGIN_VERSION, 'all' );
				wp_enqueue_style( 'simplybuy-style', SBUY_CORE_PLUGIN_URL . 'assets/style/simplybuy-cart.min.css', array( 'select2-style' ), SBUY_PLUGIN_VERSION, 'all' );
			}

			if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'simply-buy-checkout' ) ) {
				wp_enqueue_style( 'zebra-datepicker', SBUY_CORE_PLUGIN_URL . 'assets/style/zebra_datepicker.min.css', array(), SBUY_PLUGIN_VERSION, 'all' );
				wp_enqueue_style( 'simplybuy-style', SBUY_CORE_PLUGIN_URL . 'assets/style/simplybuy-checkout.min.css', array(
					'zebra-datepicker'
				), SBUY_PLUGIN_VERSION, 'all' );
			}
		}

		/**
		 * Frontend scripts
		 *
		 * @since 1.0.0
		 */
		public function scripts() {

			global $post;

			if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'simply-buy-cart' ) ) {
				wp_enqueue_script( 'select2-script', SBUY_CORE_PLUGIN_URL . 'assets/script/select2.min.js', array( 'jquery' ), SBUY_PLUGIN_VERSION, array( 'in_footer' => false ) );
				wp_enqueue_script( 'sbuy-script', SBUY_CORE_PLUGIN_URL . 'assets/script/simplybuy-cart.min.js', array( 'select2-script' ), SBUY_PLUGIN_VERSION, array( 'in_footer' => true ) );

				wp_localize_script( 'sbuy-script', 'sbuy_obj', array(
					'ajaxurl'                     => admin_url( 'admin-ajax.php' ),
					'nonce'                       => wp_create_nonce( 'simplybuy-nonce' ),
					'searchbox_placeholder'       => esc_html__( 'Search for item...', 'simply-buy' ),
					'searchbox_input_placeholder' => esc_html__( 'E.g. file folder, shoe rack, floor lamp, etc.', 'simply-buy' ),
					'product_price_label'         => esc_html__( 'Price', 'simply-buy' ),
					'add_product_button'          => esc_html__( 'Add', 'simply-buy' ),
					'cart_page_link'              => get_permalink( sbuy_get_localized_page_id( 'simply_buy_cart_page' ) ),
					'checkout_page_link'          => get_permalink( sbuy_get_localized_page_id( 'simply_buy_checkout_page' ) ),
					'currency_symbol'             => SBUY_CORE_CURRENCY_SYMBOL,
					'cookie_name'                 => sbuy_get_cookie_name(),
					'subtotal_label'              => esc_html__( 'Subtotal', 'simply-buy' ),
					'checkout_button_label'       => esc_html__( 'Go to checkout', 'simply-buy' ),
					'lang'                        => sbuy_get_current_language(),
				) );
			}

			if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'simply-buy-checkout' ) ) {
				wp_enqueue_script( 'zebra-datepicker', SBUY_CORE_PLUGIN_URL . 'assets/script/zebra_datepicker.min.js', array( 'jquery' ), SBUY_PLUGIN_VERSION, array( 'in_footer' => true ) );
				wp_enqueue_script( 'sbuy-script', SBUY_CORE_PLUGIN_URL . 'assets/script/simplybuy-checkout.min.js', array(
					'jquery',
					'jquery-ui-core',
					'jquery-ui-tabs',
					'jquery-ui-dialog',
					'zebra-datepicker'
				), SBUY_PLUGIN_VERSION, array( 'in_footer' => true ) );

				wp_localize_script( 'sbuy-script', 'sbuy_obj', array(
					'ajaxurl'                  => admin_url( 'admin-ajax.php' ),
					'nonce'                    => wp_create_nonce( 'simplybuy-nonce' ),
					'product_price_label'      => esc_html__( 'Price', 'simply-buy' ),
					'add_product_button'       => esc_html__( 'Add', 'simply-buy' ),
					'cart_page_link'           => get_permalink( sbuy_get_localized_page_id( 'simply_buy_cart_page' ) ),
					'checkout_page_link'       => get_permalink( sbuy_get_localized_page_id( 'simply_buy_checkout_page' ) ),
					'thankyou_page_link'       => get_permalink( sbuy_get_localized_page_id( 'simply_buy_thankyou_page' ) ),
					'signin_modal_title'       => esc_html__( 'Register', 'simply-buy' ),
					'signin_modal_no_username' => esc_html__( 'Please enter a value', 'simply-buy' ),
					'signin_modal_no_password' => esc_html__( 'Please enter a value', 'simply-buy' ),
					'minimum_pickup'           => sbuy_has_pro_access() ? sbuy_get_options( 'minimum_pickup', 'sbuy_settings_finance' ): 0,
					'currency_symbol'          => SBUY_CORE_CURRENCY_SYMBOL,
					'surchage_text'            => esc_html__( 'Surcharge to the minimum pick-up price:', 'simply-buy' ),
					'morning_shift_name'       => esc_html__( 'Morning', 'simply-buy' ),
					'afternoon_shift_name'     => esc_html__( 'Afternoon', 'simply-buy' ),
					'terms_error'              => esc_html__( 'Please check the box.', 'simply-buy' ),
					/* translators: %d: number of missing required fields */
					'fields_missing_template'  => esc_html__( 'Please fill in all required fields. (%d field(s) missing)', 'simply-buy' ),
					'no_available_time'        => esc_html__( 'No available time', 'simply-buy' ),
					'initial_text_for_time'    => esc_html__( 'Please select a day first', 'simply-buy' ),
					'cookie_name'              => sbuy_get_cookie_name(),
					'disabled_dates'           => sbuy_get_disabled_dates(),
					'min_booking_days'         => max( 1, (int) sbuy_get_options( 'min_booking_days', 'sbuy_settings_time' ) ),
					'email_error'              => esc_html__( 'Email address not valid', 'simply-buy' ),
					'postalcode_error'         => esc_html__( 'Postalcode not valid', 'simply-buy' ),
					'phone_error'              => esc_html__( 'Phone number not valid', 'simply-buy' ),
					'date_format'              => get_option( 'date_format' ),
					'months'                   => sbuy_get_months(),
					'days'                     => sbuy_get_days(),
					'clear_datepicker'         => esc_html__( 'Clear', 'simply-buy' ),
					'current_time'             => current_datetime()->format( 'H' ),
					'locale'                   => get_locale(),
					'timezone'                 => wp_timezone_string(),
					'surcharge_floor'          => sbuy_get_options( 'surcharge_floor', 'sbuy_settings_finance' ) ?? 0,
					'surcharge_15_50_meter'    => sbuy_get_options( 'surcharge_15_50_meter', 'sbuy_settings_finance' ) ?? 0,
					'surcharge_50_plus_meter'  => sbuy_get_options( 'surcharge_50_plus_meter', 'sbuy_settings_finance' ) ?? 0,
					'lang'                     => sbuy_get_current_language(),
				) );
			}
		}

		/**
		 * Dashboard scripts
		 *
		 * @param string $hook unique name of the page.
		 *
		 * @since 1.0.0
		 */
		public function admin_scripts( $hook ) {

			if ( 'simplybuy_page_simply-buy-bookings' === $hook ) {
				wp_enqueue_style( 'simplybuy-admin-css', SBUY_CORE_PLUGIN_URL . 'assets/style/simplybuy-bookings.min.css', array(), SBUY_PLUGIN_VERSION, 'all' );
				wp_enqueue_script( 'simplybuy-booking', SBUY_CORE_PLUGIN_URL . 'assets/script/simplybuy-bookings.min.js', array(
					'jquery',
					'jquery-ui-core',
					'jquery-ui-tabs'
				), SBUY_PLUGIN_VERSION, array( 'in_footer' => true ) );

				wp_localize_script( 'simplybuy-booking', 'sbuy_obj', array(
					'nonce' => wp_create_nonce( 'simplybuy-nonce' )
				) );
			}

			if ( 'toplevel_page_simply-buy' === $hook ) {
				wp_enqueue_style( 'simplybuy-admin-css', SBUY_CORE_PLUGIN_URL . 'assets/style/simplybuy-settings.min.css', array(), SBUY_PLUGIN_VERSION, 'all' );
				wp_enqueue_script( 'simplybuy-settings', SBUY_CORE_PLUGIN_URL . 'assets/script/simplybuy-settings.min.js', array( 'jquery' ), SBUY_PLUGIN_VERSION, array( 'in_footer' => true ) );
			}
		}
	}
}
