<?php
if (!defined('ABSPATH')) exit;

class RsAdminAssets
{
    public static function init(): void
    {
        add_action('admin_enqueue_scripts', [self::class, 'enqueue']);
    }

    public static function enqueue(string $hook_suffix): void
    {
        $screen = function_exists('get_current_screen') ? get_current_screen() : null;
        $screen_id = $screen ? $screen->id : '';

        // $allowed = [
        //     'toplevel_page_rs-zoddak',
        //     'rs-zoddak_page_rs-zoddak-settings',
        //     'settings_page_rs-zoddak',
        // ];

        // if ($screen_id && !in_array($screen_id, $allowed, true)) {
        //     return;
        // }

        // Si no estás seguro del screen id, usa hook_suffix temporalmente:
        // error_log("hook_suffix=$hook_suffix screen_id=$screen_id");

        wp_enqueue_style('wp-color-picker');
        wp_enqueue_script('wp-color-picker');
        wp_add_inline_script(
            'wp-color-picker',
            'jQuery(document).ready(function($){$(".my-color-field").wpColorPicker();});'
        );

        wp_enqueue_script(
            'rs-zoddak-admin-js',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/js/rs-custom-properties-display.js',
            ['jquery', 'wp-color-picker'],
            '1.0.0',
            true
        );

        wp_enqueue_script(
            'rs-zoddak-custom-filter-card',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/js/rs-custom-filter-card.js',
            ['jquery', 'wp-color-picker'],
            '1.0.0',
            true
        );

        wp_localize_script('rs-zoddak-admin-js', 'zoddakVars', [
            'previewSelector' => '#property-preview'
        ]);

        $options = [
            'horizontalLineColor' => get_option('rs_zoddak_color_horizontal_line', '#c61c1c'),
            'typeRefColor'        => get_option('rs_zoddak_type_property_btn_color'),
            'currentPageColor'    => get_option('rs_zoddak_current_page_color'),
            'cardBgColor'         => get_option('rs_zoddak_card_bg_color'),
            'cardBorder'          => get_option('rs_zoddak_border_rounded'),
        ];

        wp_enqueue_style(
            'rs-zoddak-admin-style-base',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/css/rs-style.css',
            [],
            '1.0.0'
        );

        $root = "
            :root{
                --line-hor: {$options['horizontalLineColor']};
                --ref-type-color: {$options['typeRefColor']};
                --current-page-color: {$options['currentPageColor']};
                --card-bg-color: {$options['cardBgColor']};
                --card-border: {$options['cardBorder']};
            }
        ";
        wp_add_inline_style('rs-zoddak-admin-style-base', $root);

        $filterOpts = [
            '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'),
        ];

        wp_enqueue_style(
            'rs-zoddak-customize-filter',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/css/rs-custom-filter-card.css',
            [],
            '1.0.0'
        );

        $rootFilter = "
            :root{
                --filter-bg: {$filterOpts['filterBg']};
                --filter-btn: {$filterOpts['filterBtn']};
                --filter-reset: {$filterOpts['filterReset']};
            }
        ";
        wp_add_inline_style('rs-zoddak-customize-filter', $rootFilter);

        wp_enqueue_style(
            'rs-zoddak-admin-style-detail',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/css/rs-custom-details-page.css',
            ['dashicons'],
            '1.0.0'
        );

        $detailOpts = [
            'bgFeatureColor'    => get_option('rs-zoddak-icons-color', '#110400'),
            'bgTxtFeatureColor' => get_option('rs-zoddak-icons-txt-color'),
        ];

        $dynamicCss = "
            :root {
                --bg-features-color : {$detailOpts['bgFeatureColor']};
                --color-text-features : {$detailOpts['bgTxtFeatureColor']};
            }
        ";
        wp_add_inline_style('rs-zoddak-admin-style-detail', $dynamicCss);

        // Mejor que FontAwesome sea global (un solo handle), pero si lo dejas aquí, ok:
        wp_enqueue_style(
            'rs-zoddak-fontawesome',
            'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css',
            [],
            '6.5.0'
        );

        wp_enqueue_script(
            'rs-zoddak-custom-details-interactions',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/js/rs-custom-details-page.js',
            ['jquery'],
            '1.0.0',
            true
        );

        wp_enqueue_script(
            'zoddak-resales-admin',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/js/resales_admin_settings.js',
            ['jquery'],
            '1.0.0',
            true
        );

        wp_enqueue_style(
            'select2-css',
            'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css'
        );
        wp_enqueue_script(
            'select2-js',
            'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js',
            ['jquery'],
            null,
            true
        );

        wp_enqueue_script(
            'rs-zoddak-admin-routes',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/js/resales-routes.js',
            [],
            '1.0.0'
        );

        wp_enqueue_style(
            'rs-zoddak-admin-settings',
            ZODDAK_RESALES_PLUGIN_URL . 'assets/css/resales-admin-settings.css',
            [],
            '1.0.0'
        );
       
        wp_enqueue_style('dashicons');

        wp_enqueue_style(
            'property-details-fontawesome',
            'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css',
            [],
            '5.15.4'
        );
        wp_enqueue_style('leaflet-css', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css');
        wp_enqueue_script('leaflet-js', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js', [], null, true);
        wp_enqueue_style('select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css');
        wp_enqueue_script('select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js', ['jquery'], null, true);
        wp_add_inline_script('select2', "jQuery(document).ready(function($){ $('#area').select2({ placeholder: 'Select areas', width: '100%', allowClear: true }); });");
    }
}