<?php

/**
 * Front Scripts Handler
 * 
 * @package cwpt
 * @since 1.0.0
 */

defined('ABSPATH') or die;

class CWPT_Front_Scripts
{
    /**
     * Enqueue front-end scripts and styles
     * 
     * @return void
     */
    public static function enqueue()
    {
        $plugin_url = plugin_dir_url(dirname(__FILE__) . '/../custom-woocommerce-product-type.php');
        
        wp_enqueue_style(
            'cwpt_front_css',
            $plugin_url . 'assets/css/cwpt_front.css',
            [],
            null
        );

        wp_enqueue_style(
            'cwpt_cmb2_front_css',
            $plugin_url . 'libraries/cmb2/css/cmb2-front.min.css',
            [],
            null
        );

        // Register datepicker assets for use when calendar fields are rendered
        // wp_register_style('jquery-ui-datepicker', $plugin_url . 'assets/css/jquery-ui.css', [], '1.13.2');
        wp_register_script('cwpt_front_js', $plugin_url . 'assets/js/cwpt_front.js', ['jquery', 'jquery-ui-datepicker'], null, true);

        if (is_product()) {
            wp_enqueue_style('jquery-ui-datepicker');
            wp_enqueue_script('cwpt_front_js');
            wp_localize_script('cwpt_front_js', 'customBookingAjax', [
                'ajax_url' => admin_url('admin-ajax.php'),
                'date_format' => 'mm/dd/yy',
                'min_date' => 0,
                'max_date' => '+1y',
                'show_calendar_input' => 'off',
            ]);

            // Ensure WooCommerce frontend scripts/styles are registered/enqueued for gallery features.
            if (class_exists('WC_Frontend_Scripts')) {
                WC_Frontend_Scripts::load_scripts();
            }

            if (function_exists('WC')) {
                $wc_url = WC()->plugin_url();
                $wc_version = defined('WC_VERSION') ? WC_VERSION : null;

                $wc_styles = [
                    'woocommerce-layout' => [
                        'src' => $wc_url . 'assets/css/woocommerce-layout.css',
                        'deps' => [],
                        'media' => 'all',
                        'rtl' => true,
                    ],
                    'woocommerce-smallscreen' => [
                        'src' => $wc_url . 'assets/css/woocommerce-smallscreen.css',
                        'deps' => ['woocommerce-layout'],
                        'media' => 'only screen and (max-width: 768px)',
                        'rtl' => true,
                    ],
                    'woocommerce-general' => [
                        'src' => $wc_url . 'assets/css/woocommerce.css',
                        'deps' => [],
                        'media' => 'all',
                        'rtl' => true,
                    ],
                ];

                foreach ($wc_styles as $handle => $style) {
                    if (!wp_style_is($handle, 'registered')) {
                        wp_register_style($handle, $style['src'], $style['deps'], $wc_version, $style['media']);
                        if (!empty($style['rtl'])) {
                            wp_style_add_data($handle, 'rtl', 'replace');
                        }
                    }
                    if (!wp_style_is($handle, 'enqueued')) {
                        wp_enqueue_style($handle);
                    }
                }

                // Ensure Photoswipe styles are available when lightbox support is enabled.
                if (current_theme_supports('wc-product-gallery-lightbox')) {
                    if (!wp_style_is('photoswipe', 'registered')) {
                        wp_register_style('photoswipe', $wc_url . 'assets/css/photoswipe/photoswipe.min.css', [], $wc_version);
                    }
                    if (!wp_style_is('photoswipe-default-skin', 'registered')) {
                        wp_register_style('photoswipe-default-skin', $wc_url . 'assets/css/photoswipe/default-skin/default-skin.min.css', ['photoswipe'], $wc_version);
                    }
                    if (!wp_style_is('photoswipe-default-skin', 'enqueued')) {
                        wp_enqueue_style('photoswipe-default-skin');
                    }
                }

                // Enqueue gallery scripts if registered (load_scripts should register them).
                if (wp_script_is('wc-single-product', 'registered') && !wp_script_is('wc-single-product', 'enqueued')) {
                    wp_enqueue_script('wc-single-product');
                }
                if (current_theme_supports('wc-product-gallery-zoom') && wp_script_is('zoom', 'registered') && !wp_script_is('zoom', 'enqueued')) {
                    wp_enqueue_script('zoom');
                }
                if (current_theme_supports('wc-product-gallery-slider') && wp_script_is('flexslider', 'registered') && !wp_script_is('flexslider', 'enqueued')) {
                    wp_enqueue_script('flexslider');
                }
                if (current_theme_supports('wc-product-gallery-lightbox') && wp_script_is('photoswipe-ui-default', 'registered') && !wp_script_is('photoswipe-ui-default', 'enqueued')) {
                    wp_enqueue_script('photoswipe-ui-default');
                    if (function_exists('woocommerce_photoswipe')) {
                        add_action('wp_footer', 'woocommerce_photoswipe');
                    }
                }
            }
        }

        wp_enqueue_style(
            'fontawesomeiselect',
            $plugin_url . 'libraries/cmb2-field-faiconselect/css/faws/css/font-awesome.min.css',
            [],
            null
        );
    }
}
