<?php 

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Loads the child theme textdomain.
 */
function wpdocs_child_theme_setup() {
    load_child_theme_textdomain( 'danvatwo', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'wpdocs_child_theme_setup' );

if ( ! defined( 'REALTIME_THEMES_HELPER_PATH' ) ) {
    define( 'REALTIME_THEMES_HELPER_PATH', get_stylesheet_directory() . '/' );
}

if ( ! defined( 'REALTIME_THEMES_HELPER_URL' ) ) {
    define( 'REALTIME_THEMES_HELPER_URL', get_stylesheet_directory_uri() . '/' );
}

add_action('admin_enqueue_scripts', function () {
    wp_enqueue_style(
        'my-font',
        'https://fonts.googleapis.com/css2?family=Bai+Jamjuree:wght@400;600;700&display=swap',
        [],
        null
    );
});


/**
 * Theme button admin bar
 */
add_action('admin_bar_menu', function ($wp_admin_bar) {
    if (! current_user_can('manage_options')) {
        return;
    }

    $wp_admin_bar->add_node([
        'id'    => 'real_time_themes_helper_button2',
        'title' => '<span style="
            color:#fff;
            background:#000;
            padding:0px 8px; 
            height: 100%; 
            font-weight: 700; 
            display: flex; 
            align-items: center; 
            gap: 5px; 
            width: 100%; 
            justify-content: center
        ">
            <img src="http://realtimethemes.com/img/realtimethemes_logo.png" 
                 alt="Real Time Themes Logo" 
                 style="height: 15px; width: auto; object-fit: cover;" />
            Real Time Themes Helper
        </span>',
        'href'  => admin_url('admin.php?page=realtime-themes-helper'),
        'meta'  => [
            'class'  => 'realtime-themes-helper-button',
            'title'  => 'Open Real Time Themes Helper',
            'target' => '_blank',
        ]
    ]);
}, 100);

/**
 * Scripts
 */
add_action( 'enqueue_block_editor_assets', function() {

    $theme = wp_get_theme();

    if ( $theme->parent() ) {
        $theme = $theme->parent();
    }

    $theme_uri = $theme->get( 'ThemeURI' );

    $settings = get_option( 'realtime_themes_helper_settings', [
        'allowed_blocks' => [],
    ]);

    wp_enqueue_script(
        'realtime-themes-helper-editor',
        get_stylesheet_directory_uri() . '/assets/js/script.js',
        [ 'wp-hooks', 'wp-element', 'wp-edit-post' ],
        filemtime( get_stylesheet_directory() . '/assets/js/script.js' ),
        true
    );

    error_log( 'Theme URI detectado: ' . $theme_uri );


    wp_localize_script(
        'realtime-themes-helper-editor',
        'RealtimeThemesHelper',
        [ 'allowedBlocks' => $settings['allowed_blocks'], 'themeUri' => $theme_uri ]
    );
}, 20);

$files = [
    'settings.php',
    'admin-page.php',
];

foreach ( $files as $file ) {
    $path = REALTIME_THEMES_HELPER_PATH . 'assets/php/' . $file;
    if ( file_exists( $path ) ) {
        require_once $path;
    }
}
