<?php 

class RsFrontAssets{
    public static function init(): void
    {
        add_action('wp_enqueue_scripts', [self::class, 'enqueue']);
    }
    private static function isResalesRequest(): bool
    {
        return (bool) (get_query_var('rs_zoddak_route_base') || get_query_var('rs_zoddak_property_ref'));
    }

    public static function enqueue(): void
    {
        if(!self::isResalesRequest()){
            return;
        }
        $options = [
            'bgFeatureColor'    => get_option('rs-zoddak-icons-color', '#110400'),
            'bgTxtFeatureColor' => get_option('rs-zoddak-icons-txt-color', '#000000'),
            'bgStageColor'      => get_option('rs-zoddak-btn-stage-color'),
            'bgPriceColor'      => get_option('rs-zoddak-btn-price-color'),
        ];
        
        $dynamicCss = "
        :root {
            --rs-bg-features-color: {$options['bgFeatureColor']};
            --rs-color-text-features: {$options['bgTxtFeatureColor']};
            --rs-bg-price-color: {$options['bgPriceColor']};
            --rs-bg-stage-color: {$options['bgStageColor']};
            --rs-border-value : {$options['border']};
        }";

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

        $options = [
            'apiUrl'          => get_option('rs_zoddak_api_url'),
            'token'           => get_option('rs_zoddak_api_token'),
            'perPage'         => get_option('rs_zoddak_items_per_page', 10),
            'layout'          => get_option('rs_zoddak_property_layout', 'horizontal'),
            'cardSize'        => get_option('rs_zoddak_card_size', 'small'),
            'cardBg'          => get_option('rs_zoddak_card_bg_color', '#ffffff'),
            'textSize'        => get_option('rs_zoddak_text_size', '14px'),
            'pageWidth'       => get_option('rs_zoddak_page_width', '1200px'),
            'btnDetails'      => get_option('rs_zoddak_color_button_details', '#020202'),
            'btnTypeColor'    => get_option('rs_zoddak_type_property_btn_color', '#dddd0f'),
            'btnTypeTxt'      => get_option('rs_zoddak_type_property_txt_color', '#ffffff'),
            'hoverColor'      => get_option('rs_zoddak_hover_color', '#110400'),
            'currentColor'    => get_option('rs_zoddak_current_page_color', '#110400'),
            'filterBg'        => get_option('rs-zoddak-bg-color-search-menu', '#f5f5f5'),
            'filterBtn'       => get_option('rs-zoddak-btn-color-search-menu', '#070100'),
            'filterReset'     => get_option('rs-zoddak-reset-btn-color', '#ffffff'),
            'borderRadius'    => get_option('rs_zoddak_border_rounded', true),
            'horizontalLine'  => get_option('rs_zoddak_color_horizontal_line', '#c61c1c'),
            'currentPageColor'=> get_option('rs_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(
            'rs-zoddak-page-properties-style',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/css/rs-page-properties.css',
            [],
            filemtime(ZODDAK_RESALES_PLUGIN_PATH . 'assets/css/rs-page-properties.css')
        );
        wp_add_inline_style('rs-zoddak-page-properties-style', $dynamic_css);

        wp_enqueue_script(
            'rs-zoddak-property-page',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/js/rs-page-properties.js',
            [],
            filemtime(ZODDAK_RESALES_PLUGIN_PATH . 'assets/js/rs-page-properties.js'),
            true
        );

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

        wp_localize_script('rs-zoddak-services', 'RS_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(
            'rs-property-details-script',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/js/rs-property-details-script.js',
            array('swiper-js', 'bootstrap-js'),
            '1.0.0',
            true
        );
        if ( ! wp_style_is('rs-zoddak-fontawesome', 'enqueued') ) {
            wp_enqueue_style(
                'rs-zoddak-fontawesome',
                'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css',
                [],
                '6.5.0'
            );
        }
    }
}