<?php
if ( ! defined( '_S_VERSION' ) ) {
	define( '_S_VERSION', '0.2' );
}

function usg_scripts() {
    wp_enqueue_style( 'usg-style', get_stylesheet_uri(), array(), _S_VERSION );
    wp_enqueue_style( 'base', get_template_directory_uri() . '/assets/css/base.css', array(), _S_VERSION );
    wp_enqueue_style( 'layout', get_template_directory_uri() . '/assets/css/layout.css', array(), _S_VERSION );
    wp_enqueue_script('main-js', get_template_directory_uri() . '/assets/js/main.js', array('jquery'), _S_VERSION, true);
    wp_enqueue_script('jquery');
    
    if(is_page_template('templates/front-page.php')){
        
        wp_enqueue_style( 'front-page', get_template_directory_uri() . '/assets/css/front-page.css', array(), _S_VERSION);
        
        wp_enqueue_script('jquery-mask', 'https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js', array('jquery'), '1.14.16', true);
        wp_enqueue_style('owl-carousel-css', get_template_directory_uri() . '/assets/owl-carousel/owl.carousel.min.css', array(), '2.3.4');
        wp_enqueue_style('owl-carousel-theme', get_template_directory_uri() . '/assets/owl-carousel/owl.theme.default.min.css', array(), '2.3.4');
        wp_enqueue_script('owl-carousel-js', get_template_directory_uri() . '/assets/owl-carousel/owl.carousel.min.js', array('jquery'), '2.3.4', true);

        wp_enqueue_script('badanie-js', get_template_directory_uri() . '/assets/js/badanie-select.js', array('jquery'), _S_VERSION, true);
        wp_enqueue_script('calendar-js', get_template_directory_uri() . '/assets/js/calendar.js', array('jquery'), _S_VERSION, true);

        
        wp_enqueue_script('order-js', get_template_directory_uri() . '/assets/js/order.js', array('jquery'), _S_VERSION, true);

        $nonce = wp_create_nonce('create_order_nonce');
        
        wp_localize_script('order-js', 'myAjax', array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'nonce' => $nonce
        ));

        wp_enqueue_script('lightbox-user-js', get_template_directory_uri() . '/assets/js/lightbox.js', array('jquery', 'lightbox-js'), _S_VERSION, true);
        
        wp_enqueue_style('lightbox-css', 'https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/css/lightbox.min.css');
        wp_enqueue_script('lightbox-js', 'https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/js/lightbox.min.js', array('jquery'), null, true);
        
        $blocked_dates = get_option('blocked_dates', '');
        $day_limits = get_option('day_limits', '');
        $time_gaps = get_option('time_gaps', '');
        $block_week_days = get_option('blocked_week_days', '');
        $days_of_week = get_option('blocked_days', '');

        $days_of_week_array = !empty($days_of_week) ? explode("\n", $days_of_week) : array();
        $days_of_week_array = array_map(function($entry) {
            return trim($entry, "\r");
        }, $days_of_week_array);

        $blocked_dates_array = !empty($blocked_dates) ? explode("\n", $blocked_dates) : array();
        $blocked_dates_array = array_map(function($entry) {
            return trim($entry, "\r");
        }, $blocked_dates_array);

        $time_gaps_array = !empty($time_gaps) ? explode("\n", $time_gaps) : array();
        $time_gaps_array = array_map(function($entry) {
            return trim($entry, "\r");
        }, $time_gaps_array);

        wp_localize_script('calendar-js', 'calendarData', array(
            'blockedDates' => $blocked_dates_array,
            'dayLimits' => $day_limits,
            'timeGaps' => $time_gaps_array,
            'blockWeekDays' => $block_week_days,
            'daysWeek' => $days_of_week_array,
        ));
    }

    if(is_page_template('templates/testomonials.php')){
        wp_enqueue_style('owl-carousel-css', get_template_directory_uri() . '/assets/owl-carousel/owl.carousel.min.css', array(), '2.3.4');
        wp_enqueue_style('owl-carousel-theme', get_template_directory_uri() . '/assets/owl-carousel/owl.theme.default.min.css', array(), '2.3.4');
        wp_enqueue_script('owl-carousel-js', get_template_directory_uri() . '/assets/owl-carousel/owl.carousel.min.js', array('jquery'), '2.3.4', true);

        wp_enqueue_script('testomonials-js', get_template_directory_uri() . '/assets/js/testomonials.js', array('jquery'), _S_VERSION, true);

        wp_enqueue_style( 'testomonials-page', get_template_directory_uri() . '/assets/css/testomonials.css', array(), _S_VERSION);
    }
    if(is_page_template('templates/contact.php')){
        wp_enqueue_style('contact-css', get_template_directory_uri() . '/assets/css/contact.css', array(), _S_VERSION);
    }
    if(is_page_template('templates/thank-you.php')){
        wp_enqueue_style('thank-css', get_template_directory_uri() . '/assets/css/thank-you.css', array(), _S_VERSION);
    }
    wp_enqueue_style( 'checkout-page', get_template_directory_uri() . '/assets/css/checkout.css', array(), _S_VERSION);
}

add_action('wp_enqueue_scripts', 'usg_scripts');