<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WPCSR_Scripts{
    function __construct(){
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
        //add_action( 'user_register', array( $this, 'auto_login_new_user' ) );
    }
    /*** GENERAL ***/
    function enqueue_scripts() {
        global $post, $wpcargo;
		$result_page_url = ( get_option('wpcsr_request_result') ) ? get_option('wpcsr_request_result') : 0 ;
		if ( ( is_a( $post, 'WP_Post' ) 
			&& ( has_shortcode( $post->post_content, 'wpcsr_single_rate_shipment' ) || has_shortcode( $post->post_content, 'wpcsr_rate_shipment' ) ) ) 
			|| is_home() 
			|| is_front_page() 
			|| is_page( $result_page_url )
		   ){
			$template       = get_page_template_slug( $post->ID );
			$is_dashboard   = $template == 'dashboard.php' ? 1 : 0 ;
            // Styles
            wp_enqueue_style( 'jquery-ui-styles', WPCSR_URL . 'assets/css/jquery-ui.min.css', array(), WPCSR_VERSION );
            wp_enqueue_style( 'wpcsr-datetime-styles', WPCSR_URL . 'assets/css/jquery.datetimepicker.min.css', array(), WPCSR_VERSION );
            wp_enqueue_style( 'wpcsr-card-styles', WPCSR_URL . 'assets/css/card.min.css', WPCSR_VERSION );
			wp_enqueue_style( 'wpcsr-banner-styles', WPCSR_URL . 'assets/css/banner-style.css', WPCSR_VERSION );
			wp_enqueue_style( 'wpcsr-styles', WPCSR_URL . 'assets/css/style.css', WPCSR_VERSION );
			
            // Scripts
            wp_register_script( 'wpcsr-repeater-scripts', WPCSR_URL. 'assets/js/jquery.repeater.js', array( 'jquery' ), WPCSR_VERSION, true );
            wp_register_script( 'wpcsr-datetime-scripts', WPCSR_URL. 'assets/js/jquery.datetimepicker.min.js', array( 'jquery' ), WPCSR_VERSION, true );
            wp_register_script( 'wpcsr-scripts', WPCSR_URL. 'assets/js/jquery.js', array( 'jquery', 'jquery-ui-autocomplete' ), WPCSR_VERSION, true );
            wp_register_script( 'wpcsr-custom-script', WPCSR_URL . 'assets/js/custom.repeater.js', array( 'jquery' ), WPCSR_VERSION, true );
            
			wp_enqueue_script( 'jquery' );
			wp_enqueue_script( 'jquery-ui-autocomplete' );
            wp_enqueue_script( 'wpcsr-repeater-scripts' );
            wp_enqueue_script('wpcsr-custom-script'); 
            wp_enqueue_script('wpcsr-scripts'); 
			
			
            $wpcsrate       = isset($_GET['wpcsrate']) ? $_GET['wpcsrate'] : 1 ;
            $dashboardURL   = get_permalink().'?acccreated=true&wpcsrate='.$wpcsrate;
			$translations	= array(
				'ajaxurl' 				=> admin_url( 'admin-ajax.php' ),
				'formSubmitError'       => __( 'Something went wrong during process, Please try agian later.', 'wpcargo-shipment-rate' ),
				'dashboardURL'          => $dashboardURL,
				'deleteMessage'         => __( 'Are you sure you want to Delete Address?', 'wpcargo-shipment-rate' ),
				'errorEmailExist'       => __('Email already Exist.', 'wpcargo-shipment-rate'),
				'errorInCorrectEmail'   => __('Incorrent Email Format.', 'wpcargo-shipment-rate'),
				'errorPasswordlength'   => __('Password must at least 6 Characters.', 'wpcargo-shipment-rate'),
				'errorPasswordNotMatch' => __('Password NOT matched.', 'wpcargo-shipment-rate'),
				'confirmDeleteRepeater' => __( 'Are you sure you want to delete this Parcel?', 'wpcargo-shipment-rate' ),
				'searchShipperTitle'    => __( 'Select Shipper', 'wpcargo-shipment-rate' ),
				'searchReceiverTitle'   => __( 'Select Receiver', 'wpcargo-shipment-rate' ),
				'usernameLabel'         => __('Username', 'wpcargo-shipment-rate'),
				'passwordLabel'         => __('Password', 'wpcargo-shipment-rate'),
				'pickupRateLabel'       => __('Pickup Rate', 'wpcargo-shipment-rate'),
				'taxPercentage'         => $wpcargo->tax,
				'urlField'              => wpcsr_get_url_field_meta(),
				'pickupLabel'           => wpcsr_pickup_label(),
				'dropOffLabel'          => wpcsr_dropoff_label(),
				'pickupZone'            => get_wpcsr_pickup_rates_column('zone'),
				'referenceZone'         => wpcsr_pickup_reference(),
				'rateURL'               => '',
				'is_dashboard'			=> $is_dashboard,
				'timeFormat'			=> $wpcargo->time_format(),
				'dateFormat'			=> $wpcargo->date_format(),
				'currenySymbol' 		=> wpcsr_currency()
			);
            wp_localize_script( 'wpcsr-scripts', 'WPCSRAjaxHandler', $translations );
        }
    }
    /*** ADMIN ***/
    function admin_enqueue_scripts() {
        global $wpcargo;
		$translations = array(
			'ajaxurl'               => admin_url( 'admin-ajax.php' ),
			'rateMessage' 		    => __( 'Can\'t proceed to find rate. all field must be not Empty.', 'wpcargo-shipment-rate' ),
			'confirmDeleteRepeater' => __( 'Are you sure you want to delete this Parcel?', 'wpcargo-shipment-rate' ),
			'confirmDeleteWarehouse' => __( 'Are you sure you want to delete this Warehouse?', 'wpcargo-shipment-rate' ),
			'timeFormat'			=> $wpcargo->time_format(),
			'dateFormat'			=> $wpcargo->date_format(),
			'taxPercentage'         => $wpcargo->tax,
			'pickupLabel'           => wpcsr_pickup_label(),
			'dropOffLabel'          => wpcsr_dropoff_label(),
			'pickupZone'            => get_wpcsr_pickup_rates_column('zone'),
			'referenceZone'         => wpcsr_pickup_reference()
		);
		if( function_exists( 'get_woocommerce_currency_symbol' ) ){
			$translations['currenySymbol'] = get_woocommerce_currency_symbol();
		}
        // Styles
        wp_enqueue_style( 'jquery-ui-styles', WPCSR_URL . 'assets/css/jquery-ui.min.css', array(), WPCSR_VERSION );
        wp_register_style( 'wpcsr-font-awesome', WPCSR_URL . 'admin/assets/css/font-awesome.min.css', WPCSR_VERSION );  
        wp_register_style( 'wpcsr-style', WPCSR_URL . 'admin/assets/css/wpcsr-style.css', WPCSR_VERSION );
        wp_enqueue_style( 'wpcsr-font-awesome' );
        wp_enqueue_style( 'wpcsr-style' );
        wp_enqueue_script( 'jquery' );
        wp_enqueue_script( 'jquery-ui-autocomplete' );
        wp_register_script( 'wpcsr-repeater-scripts', WPCSR_URL. 'assets/js/jquery.repeater.js', array( 'jquery' ), WPCSR_VERSION, false );
        wp_register_script( 'wpcsr-js', WPCSR_URL. 'admin/assets/js/wpcsr-script.js', array( 'jquery' ) );
        wp_enqueue_script( 'wpcsr-repeater-scripts' );
		
        wp_localize_script( 'wpcsr-js', 'wpcsrAjaxHandler', $translations );
        wp_enqueue_script( 'wpcsr-js' );
        wp_enqueue_script( 'wpcsr-add-ajax', WPCSR_URL. 'admin/assets/js/shipment-rate-ajax.js', array('jquery'), WPCSR_VERSION, true );
        wp_localize_script( 'wpcsr-add-ajax', 'wpcsr_add', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), ) );
        
    }
	function auto_login_new_user( $user_id ) {
        wp_set_current_user($user_id);
        wp_set_auth_cookie($user_id);
        //wp_redirect( admin_url( 'profile.php' ) );
        exit;
    }}
new WPCSR_Scripts;