<?php 

class FrontAssets{
    public static function init(): void
    {
        add_action('wp_enqueue_scripts', [self::class, 'enqueue']);
    }
    private static function isNewDevsRequest(): bool
    {
        return (bool) (get_query_var('zoddak_route_base') || get_query_var('zoddak_property_ref'));
    }

    public static function enqueue(): void
    {
        if(!self::isNewDevsRequest()){
            return;
        }
        $options = [
            'bgFeatureColor'    => get_option('zoddak-icons-color', '#110400'),
            'bgTxtFeatureColor' => get_option('zoddak-icons-txt-color', '#000000'),
            'bgStageColor'      => get_option('zoddak-btn-stage-color'),
            'bgPriceColor'      => get_option('zoddak-btn-price-color'),
        ];

        $dynamicCss = "
        :root {
            --bg-features-color: {$options['bgFeatureColor']};
            --color-text-features: {$options['bgTxtFeatureColor']};
            --bg-price-color: {$options['bgPriceColor']};
            --bg-stage-color: {$options['bgStageColor']};
        }";

        wp_enqueue_style(
            'zoddak-property-detail-style',
            ZODDAK_PLUGIN_URL . 'assets/css/property-details.css',
            [],
            filemtime(ZODDAK_PLUGIN_PATH . 'assets/css/property-details.css')
        );
        wp_add_inline_style('zoddak-property-detail-style', $dynamicCss);

        $options = [
            'apiUrl'          => get_option('zoddak_api_url'),
            'token'           => get_option('zoddak_api_token'),
            'perPage'         => get_option('zoddak_items_per_page', 10),
            'layout'          => get_option('zoddak_property_layout', 'horizontal'),
            'cardSize'        => get_option('zoddak_card_size', 'small'),
            'cardBg'          => get_option('zoddak_card_bg_color', '#ffffff'),
            'textSize'        => get_option('zoddak_text_size', '14px'),
            'pageWidth'       => get_option('zoddak_page_width', '1200px'),
            'btnDetails'      => get_option('zoddak_color_button_details', '#020202'),
            'btnTypeColor'    => get_option('zoddak_type_property_btn_color', '#dddd0f'),
            'btnTypeTxt'      => get_option('zoddak_type_property_txt_color', '#ffffff'),
            'hoverColor'      => get_option('zoddak_hover_color', '#110400'), // 👈 ojo con tu ...
            'currentColor'    => get_option('zoddak_current_page_color', '#110400'),
            'filterBg'        => get_option('zoddak-bg-color-search-menu', '#f5f5f5'),
            'filterBtn'       => get_option('zoddak-btn-color-search-menu', '#070100'),
            'filterReset'     => get_option('zoddak-reset-btn-color', '#ffffff'),
            'borderRadius'    => get_option('zoddak_border_rounded', true),
            'horizontalLine'  => get_option('zoddak_color_horizontal_line', '#c61c1c'),
            'currentPageColor'=> get_option('zoddak_current_page_color', 'black'),
        ];

        $dynamic_css = "
        :root{
            --page-width: {$options['pageWidth']};
            --text-size: {$options['textSize']};
            --card-size: {$options['cardSize']};
            --card-bg: {$options['cardBg']};
            --btn-details: {$options['btnDetails']};
            --type-btn-bg: {$options['btnTypeColor']};
            --type-btn-color: {$options['btnTypeTxt']};
            --hover-color: {$options['hoverColor']};
            --current-color: {$options['currentColor']};
            --filter-bg: {$options['filterBg']};
            --filter-btn: {$options['filterBtn']};
            --filter-reset: {$options['filterReset']};
            --border-radius: {$options['borderRadius']};
            --line-color: {$options['horizontalLine']};
            --current-page-color: {$options['currentPageColor']};
        }";
        wp_enqueue_style(
            'zoddak-page-properties-style',
            ZODDAK_PLUGIN_URL . 'assets/css/page-properties.css',
            [],
            filemtime(ZODDAK_PLUGIN_PATH . 'assets/css/page-properties.css')
        );
        wp_add_inline_style('zoddak-page-properties-style', $dynamic_css);

        wp_enqueue_script(
            'zoddak-property-page',
            ZODDAK_PLUGIN_URL . 'assets/js/page-properties.js',
            [],
            filemtime(ZODDAK_PLUGIN_PATH . 'assets/js/page-properties.js'),
            true
        );

        wp_enqueue_script(
            'zoddak-services',
            ZODDAK_PLUGIN_URL . 'assets/js/services.js',
            ['jquery'],
            '1.0',
            true
        );

        wp_localize_script('zoddak-services', 'ZODDAK_STATS', [
            'ajax_url' => admin_url('admin-ajax.php'),
        ]);

        wp_enqueue_style('bootstrap-css', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css');
        wp_enqueue_script('bootstrap-js', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js', array('jquery'), '5.3.3', true);

        wp_enqueue_style('swiper-css', 'https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css');
        wp_enqueue_script('swiper-js', 'https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js', array(), '11.0.0', true);

        wp_enqueue_script(
            'property-details-script',
            ZODDAK_PLUGIN_URL . 'assets/js/property-details-script.js',
            array('swiper-js', 'bootstrap-js'),
            '1.0.0',
            true
        );
        if ( ! wp_style_is('zoddak-fontawesome', 'enqueued') ) {
            wp_enqueue_style(
                'zoddak-fontawesome',
                'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css',
                [],
                '6.5.0'
            );
        }
    }
}